Water level and relay code!

I finished my code and everything work great
but i have problem with my relay,

digitalWrite(Relay, HIGH);  // turn on relay
   if (95)-(distance) < (35);
   digitalWrite(Relay, LOW);   // turn off relay
   if (95)-(distance) = (70);

as you can see i want the relay ON when (95)-(distance) < (35)
& OFF when (95)-(distance) = (70)

I have this error

water.ino: In function ‘void loop()’:
water:78: error: lvalue required as left operand of assignment
lvalue required as left operand of assignment

and this is my full code

#include <LiquidCrystal.h>
#define ECHOPIN 3                            // Pin to receive echo pulse
#define TRIGPIN 4                            // Pin to send trigger pulse
#define STATUSPIN 13                        // Use for troubleshooting
int Relay = 6;
int highWater = 30;        // These values allow to calculate % of full
int lowWater = 100;        // SRF04 hangs above water (lower distance = more water)
byte symbol[8] = {         // Custom character for LCD display
  B00000, 
  B11111,
  B11111,
  B11111,
  B11111,
  B11111,
  B00000,
};
byte eq[8] = { 
        B00000,
        B00000,
        B01001,
        B10110,
        B00000,
        B01001,
        B10110,
        B00000
};
byte h[8] = { 
        B00100,
        B01110,
        B10101,
        B00100,
        B00100,
        B10101,
        B01110,
        B00100
};
LiquidCrystal lcd(7, 8, 9, 10, 11, 12);    // Assign pins

// Utility function for flashing STATUSPIN
void flashLed(int pin, int times, int wait) {

  for (int i = 0; i < times; i++) {
    digitalWrite(pin, HIGH);
    delay(wait);
    digitalWrite(pin, LOW);
    if (i + 1 < times) {
      delay(wait);
    }
  }
}

void setup() {
  
  lcd.begin(16,2);
  lcd.print("   AQUA LEVEL ");
  lcd.createChar(0, symbol);
  lcd.createChar(1, eq);
  lcd.createChar(2, h);
  pinMode(ECHOPIN, INPUT);
  pinMode(TRIGPIN, OUTPUT);
  pinMode(Relay, OUTPUT); 
  delay(3000);       // Show application name for 3 seconds.
}

void loop() {
  // Measure distance
  digitalWrite(TRIGPIN, LOW);                   // Set the trigger pin to low for 2uS
  delayMicroseconds(2);
  digitalWrite(TRIGPIN, HIGH);                  // Send a 10uS high to trigger ranging
  delayMicroseconds(10);
  digitalWrite(TRIGPIN, LOW);                   // Send pin low again
  int distance = pulseIn(ECHOPIN, HIGH);        // Read in times pulse
  distance= distance/58;                        // divide by 58 gives cm.
  
   digitalWrite(Relay, HIGH);  // turn on relay
   if (95)-(distance) < (35);
   digitalWrite(Relay, LOW);   // turn off relay
   if (95)-(distance) = (70);

  // Convert measured value to value between 0-16, to display on LCD
  // Use Arduino built-in map and constrain functions
  int scaledValue = map(constrain(distance, highWater, lowWater), lowWater, highWater, 0, 16);
  lcd.clear();
  lcd.setCursor(0,0);
  while (scaledValue > 0) {
     lcd.print((char)0);
      scaledValue--;

}
lcd.setCursor(0, 15);
lcd.print("%");
    
// CM 
int cm;
cm = (95)-(distance);
lcd.setCursor(0, 1); 
lcd.print(( char)2);
lcd.print(cm);

lcd.print(" CM   ");

// litres  
int litres;
litres = 20 * (95 - distance);
lcd.print(( char)1);
lcd.print(litres);
lcd.print(" L");

//send pulse every 3 second 
   delay(9000);
}

if (95)-(distance) = (70);

Do you mean

if ((95 - distance) == 70)

A comparison requires ==.
And remove the ;

if (95)-(distance) = (70);

// Convert measured …

Is this supposed to happen if above true?
If so remove the ; and use {} around the code to be done and add ()

if (95-distance) == 70)
{…

You do not need brackets around each value.

Weedpharma

Edited when noted more problems!

…and the if command should continue because the “;” ends it.

   if((95-distance) < 35) // ; <- this
      digitalWrite(Relay, LOW);   // turn off relay

:roll_eyes: nothing work for now i try everything

Well, let's see the changes you made, then.

Post your new code and give a full description of what is happening.

Weedpharma

You’re still doing it:

 else if (95)-(distance) <=70) {

sorry wait i upload the wrong code :confused:
UPDATE
now it’s work

#include <LiquidCrystal.h>
#define ECHOPIN 3                            // Pin to receive echo pulse
#define TRIGPIN 4                            // Pin to send trigger pulse
#define STATUSPIN 13                        // Use for troubleshooting
int Relay = 6;
int highWater = 30;        // These values allow to calculate % of full
int lowWater = 100;        // SRF04 hangs above water (lower distance = more water)
byte symbol[8] = {         // Custom character for LCD display
  B00000, 
  B11111,
  B11111,
  B11111,
  B11111,
  B11111,
  B00000,
};
byte eq[8] = { 
        B00000,
        B00000,
        B01001,
        B10110,
        B00000,
        B01001,
        B10110,
        B00000
};
byte h[8] = { 
        B00100,
        B01110,
        B10101,
        B00100,
        B00100,
        B10101,
        B01110,
        B00100
};
LiquidCrystal lcd(7, 8, 9, 10, 11, 12);    // Assign pins

// Utility function for flashing STATUSPIN
void flashLed(int pin, int times, int wait) {

  for (int i = 0; i < times; i++) {
    digitalWrite(pin, HIGH);
    delay(wait);
    digitalWrite(pin, LOW);
    if (i + 1 < times) {
      delay(wait);
    }
  }
}

void setup() {
  
  lcd.begin(16,2);
  lcd.print("   AQUA LEVEL ");
  lcd.createChar(0, symbol);
  lcd.createChar(1, eq);
  lcd.createChar(2, h);
  pinMode(ECHOPIN, INPUT);
  pinMode(TRIGPIN, OUTPUT);
  pinMode(Relay, OUTPUT); 
  delay(3000);       // Show application name for 3 seconds.
}

void loop() {
  // Measure distance
  digitalWrite(TRIGPIN, LOW);                   // Set the trigger pin to low for 2uS
  delayMicroseconds(2);
  digitalWrite(TRIGPIN, HIGH);                  // Send a 10uS high to trigger ranging
  delayMicroseconds(10);
  digitalWrite(TRIGPIN, LOW);                   // Send pin low again
  int distance = pulseIn(ECHOPIN, HIGH);        // Read in times pulse
  distance= distance/58;                        // divide by 58 gives cm.
  
  
   
  
 if (95-distance <= 35) {
   digitalWrite (6,HIGH);
 }
 else if (95-distance >= 70) {
   digitalWrite (6,LOW);
 }
  
  
  
  // Convert measured value to value between 0-16, to display on LCD
  // Use Arduino built-in map and constrain functions
  int scaledValue = map(constrain(distance, highWater, lowWater), lowWater, highWater, 0, 16);
  lcd.clear();
  lcd.setCursor(0,0);
  while (scaledValue > 0) {
     lcd.print((char)0);
      scaledValue--;

}
lcd.setCursor(0, 15);
lcd.print("%");
    
// CM 
int cm;
cm = (95)-(distance);
lcd.setCursor(0, 1); 
lcd.print(( char)2);
lcd.print(cm);

lcd.print(" CM   ");

// litres  
int litres;
litres = 20 * (95 - distance);
lcd.print(( char)1);
lcd.print(litres);
lcd.print(" L");

//send pulse every 3 second 
   delay(9000);
}

thanks everyone :wink:

Don't edit your old post!

all the problem was from “IF” i used in the second line “else if” and it’s work now.

if (95-distance <= 35) {
   digitalWrite (6,HIGH);
 }
 else if (95-distance >= 70) {
   digitalWrite (6,LOW);
 }

+1 for everyone here, thanks :slight_smile: