Go Down

Topic: SOLVED (Thanks) "if" and "els" with distance sensor and LCD display (Read 636 times) previous topic - next topic

MichDragstar

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.

Code: [Select]
#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
  
}
  

CtrlAltElite

Code: [Select]
#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
 
}
 
Has there been an outbreak of the stupid virus, and I didn't get the memo?

PaulS

Code: [Select]
float pingTime;  //time for ping to travel from the sensor to the target and return

pulseIn() does not return a float.

Code: [Select]
 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.

Code: [Select]
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?
The art of getting good answers lies in asking good questions.

MichDragstar

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

evanmars

#4
Feb 15, 2018, 08:39 pm Last Edit: Feb 15, 2018, 08:41 pm by evanmars
Code: [Select]
else if (targetDistance < 40 || targetDistance > 20) {

Maybe should be
Code: [Select]
else if (targetDistance < 40 && targetDistance > 20) {

Code: [Select]
else (targetDistance < 20); {

could be just
Code: [Select]
else {   // no ;

MichDragstar

Code: [Select]
float pingTime;  //time for ping to travel from the sensor to the target and return

pulseIn() does not return a float.

Code: [Select]
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.

Code: [Select]
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?
Code: [Select]
float pingTime;  //time for ping to travel from the sensor to the target and return

pulseIn() does not return a float.

Code: [Select]
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.

Code: [Select]
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.
Code: [Select]
float pingTime;  //time for ping to travel from the sensor to the target and return

pulseIn() does not return a float.

Code: [Select]
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.

Code: [Select]
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?
Code: [Select]
float pingTime;  //time for ping to travel from the sensor to the target and return

pulseIn() does not return a float.

Code: [Select]
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.

Code: [Select]
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 ");

CtrlAltElite

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

Has there been an outbreak of the stupid virus, and I didn't get the memo?

MichDragstar

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?

evanmars


MichDragstar

Code: [Select]
else if (targetDistance < 40 || targetDistance > 20) {

Maybe should be
Code: [Select]
else if (targetDistance < 40 && targetDistance > 20) {

Code: [Select]
else (targetDistance < 20); {

could be just
Code: [Select]
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



CtrlAltElite

Did you see the post that tells you to post all your code?
Has there been an outbreak of the stupid virus, and I didn't get the memo?

evanmars

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
Code: [Select]
else {

MichDragstar

Yes, thank you. I have to check it out. Thanks a lot.
Code: [Select]
else if (targetDistance < 40 || targetDistance > 20) {

Maybe should be
Code: [Select]
else if (targetDistance < 40 && targetDistance > 20) {

Code: [Select]
else (targetDistance < 20); {

could be just
Code: [Select]
else {   // no ;
(targetDistance < 40 && targetDistance > 20)
Thanks a lot, problem solved, you are right || must be && . Thanks friend, you made my day!!

MichDragstar

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

Go Up