Go Down

Topic: Char array question (Read 461 times) previous topic - next topic

mndless

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...

Code: [Select]

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

johnwasser

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.
Code: [Select]

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):

Code: [Select]

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:

Code: [Select]

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);
}

Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

mndless

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

Go Up