lvalue required as left operand of assignment

Hi!

I am trying to get a basic robotic arm using pneumatics to work. I am getting an "lvalue required as left operand of assignment" error at the end of my code (in my bool function).

Here is the code:

#include <WiFi.h>

// Solenoid control
int solIn1 =  10; //upper arm 
int solOut1 =  11; //upper arm
int solIn2 =  12; // lower arm
int solOut2 =  13; //lower arm

//digital input from sensors
int sensor1 =  0;
int sensor2 =  0;

//input from user
char data = 0;

bool possibleInput(); 

//control variables
char cont1 = -1;
char cont2 = -1;

void setup() {
  
  // set the digital pin as output:
  pinMode(solIn1, OUTPUT); 
  pinMode(solOut1, OUTPUT);
  pinMode(solIn2, OUTPUT);
  pinMode(solOut2, OUTPUT);

  pinMode (A0, INPUT); //sensor1
  pinMode (A1, INPUT); //sensor2
  
  Serial.begin(9600);
  Serial.println("TRON ARM");

}

void loop() {

  while (Serial.available()){
    if (Serial.available() > 0)
    {
      // read input from user
      // A = Upper arm up
      // B = Upper arm down
      // C = Lower arm up
      // D = Lower arm down

      data = Serial.read();
      if (possibleInput(data)){
        
      if (data = "A"){
         digitalWrite (solIn1, HIGH);
         digitalWrite (solOut1, LOW);
         sensor1 = digitalRead(A0);
         while(sensor1 != 1)
         digitalWrite (solIn1, LOW);
         cont1 = 0;
      }

      if (data = "B"){
         digitalWrite (solOut1, HIGH);
         digitalWrite (solIn1, LOW);
         sensor1 = digitalRead(A0);
         if(sensor1 = 1)
         digitalWrite (solOut1, LOW);
         cont1 = 0;
      }

      if (data = "C"){
         digitalWrite (solIn2, HIGH);
         digitalWrite (solOut2, LOW);
         sensor1 = digitalRead(A1);
         if(sensor1 = 1)
         digitalWrite (solIn2, LOW);
         cont2 = 0;
      }      


      if (data = "D"){
         digitalWrite (solOut2, HIGH);
         digitalWrite (solIn2, LOW);
         sensor1 = digitalRead(A1);
         if(sensor2 = 1)
         digitalWrite (solOut2, LOW);
         cont2 = 0;
      }

      else delay (1000);
      }    
    }
  }
}

bool possibleInput(char data){
  if (data = "A" && cont1 =! 1)
  return true;

  else if (data = "C" && cont2 =! 1)
  return true;

  else if (data = "B" && cont1 =! -1)
  return true;

  else if (data = "D" && cont2 =! -1)
  return true;

  else return false;
}

Any help would be greatly appreciated :slight_smile:

== not =

1 Like

Check all your 'if' statements for equality. You are incorrectly using the assignment operator '=' instead of the equality operator '=='.

2 Likes

An lvalue normally equates to a memory address, so the error message "lvalue required as left operand of assignment" in the expression:

  • if (data = "A"){*

is saying the compiler does have a place in memory where to put "A". The reason is because you're trying to put a string variable ("A") into data, which is a char variable, not a string. Those data attributes don't match, so in effect it is saying that the data type of "A" doesn't match the data type for data so it doesn't know where to put the result.

Had you written:

  • if (data = 'A') {*

you would not have had the same error message because the syntax is correct. That is, it has a memory address (the lvalue of data) where it can place a single character, 'A'. However, it seems doubtful that the expression is really what you want (a semantic error). What you probably want is to compare data with 'A', as others have pointed out:

  • if (data == 'A') {*

@econjack

it is not data that is a problem (which is a legal lvalue), its the whole expression.

Try Ctrl-T and look at the result (as well as the errors generated by the compiler).

Somewhere\LValue\LValue.ino: In function 'bool possibleInput(char)':
LValue:97: error: lvalue required as left operand of assignment
   if (data = "A" && cont1 = ! 1)
                           ^
LValue:100: error: lvalue required as left operand of assignment
   else if (data = "C" && cont2 = ! 1)
                                ^
LValue:103: error: lvalue required as left operand of assignment
   else if (data = "B" && cont1 = ! -1)
                                ^
LValue:106: error: lvalue required as left operand of assignment
   else if (data = "D" && cont2 = ! -1)
                                ^

There are some warnings that should be fixed too

Somewhere\LValue\LValue.ino: In function 'bool possibleInput(char)':
LValue:97: error: lvalue required as left operand of assignment
   if (data = "A" && cont1 = ! 1)
                           ^
LValue:100: error: lvalue required as left operand of assignment
   else if (data = "C" && cont2 = ! 1)
                                ^
LValue:103: error: lvalue required as left operand of assignment
   else if (data = "B" && cont1 = ! -1)
                                ^
LValue:106: error: lvalue required as left operand of assignment
   else if (data = "D" && cont2 = ! -1)
                                ^
Somewhere\LValue\LValue.ino:110:1: warning: control reaches end of non-void function [-Wreturn-type]
 }
 ^