user58315: Basically I'm trying to output a string based upon a value in an array, the following code is what I have come up with to achieve my desired result, but I have a feeling that there is a better way to do it.
String day(int day) {
  if (day == 1) return "Sunday";      
  if (day == 2) return "Monday";
  if (day == 3) return "Tuesday";
  if (day == 4) return "Wednesday";
  if (day == 5) return "Thursday";
  if (day == 6) return "Friday";
  if (day == 7) return "Saturday";
  else return "Undefined";
}

void setup() {
  Serial.begin(57600);
  while (!Serial);
  Serial.println(day(1));
}

void loop() {
;
}

This prints "Sunday" in the Serial monitor
I would appreciate any input on how to optimize this code, thank you!

user4255: I'm no expert, but I imagine something along these lines would work:
char* day(int day) {

  static char* dayName[7] = {"Sunday", "Monday", .... };

  if (day > 0 && day < 8) {
    return dayName[day-1];
  } 
  else
    return "Undefined";
  }
}

In any case, I'll be interested to hear why it's wrong (-:

user4143: My method makes use of the __FlashStringHelper class:
// For convenience:
typedef const __FlashStringHelper *FlashString;


FlashString reverseEnum(int val) {
    switch (val) {
        case 0: return F("Option zero");
        case 1: return F("Option one");
        case 2: return F("Option two");
        default: return F("Invalid Enum");
    }
}

Printing now is as simple as:
Serial.println(reverseEnum(2));

Because it's a "flash string" the print function is overloaded properly and it performs the proper PROGMEM reading.
You can, of course, assign to a FlashString:
FlashString myString = reverseEnum(2);
Serial.println(myString);

Or if you don't want to use a typedef you can use the raw type:
const __FlashStringHelper *myString = reverseEnum(2);
Serial.println(myString);

It's just much cleaner to use a typedef.

58315: <p>Basically I'm trying to output a string based upon a value in an array, the following code is what I have come up with to achieve my desired result, but I have a feeling that there is a better way to do it.</p>

<pre><code>String day(int day) {
  if (day == 1) return "Sunday";      
  if (day == 2) return "Monday";
  if (day == 3) return "Tuesday";
  if (day == 4) return "Wednesday";
  if (day == 5) return "Thursday";
  if (day == 6) return "Friday";
  if (day == 7) return "Saturday";
  else return "Undefined";
}

void setup() {
  Serial.begin(57600);
  while (!Serial);
  Serial.println(day(1));
}

void loop() {
;
}
</code></pre>

<p>This prints "Sunday" in the Serial monitor</p>

<p>I would appreciate any input on how to optimize this code, thank you!</p>
 4255: <p>I'm no expert, but I imagine something along these lines would work:</p>

<pre><code>char* day(int day) {

  static char* dayName[7] = {"Sunday", "Monday", .... };

  if (day &gt; 0 &amp;&amp; day &lt; 8) {
    return dayName[day-1];
  } 
  else
    return "Undefined";
  }
}
</code></pre>

<p>In any case, I'll be interested to hear why it's wrong (-:</p>
 4143: <p>My method makes use of the <code>__FlashStringHelper</code> class:</p>

<pre><code>// For convenience:
typedef const __FlashStringHelper *FlashString;


FlashString reverseEnum(int val) {
    switch (val) {
        case 0: return F("Option zero");
        case 1: return F("Option one");
        case 2: return F("Option two");
        default: return F("Invalid Enum");
    }
}
</code></pre>

<p>Printing now is as simple as:</p>

<pre><code>Serial.println(reverseEnum(2));
</code></pre>

<p>Because it's a "flash string" the <code>print</code> function is overloaded properly and it performs the proper PROGMEM reading.</p>

<p>You can, of course, assign to a <code>FlashString</code>:</p>

<pre><code>FlashString myString = reverseEnum(2);
Serial.println(myString);
</code></pre>

<p>Or if you don't want to use a typedef you can use the raw type:</p>

<pre><code>const __FlashStringHelper *myString = reverseEnum(2);
Serial.println(myString);
</code></pre>

<p>It's just much cleaner to use a typedef.</p>
