Search code examples
stringarduinorgbmonitorlvalue

Arduino RGB LED lvalue problem in string variable reading from Serial Monitor


I'm working with an RGB Led in Arduino Uno but when I try to compile this message comes for this string 'myColor = Serial.readString();': In function 'void loop()': 21:29: error: lvalue required as left operand of assignment exit status 1

The attached screen dump provides the following code:

 1  int red=9;
 2  int green=10;
 3  int blue=11;
 4  String msg="What color LED?";
 5  String myColor;
 6
 7  void setup()
 8  {
 9    pinMode(red, OUTPUT);
10    pinMode(green, OUTPUT);
11    pinMode(blue, OUTPUT);
12    Serial.begin(9600);
13  }
14
15  void loop()
16  {
17    Serial.println(msg);
18    while(Serial.available()=0) {
19    }
20    myColor = Serial.readString();
21    if (myColor=="red"){
22      digitalWrite(red,HIGH);

and the resulting compiler error is:

In function 'void loop()':
20:29: error: lvalue required as left operand of assignment
 exit status 1

enter image description here


Solution

  • Your error is on line 18. You have mistakenly written while(Serial.available()=0){ (only one '=') so it looks like an assignment to the compiler (but the object to the left of the assignment operator is not an lvalue). You meant:

    while(Serial.available()==0){