Serial with strings and char

im trying to read a string from serial so i can change the value of an variable. right now my serial is only setup to read char which works fine untill i need to change a variable that hold a string of numbers. for instance if,

var=10000

and i want to change it to something else that i cant hardcode into arduinos code then i need to be able to read a string from serial. ive been reading posts and a lot of them talk about character arrays which i dont know anything about. how can i read a string and char from serial. i want to make it like below,

 if (Serial.available()) {
    char mode = Serial.read();
if (mode == 's') {
 while(Serial.available() > 0) 
      ///READ STRING OF NUMBERS
      }
then
     var=(stringofnumbers);

what the most simple way to read an undetermined length string of number and store then in a variable?

The serial input basics thread may help.

The longest possible string of digits you could store in a variable, would be if you entered an int64 as binary.

good info thanks @Robin2 and @groundFungus and @TolpuddleSartre

based on this example,

// Example 2 - Receive with an end-marker

const byte numChars = 32;
char receivedChars[numChars];   // an array to store the received data

boolean newData = false;

void setup() {
    Serial.begin(9600);
    Serial.println("<Arduino is ready>");
}

void loop() {
    recvWithEndMarker();
    showNewData();
}

void recvWithEndMarker() {
    static byte ndx = 0;
    char endMarker = '\n';
    char rc;
    
    while (Serial.available() > 0 && newData == false) {
        rc = Serial.read();

        if (rc != endMarker) {
            receivedChars[ndx] = rc;
            ndx++;
            if (ndx >= numChars) {
                ndx = numChars - 1;
            }
        }
        else {
            receivedChars[ndx] = '\0'; // terminate the string
            ndx = 0;
            newData = true;
        }
    }
}

void showNewData() {
    if (newData == true) {
        Serial.print("This just in ... ");
        Serial.println(receivedChars);
        newData = false;
    }
}

how could i set a flag in the code like,

if (receivedChars == “my string of text”) {
then do somthing
}

not sure why it don’t work for me

Use strcmp

You can't compare strings like that but you can use the strcmp() function

if (strcmp(receivedChars, "my string of text") == 0)

UKHeliBob:
You can't compare strings like that but you can use the strcmp() function

if (strcmp(receivedChars, "my string of text") == 0)

can you please tell me what the ==0) at the end of "if (strcmp(receivedChars, "my string of text") == 0)"
is about

im trying to nest these if statements together but when i type "test 1" it just keeps repeating working 1 in the console. if i type test 2 it works fine, what am i doing wrong here?

void showNewData() {
    if (newData == true) {
        Serial.print("This just in ... ");
        Serial.println(receivedChars);
              if (strcmp(receivedChars, "test 2") == 0) {
    Serial.println("working 2");
  }
        newData = false;
    } else
    
          if (strcmp(receivedChars, "test 1") == 0) {
    Serial.println("working 1");
  }
        newData = false;
    
}

notsolowki:
can you please tell me what the ==0) at the end of "if (strcmp(receivedChars, "my string of text") == 0)"
is about

If the two strings match then strcmp() will return 0

See strcmp - C++ Reference

UKHeliBob:
If the two strings match then strcmp() will return 0

See strcmp - C++ Reference

thank you i really appreciate the help. can you please tell what the problem is when i run

  if (strcmp(receivedChars, "test 1") == 0) {
    Serial.println("working 1");
    newData = false;
  }

it just keeps printing "working 1" in the console?

Because the strings remain equal, because you're not resetting or clearing the received string?

TolpuddleSartre:
Because the strings remain equal, because you’re not resetting or clearing the received string?

im not sure what you mean. how do i clear a string. its weird because test 2 works just fine but test 1 just keeps printing? the top half of this code works untill test 1 which just keeps repeating. i though newdata = false; was suppose to take care of that?

void showNewData() {
    if (newData == true) {
        Serial.print("This just in ... ");
        Serial.println(receivedChars);
       if (strcmp(receivedChars, "test 2") == 0) {
       Serial.println("working 2");
  }
        newData = false;
    } 
           if (strcmp(receivedChars, "test 1") == 0) {
       Serial.println("working 1");
  }
        newData = false;
    }

how do i clear a string

Assigned '\0' to the first element.

TolpuddleSartre:
Assigned '\0' to the first element.

i did this and cleared the string and it still is doing the same thing??

void showNewData() {
    if (newData == true) {
        Serial.print("This just in ... ");
        Serial.println(receivedChars);
       if (strcmp(receivedChars, "test 2") == 0) {
       Serial.println("working 2");



  }
        newData = false;
    } 
           if (strcmp(receivedChars, "test 1") == 0) {
       Serial.println("working 1");
      receivedChars[numChars] = '\0';  
  }

also when it keeps printing working 1 if i type test 2 it prints test 2 q time and stops like its suppose to?

receivedChars[numChars] = '\0';  I said the first element, didn't I?

TolpuddleSartre:
receivedChars[numChars] = '\0';  I said the first element, didn't I?

do you mean like this? numChars[receivedChars] = '\0';

try to be a little more specific please

The first element.

Hope that's specific enough.

Don't forget to reset you array index too, ready for the next string.

TolpuddleSartre:
The first element.

Hope that's specific enough.

obviously it wasn't specific enough the first 2 times you said it..

if oyu looked at the example i posted

 while (Serial.available() > 0 && newData == false) {
        rc = Serial.read();

        if (rc != endMarker) {
            receivedChars[ndx] = rc;
            ndx++;
            if (ndx >= numChars) {
                ndx = numChars - 1;
            }
        }
        else {
            receivedChars[ndx] = '\0'; // terminate the string  //do you see this
            ndx = 0;
            newData = true;
        }
    }
}

so the question is what is the real problem

Without actually writing your code for you, I didn't know how to make it more obvious, so I thought maybe bold/underline might do the trick.