SOLVED (Thanks) "if" and "els" with distance sensor and LCD display

Dear friends, What I would like to do: Getting distance (ultra sonic sensor) in cm on LCD (that part is OK)
I have somewhere and error in the last part (I think) of the code with "if" and "else" and I am really stuck with it. I just don't see it. I would like to read: When the distance from sensor to waterlevel is >40cm, on the LCD I would like to read "LEV LOW" (that part is OK.)
When the distance is <40 and >20 , it must be LEV MED (level medium). (that part is OK too)
And now the trouble part: When the distance is <20 I would like to read "LEV HIGH"
I only get the messages LOW and MED, never HIGH. I tried and I searched without results. So all my hope is on you guys. Thanks a lot for your help. It means a lot to me. Kind regards, Michel.

#include <LiquidCrystal.h>//Load Liquid Crystal Library

LiquidCrystal LCD(12, 11, 5, 4, 3, 2);  //Create Liquid Crystal Object called LCD
 
int trigPin=9; //Sensor Trip pin connected to Arduino pin 9
int echoPin=7;  //Sensor Echo pin connected to Arduino pin 7
int myCounter=0;  //declare your variable myCounter and set to 0
int servoControlPin=6; //Servo control line is connected to pin 6
float pingTime;  //time for ping to travel from the sensor to the target and return
float targetDistance; //Distance to Target in Centimeters
float speedOfSound=776.5; //Speed of sound in miles per hour
 
void setup() {
  
Serial.begin(9600);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
LCD.begin(16,2); //Tell Arduino to start your 16x2 LCD
LCD.setCursor(0,0);  //Set LCD cursor to upper left corner, column 0, row 0
LCD.print("Distance:");  //Print Message on First Row
}
 
void loop() {
  
  digitalWrite(trigPin, LOW); //Set trigger pin low
  delayMicroseconds(2000); //Let signal settle
  digitalWrite(trigPin, HIGH); //Set trigPin high
  delayMicroseconds(15); //Delay in high state
  digitalWrite(trigPin, LOW); //ping has now been sent
  delayMicroseconds(10); //Delay in high state
  
  pingTime = pulseIn(echoPin, HIGH);  //pingTime in microceconds
  pingTime=pingTime/1000000; //convert pingTime to seconds by dividing by 1000000 (microseconds in a second)
  pingTime=pingTime/3600; //convert pingtime to hours by dividing by 3600 (seconds in an hour)
  targetDistance= speedOfSound * pingTime;  //This will be in miles, since we declared the speed of sound as kilometers per hour; although we're going to convert it back to centimeters 
  targetDistance=targetDistance/2; //Remember ping travels to the target and back from the target, so you must divide by 2 for actual target distance.
  targetDistance= targetDistance*160934,4;    //Convert miles to centimeters by multipling by 160934,4 

 
  
  LCD.setCursor(0,1);  //Set the cursor to the first column of the second row
  LCD.print("                "); //Print blanks to clear the row
  LCD.setCursor(0,1);   //Set Cursor again to the first column of the second row
  LCD.print(targetDistance); //Print measured distance
  LCD.print(" cm ");  //Print your units

 if (targetDistance > 40) {
      LCD.print("LEV LOW ");       
  }
else if (targetDistance < 40 || targetDistance > 20) {
      LCD.print("LEV MED ");       
  }
else (targetDistance < 20); {
  LCD.print ("LEV HI ");
}
  
   


  
  
  delay(1250); //Pause to let things settle
  
}
#include <LiquidCrystal.h>//Load Liquid Crystal Library

LiquidCrystal LCD(12, 11, 5, 4, 3, 2);  //Create Liquid Crystal Object called LCD
 
int trigPin=9; //Sensor Trip pin connected to Arduino pin 9
int echoPin=7;  //Sensor Echo pin connected to Arduino pin 7
int myCounter=0;  //declare your variable myCounter and set to 0
int servoControlPin=6; //Servo control line is connected to pin 6
float pingTime;  //time for ping to travel from the sensor to the target and return
float targetDistance; //Distance to Target in Centimeters
float speedOfSound=776.5; //Speed of sound in miles per hour
 
void setup() {
  
Serial.begin(9600);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
LCD.begin(16,2); //Tell Arduino to start your 16x2 LCD
LCD.setCursor(0,0);  //Set LCD cursor to upper left corner, column 0, row 0
LCD.print("Distance:");  //Print Message on First Row
}
 
void loop() {
  
  digitalWrite(trigPin, LOW); //Set trigger pin low
  delayMicroseconds(2000); //Let signal settle
  digitalWrite(trigPin, HIGH); //Set trigPin high
  delayMicroseconds(15); //Delay in high state
  digitalWrite(trigPin, LOW); //ping has now been sent
  delayMicroseconds(10); //Delay in high state
  
  pingTime = pulseIn(echoPin, HIGH);  //pingTime in microceconds
  pingTime=pingTime/1000000; //convert pingTime to seconds by dividing by 1000000 (microseconds in a second)
  pingTime=pingTime/3600; //convert pingtime to hours by dividing by 3600 (seconds in an hour)
  targetDistance= speedOfSound * pingTime;  //This will be in miles, since we declared the speed of sound as kilometers per hour; although we're going to convert it back to centimeters 
  targetDistance=targetDistance/2; //Remember ping travels to the target and back from the target, so you must divide by 2 for actual target distance.
  targetDistance= targetDistance*160934,4;    //Convert miles to centimeters by multipling by 160934,4 

 
  
  LCD.setCursor(0,1);  //Set the cursor to the first column of the second row
  LCD.print("                "); //Print blanks to clear the row
  LCD.setCursor(0,1);   //Set Cursor again to the first column of the second row
  LCD.print(targetDistance); //Print measured distance
  LCD.print(" cm ");  //Print your units

 if (targetDistance > 40) {
      LCD.print("LEV LOW ");       
  }
else if (targetDistance < 40 || targetDistance > 20) {
      LCD.print("LEV MED ");       
  } 
else (targetDistance < 20); {  <<<<---- OH DEAR, NO
  LCD.print ("LEV HI ");
}
  
   


  
  
  delay(1250); //Pause to let things settle
  
}
float pingTime;  //time for ping to travel from the sensor to the target and return

pulseIn() does not return a float.

 targetDistance= targetDistance*160934,4;    //Convert miles to centimeters by multipling by 160934,4

The decimal point in C++ is ., not ,. The comma operator means something completely different.

else (targetDistance < 20); {

What is this supposed to be doing? That's not an else if statement, since the if is missing.

The (targetDistance < 20) part is a valid statement, but it is meaningless. The value might be less than 20, but so what?

Sorry, I did something wrong while posting. Please find the story, see my post 7.25pm

else if (targetDistance < 40 || targetDistance > 20) {

Maybe should be

else if (targetDistance < 40 && targetDistance > 20) {
else (targetDistance < 20); {

could be just

else {   // no ;

PaulS:

float pingTime;  //time for ping to travel from the sensor to the target and return

pulseIn() does not return a float.

 targetDistance= targetDistance*160934,4;    //Convert miles to centimeters by multipling by 160934,4

The decimal point in C++ is ., not ,. The comma operator means something completely different.

else (targetDistance < 20); {

What is this supposed to be doing? That's not an else if statement, since the if is missing.

The (targetDistance < 20) part is a valid statement, but it is meaningless. The value might be less than 20, but so what?

PaulS:

float pingTime;  //time for ping to travel from the sensor to the target and return

pulseIn() does not return a float.

 targetDistance= targetDistance*160934,4;    //Convert miles to centimeters by multipling by 160934,4

The decimal point in C++ is ., not ,. The comma operator means something completely different.

else (targetDistance < 20); {

What is this supposed to be doing? That's not an else if statement, since the if is missing.

The (targetDistance < 20) part is a valid statement, but it is meaningless. The value might be less than 20, but so what?

when distance less than 20cm I would like to read "LEV HIGH" on display.

PaulS:

float pingTime;  //time for ping to travel from the sensor to the target and return

pulseIn() does not return a float.

 targetDistance= targetDistance*160934,4;    //Convert miles to centimeters by multipling by 160934,4

The decimal point in C++ is ., not ,. The comma operator means something completely different.

else (targetDistance < 20); {

What is this supposed to be doing? That's not an else if statement, since the if is missing.

The (targetDistance < 20) part is a valid statement, but it is meaningless. The value might be less than 20, but so what?

PaulS:

float pingTime;  //time for ping to travel from the sensor to the target and return

pulseIn() does not return a float.

 targetDistance= targetDistance*160934,4;    //Convert miles to centimeters by multipling by 160934,4

The decimal point in C++ is ., not ,. The comma operator means something completely different.

else (targetDistance < 20); {

What is this supposed to be doing? That's not an else if statement, since the if is missing.

The (targetDistance < 20) part is a valid statement, but it is meaningless. The value might be less than 20, but so what?

I was hoping to read "LEV HI" see: else (targetDistance < 20); {
LCD.print ("LEV HI ");

else (targetDistance < 20);<- NO SEMICOLON HERE, EVER!

CtrlAltElite:
else (targetDistance < 20);<- NO SEMICOLON HERE, EVER!

I tried a lot of things, with and without dots etc....... Thank you for helping me. Can you please rewrite the last part of the code as it should be. Are there other things in the entire code to change?

Did you see post #4?

evanmars:

else if (targetDistance < 40 || targetDistance > 20) {

Maybe should be

else if (targetDistance < 40 && targetDistance > 20) {
else (targetDistance < 20); {

could be just

else {   // no ;

Yes, thank you. I have to check it out. Thanks a lot.

MichDragstar:
I tried a lot of things, with and without dots etc....... Thank you for helping me. Can you please rewrite the last part of the code as it should be. Are there other things in the entire code to change?

When I remove ; I get an error Arduino: 1.8.2 (Windows 7), Board:"Arduino/Genuino Uno"

C:\Users\Michel\Documents\Arduino\sketch_feb14a\sketch_feb14a.ino: In function 'void loop()':

sketch_feb14a:53: error: expected ';' before '{' token

else (targetDistance < 20) {

^

exit status 1
expected ';' before '{' token

Did you see the post that tells you to post all your code?

MichDragstar:
When I remove ; I get an error Arduino: 1.8.2 (Windows 7), Board:"Arduino/Genuino Uno"

C:\Users\Michel\Documents\Arduino\sketch_feb14a\sketch_feb14a.ino: In function 'void loop()':

sketch_feb14a:53: error: expected ';' before '{' token

else (targetDistance < 20) {

^

exit status 1
expected ';' before '{' token

I didn't, compiled fine for me with just

else {

MichDragstar:
Yes, thank you. I have to check it out. Thanks a lot.

evanmars:

else if (targetDistance < 40 || targetDistance > 20) {

Maybe should be

else if (targetDistance < 40 && targetDistance > 20) {
else (targetDistance < 20); {

could be just

else {   // no ;

(targetDistance < 40 && targetDistance > 20)
Thanks a lot, problem solved, you are right || must be && . Thanks friend, you made my day!!

CtrlAltElite:
Did you see the post that tells you to post all your code?

The complete code is above, but the problem is solved now. Thank you all for helping me so fast. Wish I could do something for you guys, but Arduino is still a bit new for me, sorry. Kind regards, Michel

MichDragstar:
The complete code is above, but the problem is solved now.

The complete code above has the || where there should be a &&, and the semicolon where the semicolon shouldn't be, so we can't see exactly what it is you are trying to compile when you got the error you said you had.

Just one more question: what part of the code makes it happen I read the distance with a ,
pe 43,23cm and not 43cm

CtrlAltElite:
The complete code above has the || where there should be a &&, and the semicolon where the semicolon shouldn't be, so we can't see exactly what it is you are trying to compile when you got the error you said you had.

This is how I have changed the code now, and it works fine.

 if (targetDistance > 40) {
      LCD.print("LEV LOW ");       
  }
else if (targetDistance < 40 && targetDistance > 20) {
      LCD.print("LEV MED ");       
  }
else (targetDistance < 20); {
  LCD.print ("LEV HI ");
}

the only question I have is why there is a , in the distance value. pe 43.23cm and not simply 43cm

I think your chances of repeatedly being able to measure to an accuracy of 1/10th of a millimetre are pretty remote - the wavelength of a 40kHz signal in air is something like 8.5mm.

Can I remove the , in the valua on the LCD ? And reading pe just 43cm and not 43,22