Pages: [1]   Go Down
Author Topic: Char array question  (Read 410 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 14
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Massachusetts, USA
Offline Offline
Tesla Member
***
Karma: 202
Posts: 8716
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

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

Offline Offline
Newbie
*
Karma: 0
Posts: 14
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Pages: [1]   Go Up
Jump to: