Char array question

Need to update the value of a char array, and am failing....

searched the forum for "char array" and "change char array", did not find what I think I need....

Simply, depending on the value of the int MODE, I want to change the value of payload[]...

payload[] does not change. I've declared it as shown and moved it to inside the loop... no diff...

char payload[] = "153";
int MODE = 0;

void setup() {
  Serial.begin(9600); 
}

void loop() {

            MODE++;
            if (MODE > 3) {
                  MODE = 0;
            }
            if (MODE == 0) {
                 char payload[] = "150";

            } else if (MODE == 1) {
                 char payload[] = "151";

            } else if (MODE == 2) {
                 char payload[] = "152";

            } else if (MODE == 3) {
                 char payload[] = "153";
            } 
            Serial.print("\nPayload :: ");
            Serial.print(payload);
            Serial.print(" Mode :: ");
            Serial.print(MODE);
            delay(1000);
}

Has to be a trick to this.....any and all help much appreciated

Your problem is that by declaring a new and different 'payload' in each IF you create one that is local to that IF statement. When the statement ends that version of 'payload' is no longer usable so the rest of loop() uses the global 'payload'.

There are several ways to do it:

Easiest is probably to use a String object to which you can assign new values.

Stirng payload = "153";
int MODE = 0;

void setup() {
  Serial.begin(9600); 
}

void loop() {

            MODE++;
            if (MODE > 3) {
                  MODE = 0;
            }
            if (MODE == 0) {
                 payload = "150";

            } else if (MODE == 1) {
                 payload = "151";

            } else if (MODE == 2) {
                 payload = "152";

            } else if (MODE == 3) {
                 payload = "153";
            } 
            Serial.print("\nPayload :: ");
            Serial.print(payload.toCharArray());
            Serial.print(" Mode :: ");
            Serial.print(MODE);
            delay(1000);
}

Another choice is to change just the single character different between cases. Note the use of character constants (single quotes) and not string constants (double quotes):

char payload[] = "153";
int MODE = 0;

void setup() {
  Serial.begin(9600); 
}

void loop() {

            MODE++;
            if (MODE > 3) {
                  MODE = 0;
            }
            if (MODE == 0) {
                 payload[2] = '0';

            } else if (MODE == 1) {
                 payload[2] = '1';

            } else if (MODE == 2) {
                 payload[2] = '2';

            } else if (MODE == 3) {
                 payload[2] = '3';
            } 
            Serial.print("\nPayload :: ");
            Serial.print(payload);
            Serial.print(" Mode :: ");
            Serial.print(MODE);
            delay(1000);
}

Third option is the "string copy" function which moves each character of a string to a character array. Note that this is dangerous because it doesn't check to see if the destination is large enough to hold the string being copied:

char payload[] = "153";
int MODE = 0;

void setup() {
  Serial.begin(9600); 
}

void loop() {

            MODE++;
            if (MODE > 3) {
                  MODE = 0;
            }
            if (MODE == 0) {
                 strcpy(payload, "150");

            } else if (MODE == 1) {
                 strcpy(payload, "151");

            } else if (MODE == 2) {
                 strcpy(payload, "152");

            } else if (MODE == 3) {
                 strcpy(payload, "153");
            } 
            Serial.print("\nPayload :: ");
            Serial.print(payload);
            Serial.print(" Mode :: ");
            Serial.print(MODE);
            delay(1000);
}

Thanks! That worked like a champ.... much apprecaited!