problem reading strings

Hello i thing if i copy my code you can understand what i need to do, but let me explain to you.
I want to send a msg from a serial port and if this msg is lights,voltage i want to open 2 Relays.If the msg is end i want to close those Relays and if there is an other msg by mistake i want to send an error msg back.So i am testing it with two leds but the only thing i have is an error msg. Here is my code


const int Relay1=12;
const int Relay2=11;
String incoming;

void setup(){
Serial.begin(9600);
pinMode(Relay1,OUTPUT);
pinMode(Relay2,OUTPUT);
digitalWrite(Relay1,LOW);
digitalWrite(Relay2,LOW);
}

void loop(){
if(Serial.available()>0){
incoming=Serial.readString();
if (incoming=="lights\n"){
digitalWrite(Relay1,HIGH);
}
if (incoming=="voltage\n"){
if(Relay1==HIGH){
digitalWrite(Relay2,HIGH);
}
}
if (incoming=="end\n"){
if(Relay2==HIGH){
digitalWrite(Relay2,LOW);
digitalWrite(Relay1,LOW);
}
else{
digitalWrite(Relay1,LOW);
}
}
if (incoming!="voltage\n" || incoming!="lights\n" || incoming!="end\n"){
Serial.println("error");
}
}
}

2 possible errors

  1. you are not sending '\n' as the line ending the serial monitor allows a number of different line endings.

  2. readString() does NOT include the line ending as part of the string.

Mark

Ok...... first i had them without \n....... but i tried..... now i have already erase \n

First, the String class is a bit bulky and most programmers using the Arduino family prefer character arrays. If the longest message you might receive is 10 chars, then define your input buffer as:

char incoming[11];

You need the extra char because C strings are null (’\0’) terminated.

Second, you cannot use an expression like:

if (incoming==“lights\n”)

The C++ String class uses a compare() method to perform comparisons, but C uses strcmp(). So your code would have to change to something like:

if (strcmp(incoming, “lights”) == 0) { // Success when strcmp() returns 0

Alter you code accordingly and see what happens.

Also, since you’re a new poster, you should read and follow the suggestions made in the first to posts to this Forum by Nick Gammon before posting.

I wrote an extra command

Serial.println(incoming);

to see the value of this variable

and when i send lights incoming ==lights etc

but in comparison i don't know what happened

Second, you cannot use an expression like:

Quote
if (incoming=="lights\n")

== is overloaded when used with Strings and so it will work!

Mark

This is what I was suggesting so you can get rid of the String class:

const int Relay1=12;
const int Relay2=11;
int bytesRead;
char incoming[11];

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

void loop(){
  int matchFlag = 0;
  
  if(Serial.available()>0){
    bytesRead = Serial.readBytesUntil('\n', incoming, 10);
    incoming[bytesRead] = '\0';                        // Add null terminator...
    if (strcmp(incoming, "lights") == 0){
      Serial.println("Lights matched");
      matchFlag = 1;
    }
    if (strcmp(incoming, "voltage") == 0){
      Serial.println("Voltage matched");
      matchFlag = 1;
      if (Relay1 == HIGH) {
        digitalWrite(Relay2, HIGH);
      }
    }

    if (strcmp(incoming, "end") == 0){
      Serial.println("End matched");
      matchFlag = 1;

      if(Relay2==HIGH){
        digitalWrite(Relay2,LOW);
        digitalWrite(Relay1,LOW);
      }
      else{
        digitalWrite(Relay1,LOW);
      }
    }
    if (matchFlag == 0){
      Serial.println("error");
    }
  }
}

You should be able to figure out what you want to do from this.