Error lvalue required as left operand of assignment for hydroponic sensors

I need some help with my project, i've made a project about controlling nutrition and Ph level of hydroponics water, my project using lcd and TDS sensor and PH sensor, and for make it more efficient i try to separate the code to different tabs.

Here is my code for the LCD and also the menus

#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 16, 2);
#include "sensor.h"
#include "water_level.h"
#include "peristaltic_pump.h"
#include "menu.h"

int analog_tds;
int analod_ph;

int WhichScreen =0;
boolean hasChanged = true;
const int btnMenu = 4;
int buttonState;
int lastButtonState = LOW;
unsigned long lastDebounceTime = 0;
unsigned long debounceDelay = 50;

void setup() {
  pinMode(TdsSensorPin,INPUT);  
  pinMode(ph_pin,INPUT);
  pinMode(dc_pump1, OUTPUT);
  pinMode(btnMenu, INPUT);

  lcd.init();
  lcd.backlight();

  Serial.begin(115200);
}

void loop() {  
  if (hasChanged == true) {
   
  switch(WhichScreen) {
    case 1:{
      firstScreen();
    }
      break;
   
    case 2:{
      secondScreen();
    }
      break;
   
    case 3:{
      thirdScreen();
    }
      break;
   
    case 4:{
      fourthScreen();
    }
      break;
   
    case 5:{
      fifthScreen();
    }
      break;
   
    case 6:{
      sixthScreen();
    }
      break;
    case 0:{
      }
      break;
    }
}
    int reading = digitalRead(btnMenu);
  if (reading != lastButtonState) {
    lastDebounceTime = millis();
  }
 
if ((millis() - lastDebounceTime) > debounceDelay) {
    if (reading != buttonState) {
      buttonState = reading;
 
      if (buttonState == HIGH) {
        hasChanged = true;
        WhichScreen++;   
      }
    } else {
      hasChanged = false;
    }
  }
  lastButtonState = reading;
  
  if (WhichScreen > 6){
    WhichScreen = 0;
  }
}

and here is the code for the sensors

//==========PH============
#define ph_pin A0
#define Po 0
float PH_step;
int nilai_analog_PH;
double TeganganPh;

//untuk kalibrasi
float PH4 = 3.15;
float PH7 = 2.5;
//========================

//==========TDS===========
#define TdsSensorPin A1
#define VREF 5.0
#define SCOUNT  30
int analogBuffer[SCOUNT];
int analogBufferTemp[SCOUNT];
int analogBufferIndex = 0, copyIndex = 0;
float averageVoltage = 0, tdsValue = 0, temperature = 25;
//========================

int getMedianNum(int bArray[], int iFilterLen)
{
  int bTab[iFilterLen];
  for (byte i = 0; i < iFilterLen; i++)
    bTab[i] = bArray[i];
  int i, j, bTemp;
  for (j = 0; j < iFilterLen - 1; j++)
  {
    for (i = 0; i < iFilterLen - j - 1; i++)
    {
      if (bTab[i] > bTab[i + 1])
      {
        bTemp = bTab[i];
        bTab[i] = bTab[i + 1];
        bTab[i + 1] = bTemp;
      }
    }
  }
  if ((iFilterLen & 1) > 0)
    bTemp = bTab[(iFilterLen - 1) / 2];
  else
    bTemp = (bTab[iFilterLen / 2] + bTab[iFilterLen / 2 - 1]) / 2;
  return bTemp;
}

int sensor_tds() {
  static unsigned long analogSampleTimepoint = millis();
  if (millis() - analogSampleTimepoint > 40U)
  {
    analogSampleTimepoint = millis();
    analogBuffer[analogBufferIndex] = analogRead(A1);
    analogBufferIndex++;
    if (analogBufferIndex == SCOUNT)
      analogBufferIndex = 0;
  }
  static unsigned long printTimepoint = millis();
  if (millis() - printTimepoint > 2000U)
  {
    printTimepoint = millis();
    for (copyIndex = 0; copyIndex < SCOUNT; copyIndex++)
      analogBufferTemp[copyIndex] = analogBuffer[copyIndex];
    averageVoltage = getMedianNum(analogBufferTemp, SCOUNT) * (float)VREF / 1024.0;
    float compensationCoefficient = 1.0 + 0.02 * (temperature - 25.0);
    float compensationVolatge = averageVoltage / compensationCoefficient;
    tdsValue = (133.42 * compensationVolatge * compensationVolatge * compensationVolatge
                - 255.86 * compensationVolatge * compensationVolatge + 857.39 * compensationVolatge) * 0.5;
  }
}

int sensor_ph() {
  nilai_analog_PH = analogRead(A0);
  TeganganPh = 5.0 / 1024.0 * nilai_analog_PH;

  PH_step = (PH4 - PH7) / 3;
  Po = 7.00 + ((PH7 - TeganganPh) / PH_step);
}

The problem located at Po the error says lvalue required as left operand of assignment.

Thank you for your time to respond my problem

Post the actual error message. Note that the line number tells you where the error was discovered.

Po is created by a #define and is zero. You can't assign a number to a number.

The error says this

Arduino: 1.8.17 Hourly Build 2021/09/06 02:33 (Windows 10), Board: "Arduino Uno"
In file included from C:\Users\Ghoond\Documents\Arduino\Tes_sensors_dif_tab\Tes_sensors_dif_tab.ino:4:0:
C:\Users\Ghoond\Documents\Arduino\Tes_sensors_dif_tab\sensor.h: In function 'int sensor_ph()':
sensor.h:77:44: error: lvalue required as left operand of assignment
   Po = 7.00 + ((PH7 - TeganganPh) / PH_step);
                                            ^
In file included from C:\Users\Ghoond\Documents\Arduino\Tes_sensors_dif_tab\menu.h:1:0,
                 from C:\Users\Ghoond\Documents\Arduino\Tes_sensors_dif_tab\Tes_sensors_dif_tab.ino:7:
C:\Users\Ghoond\Documents\Arduino\Tes_sensors_dif_tab\sensor.h: At global scope:
sensor.h:4:7: error: redefinition of 'float PH_step'
 float PH_step;
       ^~~~~~~
In file included from C:\Users\Ghoond\Documents\Arduino\Tes_sensors_dif_tab\Tes_sensors_dif_tab.ino:4:0:
C:\Users\Ghoond\Documents\Arduino\Tes_sensors_dif_tab\sensor.h:4:7: note: 'float PH_step' previously declared here
 float PH_step;
       ^~~~~~~
In file included from C:\Users\Ghoond\Documents\Arduino\Tes_sensors_dif_tab\menu.h:1:0,
                 from C:\Users\Ghoond\Documents\Arduino\Tes_sensors_dif_tab\Tes_sensors_dif_tab.ino:7:
sensor.h:5:5: error: redefinition of 'int nilai_analog_PH'
 int nilai_analog_PH;
     ^~~~~~~~~~~~~~~
In file included from C:\Users\Ghoond\Documents\Arduino\Tes_sensors_dif_tab\Tes_sensors_dif_tab.ino:4:0:
C:\Users\Ghoond\Documents\Arduino\Tes_sensors_dif_tab\sensor.h:5:5: note: 'int nilai_analog_PH' previously declared here
 int nilai_analog_PH;
     ^~~~~~~~~~~~~~~
In file included from C:\Users\Ghoond\Documents\Arduino\Tes_sensors_dif_tab\menu.h:1:0,
                 from C:\Users\Ghoond\Documents\Arduino\Tes_sensors_dif_tab\Tes_sensors_dif_tab.ino:7:
sensor.h:6:8: error: redefinition of 'double TeganganPh'
 double TeganganPh;
        ^~~~~~~~~~
In file included from C:\Users\Ghoond\Documents\Arduino\Tes_sensors_dif_tab\Tes_sensors_dif_tab.ino:4:0:
C:\Users\Ghoond\Documents\Arduino\Tes_sensors_dif_tab\sensor.h:6:8: note: 'double TeganganPh' previously declared here
 double TeganganPh;
        ^~~~~~~~~~
In file included from C:\Users\Ghoond\Documents\Arduino\Tes_sensors_dif_tab\menu.h:1:0,
                 from C:\Users\Ghoond\Documents\Arduino\Tes_sensors_dif_tab\Tes_sensors_dif_tab.ino:7:
sensor.h:9:7: error: redefinition of 'float PH4'
 float PH4 = 3.15;
       ^~~
In file included from C:\Users\Ghoond\Documents\Arduino\Tes_sensors_dif_tab\Tes_sensors_dif_tab.ino:4:0:
C:\Users\Ghoond\Documents\Arduino\Tes_sensors_dif_tab\sensor.h:9:7: note: 'float PH4' previously defined here
 float PH4 = 3.15;
       ^~~
In file included from C:\Users\Ghoond\Documents\Arduino\Tes_sensors_dif_tab\menu.h:1:0,
                 from C:\Users\Ghoond\Documents\Arduino\Tes_sensors_dif_tab\Tes_sensors_dif_tab.ino:7:
sensor.h:10:7: error: redefinition of 'float PH7'
 float PH7 = 2.5;
       ^~~
In file included from C:\Users\Ghoond\Documents\Arduino\Tes_sensors_dif_tab\Tes_sensors_dif_tab.ino:4:0:
C:\Users\Ghoond\Documents\Arduino\Tes_sensors_dif_tab\sensor.h:10:7: note: 'float PH7' previously defined here
 float PH7 = 2.5;
       ^~~
In file included from C:\Users\Ghoond\Documents\Arduino\Tes_sensors_dif_tab\menu.h:1:0,
                 from C:\Users\Ghoond\Documents\Arduino\Tes_sensors_dif_tab\Tes_sensors_dif_tab.ino:7:
sensor.h:17:24: error: redefinition of 'int analogBuffer [30]'
 int analogBuffer[SCOUNT];
                        ^
In file included from C:\Users\Ghoond\Documents\Arduino\Tes_sensors_dif_tab\Tes_sensors_dif_tab.ino:4:0:
C:\Users\Ghoond\Documents\Arduino\Tes_sensors_dif_tab\sensor.h:17:5: note: 'int analogBuffer [30]' previously declared here
 int analogBuffer[SCOUNT];
     ^~~~~~~~~~~~
In file included from C:\Users\Ghoond\Documents\Arduino\Tes_sensors_dif_tab\menu.h:1:0,
                 from C:\Users\Ghoond\Documents\Arduino\Tes_sensors_dif_tab\Tes_sensors_dif_tab.ino:7:
sensor.h:18:28: error: redefinition of 'int analogBufferTemp [30]'
 int analogBufferTemp[SCOUNT];
                            ^
In file included from C:\Users\Ghoond\Documents\Arduino\Tes_sensors_dif_tab\Tes_sensors_dif_tab.ino:4:0:
C:\Users\Ghoond\Documents\Arduino\Tes_sensors_dif_tab\sensor.h:18:5: note: 'int analogBufferTemp [30]' previously declared here
 int analogBufferTemp[SCOUNT];
     ^~~~~~~~~~~~~~~~
In file included from C:\Users\Ghoond\Documents\Arduino\Tes_sensors_dif_tab\menu.h:1:0,
                 from C:\Users\Ghoond\Documents\Arduino\Tes_sensors_dif_tab\Tes_sensors_dif_tab.ino:7:
sensor.h:19:5: error: redefinition of 'int analogBufferIndex'
 int analogBufferIndex = 0, copyIndex = 0;
     ^~~~~~~~~~~~~~~~~
In file included from C:\Users\Ghoond\Documents\Arduino\Tes_sensors_dif_tab\Tes_sensors_dif_tab.ino:4:0:
C:\Users\Ghoond\Documents\Arduino\Tes_sensors_dif_tab\sensor.h:19:5: note: 'int analogBufferIndex' previously defined here
 int analogBufferIndex = 0, copyIndex = 0;
     ^~~~~~~~~~~~~~~~~
In file included from C:\Users\Ghoond\Documents\Arduino\Tes_sensors_dif_tab\menu.h:1:0,
                 from C:\Users\Ghoond\Documents\Arduino\Tes_sensors_dif_tab\Tes_sensors_dif_tab.ino:7:
sensor.h:19:28: error: redefinition of 'int copyIndex'
 int analogBufferIndex = 0, copyIndex = 0;
                            ^~~~~~~~~
In file included from C:\Users\Ghoond\Documents\Arduino\Tes_sensors_dif_tab\Tes_sensors_dif_tab.ino:4:0:
C:\Users\Ghoond\Documents\Arduino\Tes_sensors_dif_tab\sensor.h:19:28: note: 'int copyIndex' previously defined here
 int analogBufferIndex = 0, copyIndex = 0;
                            ^~~~~~~~~
In file included from C:\Users\Ghoond\Documents\Arduino\Tes_sensors_dif_tab\menu.h:1:0,
                 from C:\Users\Ghoond\Documents\Arduino\Tes_sensors_dif_tab\Tes_sensors_dif_tab.ino:7:
sensor.h:20:7: error: redefinition of 'float averageVoltage'
 float averageVoltage = 0, tdsValue = 0, temperature = 25;
       ^~~~~~~~~~~~~~
In file included from C:\Users\Ghoond\Documents\Arduino\Tes_sensors_dif_tab\Tes_sensors_dif_tab.ino:4:0:
C:\Users\Ghoond\Documents\Arduino\Tes_sensors_dif_tab\sensor.h:20:7: note: 'float averageVoltage' previously defined here
 float averageVoltage = 0, tdsValue = 0, temperature = 25;
       ^~~~~~~~~~~~~~
In file included from C:\Users\Ghoond\Documents\Arduino\Tes_sensors_dif_tab\menu.h:1:0,
                 from C:\Users\Ghoond\Documents\Arduino\Tes_sensors_dif_tab\Tes_sensors_dif_tab.ino:7:
sensor.h:20:27: error: redefinition of 'float tdsValue'
 float averageVoltage = 0, tdsValue = 0, temperature = 25;
                           ^~~~~~~~
In file included from C:\Users\Ghoond\Documents\Arduino\Tes_sensors_dif_tab\Tes_sensors_dif_tab.ino:4:0:
C:\Users\Ghoond\Documents\Arduino\Tes_sensors_dif_tab\sensor.h:20:27: note: 'float tdsValue' previously defined here
 float averageVoltage = 0, tdsValue = 0, temperature = 25;
                           ^~~~~~~~
In file included from C:\Users\Ghoond\Documents\Arduino\Tes_sensors_dif_tab\menu.h:1:0,
                 from C:\Users\Ghoond\Documents\Arduino\Tes_sensors_dif_tab\Tes_sensors_dif_tab.ino:7:
sensor.h:20:41: error: redefinition of 'float temperature'
 float averageVoltage = 0, tdsValue = 0, temperature = 25;
                                         ^~~~~~~~~~~
In file included from C:\Users\Ghoond\Documents\Arduino\Tes_sensors_dif_tab\Tes_sensors_dif_tab.ino:4:0:
C:\Users\Ghoond\Documents\Arduino\Tes_sensors_dif_tab\sensor.h:20:41: note: 'float temperature' previously defined here
 float averageVoltage = 0, tdsValue = 0, temperature = 25;
                                         ^~~~~~~~~~~
In file included from C:\Users\Ghoond\Documents\Arduino\Tes_sensors_dif_tab\menu.h:1:0,
                 from C:\Users\Ghoond\Documents\Arduino\Tes_sensors_dif_tab\Tes_sensors_dif_tab.ino:7:
C:\Users\Ghoond\Documents\Arduino\Tes_sensors_dif_tab\sensor.h: In function 'int getMedianNum(int*, int)':
sensor.h:23:5: error: redefinition of 'int getMedianNum(int*, int)'
 int getMedianNum(int bArray[], int iFilterLen)
     ^~~~~~~~~~~~
In file included from C:\Users\Ghoond\Documents\Arduino\Tes_sensors_dif_tab\Tes_sensors_dif_tab.ino:4:0:
C:\Users\Ghoond\Documents\Arduino\Tes_sensors_dif_tab\sensor.h:23:5: note: 'int getMedianNum(int*, int)' previously defined here
 int getMedianNum(int bArray[], int iFilterLen)
     ^~~~~~~~~~~~
In file included from C:\Users\Ghoond\Documents\Arduino\Tes_sensors_dif_tab\menu.h:1:0,
                 from C:\Users\Ghoond\Documents\Arduino\Tes_sensors_dif_tab\Tes_sensors_dif_tab.ino:7:
C:\Users\Ghoond\Documents\Arduino\Tes_sensors_dif_tab\sensor.h: In function 'int sensor_tds()':
sensor.h:48:5: error: redefinition of 'int sensor_tds()'
 int sensor_tds() {
     ^~~~~~~~~~
In file included from C:\Users\Ghoond\Documents\Arduino\Tes_sensors_dif_tab\Tes_sensors_dif_tab.ino:4:0:
C:\Users\Ghoond\Documents\Arduino\Tes_sensors_dif_tab\sensor.h:48:5: note: 'int sensor_tds()' previously defined here
 int sensor_tds() {
     ^~~~~~~~~~
In file included from C:\Users\Ghoond\Documents\Arduino\Tes_sensors_dif_tab\menu.h:1:0,
                 from C:\Users\Ghoond\Documents\Arduino\Tes_sensors_dif_tab\Tes_sensors_dif_tab.ino:7:
C:\Users\Ghoond\Documents\Arduino\Tes_sensors_dif_tab\sensor.h: In function 'int sensor_ph()':
sensor.h:72:5: error: redefinition of 'int sensor_ph()'
 int sensor_ph() {
     ^~~~~~~~~
In file included from C:\Users\Ghoond\Documents\Arduino\Tes_sensors_dif_tab\Tes_sensors_dif_tab.ino:4:0:
C:\Users\Ghoond\Documents\Arduino\Tes_sensors_dif_tab\sensor.h:72:5: note: 'int sensor_ph()' previously defined here
 int sensor_ph() {
     ^~~~~~~~~
In file included from C:\Users\Ghoond\Documents\Arduino\Tes_sensors_dif_tab\menu.h:1:0,
                 from C:\Users\Ghoond\Documents\Arduino\Tes_sensors_dif_tab\Tes_sensors_dif_tab.ino:7:
sensor.h:77:44: error: lvalue required as left operand of assignment
   Po = 7.00 + ((PH7 - TeganganPh) / PH_step);
                                            ^
In file included from C:\Users\Ghoond\Documents\Arduino\Tes_sensors_dif_tab\Tes_sensors_dif_tab.ino:7:0:
C:\Users\Ghoond\Documents\Arduino\Tes_sensors_dif_tab\menu.h: At global scope:
menu.h:4:12: error: expected constructor, destructor, or type conversion before ';' token
 sensor_ph();
            ^
menu.h:5:13: error: expected constructor, destructor, or type conversion before ';' token
 sensor_tds();
             ^
exit status 1
lvalue required as left operand of assignment

but on the arduino IDE line

Po = 7.00 + ((PH7 - TeganganPh) / PH_step);

on function sensor_ph() are blocked red

Declare Po to be a legitimate variable.

previously i write const int Po = 0; then it say redefine Po

Lose the const.

it still say redefinition of 'int Po'

Then remove the other definition.

before the error there's no definition, but it say redefinition ph_pin and then the Po, then comes up the error lvalue required

i have try the codes under one file for testing reading ph meter and tds meter and it works

Glad the problem is solved, please mark the thread "Solved".

No, what i meant is the codes are works but outside of this main problem, the works one is only reading the sensor and the outputs are displayed at serial.

My main objective are the sensor output displayed at LCD, the sensors codes are same like the works one but when i want to implement it with LCD it has the error

The sensors code are like this

//=======PH===============
const int ph_pin = A0;
float Po = 0;
float PH_step;
int nilai_analog_PH;
double TeganganPh;

//untuk kalibrasi
float PH4 = 3.15;
float PH7 = 2.5;
//========================

//=========TDS============
#define TdsSensorPin A1
#define VREF 5
#define SCOUNT  30
int analogBuffer[SCOUNT];
int analogBufferTemp[SCOUNT];
int analogBufferIndex = 0,copyIndex = 0;
float averageVoltage = 0,tdsValue = 0,temperature = 25;
//========================

int analog_tds;
int analod_ph;

void setup() {
  pinMode(TdsSensorPin,INPUT);  
  pinMode(ph_pin,INPUT);

  Serial.begin(115200);
}

int sensor_tds(){
static unsigned long analogSampleTimepoint = millis();
   if(millis()-analogSampleTimepoint > 40U)
   {
     analogSampleTimepoint = millis();
     analogBuffer[analogBufferIndex] = analogRead(A1);
     analogBufferIndex++;
     if(analogBufferIndex == SCOUNT) 
         analogBufferIndex = 0;
   }   
   static unsigned long printTimepoint = millis();
   if(millis()-printTimepoint > 2000U)
   {
      printTimepoint = millis();
      for(copyIndex=0;copyIndex<SCOUNT;copyIndex++)
        analogBufferTemp[copyIndex]= analogBuffer[copyIndex];
      averageVoltage = getMedianNum(analogBufferTemp,SCOUNT) * (float)VREF / 1024.0;
      float compensationCoefficient=1.0+0.02*(temperature-25.0);
      float compensationVolatge=averageVoltage/compensationCoefficient;
      tdsValue=(133.42*compensationVolatge*compensationVolatge*compensationVolatge
      - 255.86*compensationVolatge*compensationVolatge + 857.39*compensationVolatge)*0.5;
      return analogRead(0);
   }
}
int getMedianNum(int bArray[], int iFilterLen) 
{
      int bTab[iFilterLen];
      for (byte i = 0; i<iFilterLen; i++)
      bTab[i] = bArray[i];
      int i, j, bTemp;
      for (j = 0; j < iFilterLen - 1; j++) 
      {
      for (i = 0; i < iFilterLen - j - 1; i++) 
          {
        if (bTab[i] > bTab[i + 1]) 
            {
        bTemp = bTab[i];
            bTab[i] = bTab[i + 1];
        bTab[i + 1] = bTemp;
         }
      }
      }
      if ((iFilterLen & 1) > 0)
    bTemp = bTab[(iFilterLen - 1) / 2];
      else
    bTemp = (bTab[iFilterLen / 2] + bTab[iFilterLen / 2 - 1]) / 2;
      return bTemp;
}

int sensor_ph(){
  nilai_analog_PH = analogRead(A0);
  Serial.print("Nilai ADC Ph: ");
  Serial.println(nilai_analog_PH);
  TeganganPh = 5 / 1024.0 * nilai_analog_PH;
  Serial.print("TeganganPh: ");
  Serial.println(TeganganPh, 3);

  PH_step = (PH4 - PH7) / 3;
  Po = 7.00 + ((PH7 - TeganganPh) / PH_step);
}

void loop() {
  sensor_tds();
  delay(200);
  sensor_ph();
  Serial.print("TDS : ");
  Serial.println(tdsValue);
  Serial.print("PH : ");
  Serial.println(Po);
}

Its basically the same but without the loop one

What error?

The same error, lvalue required as left operand of assignment

The problem is that PH4 and PH7 are already defined in some files included by the compiler

/home/test/.arduino15/packages/arduino/tools/avr-gcc/7.3.0-atmel3.6.1-arduino7/avr/include/avr/iomxx0_1.h:1113:0: note: this is the location of the previous definition
 # define PH4    4

Thats the problem the PH4 and the PH7 are not defined in any files, only declared in float at file sensors.h

Is the sensors code inside "sensors.h"? Don't put all that code inside a header file since it seems to be getting included multiple times (once from the main ino file and once through menu.h). Move it out of there and into a tab. That is one reason you'd get redefinition errors.

But they are defined in .arduino15/packages/arduino/tools/avr-gcc/7.3.0-atmel3.6.1-arduino7/avr/include/avr/iomxx0_1.h
< edit > They are used to specify the specific bits of PORT H on the Mega

1109 # define PORTH  _SFR_MEM8(0x102)
1110 # define PH7    7
1111 # define PH6    6
1112 # define PH5    5
1113 # define PH4    4
1114 # define PH3    3
1115 # define PH2    2
1116 # define PH1    1
1117 # define PH0    0

Best to avoid exclusively using capital letters in variable names.

1 Like

Yes the sensors code are inside sensor.h, i was trying to put the sensor reading to menu.h then it will be displayed to the main ino code. So am i have to only include it to menu.h?