Help with Serial Behaviour

Hello,

I am having trouble with reading strings from a serial port using my Arduino. I send one string, it is translated correctly, and the response is perfect. However, when I send a second string, it is not translate/evaluated perfectly well and I cannot get strings sent as commands (after the first command) to be executed. Can someone help to point out where I have gone wrong in my code?

Here is the code I have been using:

char incomingChar; // for incoming serial data
char serialMessage[75];
int i = 0;

void setup() {
   Serial.begin(9600); // opens serial port, sets data rate to 9600 bps
}

void loop() {

  while (Serial.available() > 0) {
    // read the incoming byte:
    incomingChar = Serial.read();
    serialMessage[i] = incomingChar;
    //Serial.print(serialMessage[i]);
    i++;
  }

    String incomingString(serialMessage);
    if(incomingString == "Stand") {
      Stand();
      Serial.print("I received: ");
      Serial.println(incomingString);
      i = 0;
      delay(1000);
    }
    if(incomingString == "Step") {
      Step();
      Serial.print("I received: ");
      Serial.println(incomingString);
      i = 0;
      delay(1000);
    }
  }

There are some more methods within my code underneath, such as 'Stand' and 'Step', but I left those out for brevity.

Thank you

Hi @sp220

How your serial monitor is configured?
Below are 4 options.
NO LINE ENDING
NEWLINE
CARRIAGE RETURN
BOTH NL & CR

Select NO LINE ENDING and test your sketch again

RV mineirin
Serial monitor

Thank you for that! This helped a little. I made some incremental progress. I can now send 2 commands before it stops working just as it did before. Here is the amended code:

char incomingChar; // for incoming serial data
char serialMessage[75];
int i = 0;

void setup() {
   Serial.begin(9600); // opens serial port, sets data rate to 9600 bps
}

void loop() {

  while (Serial.available() > 0) {
    // read the incoming byte:
    incomingChar = Serial.read();
    serialMessage[i] = incomingChar;
    i++;
  }

    String incomingString(serialMessage);
    if(incomingString == "Stand") {
      Stand();
      Serial.print("I received: ");
      Serial.println(incomingString);
      i = 0;
      serialMessage[i] = 0;
      delay(1000);
      
    }
    if(incomingString == "Step") {
      Step();
      Serial.print("I received: ");
      Serial.println(incomingString);
      i = 0;
      serialMessage[i] = 0;
      delay(1000);
    }
  
}

I recommend the serial input basics tutorial, robust ways to read serial data.

Thank you! That took care of everything. Thanks.

Hi @sp220

Simplifying your sketch.

RV mineirin


String incomingString = "";

void setup() {
  Serial.begin(9600); // opens serial port, sets data rate to 9600 bps
}

void loop() {

  while (Serial.available() > 0) {
    incomingString = Serial.readString();
  }

  if (incomingString == "Stand") {
    Stand();
    Serial.print("I received: ");
    Serial.println(incomingString);
    delay(1000);
  }
  if (incomingString == "Step") {
    Step();
    Serial.print("I received: ");
    Serial.println(incomingString);
    delay(1000);
  }
  incomingString = "";
}

For a selection of sketches to read from Serial using Strings and SafeString see my tutorial Arduino Software Solutions
The pros and cons of each approach are listed

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.