0
#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
#define trigpinyellow 10
#define echopingreen 9
#define Grove_Water_Sensor 8
void setup() 
{
    Serial.begin(9600);
    pinMode(trigpinyellow,OUTPUT);
    pinMode(echopingreen,INPUT);
    pinMode(Grove_Water_Sensor, INPUT);
    lcd.begin(16, 2);
}

void loop() 
{
    long duration,distance;
    digitalWrite(trigpinyellow,LOW);
    delayMicroseconds(1000);
    digitalWrite(trigpinyellow,HIGH);
    delayMicroseconds(1000);
    digitalWrite(trigpinyellow,LOW);
    duration=pulseIn(echopingreen,HIGH);
    distance = (duration/2) / 29.1;
    Serial.println(distance);

    if( digitalRead(Grove_Water_Sensor) == LOW && digitalRead(distance) >=20  )
    {
        lcd.clear();  
        lcd.print("water_came");  
        lcd.setCursor(0, 1);
    }
    if( digitalRead(Grove_Water_Sensor) == LOW && digitalRead(distance) <=8 )
    {
        lcd.clear();  
        lcd.write("Tank_is_full");
    }
    if( digitalRead(Grove_Water_Sensor) == HIGH && digitalRead(distance) >=20 )
    {
        lcd.clear();
        lcd.print("Fill The Tank PLEASE");  
    }
    if( digitalRead(Grove_Water_Sensor) == HIGH && digitalRead(distance) <=8 )
    {
        lcd.clear();
        lcd.print("Tank_is_FULL");  
    }
}
8
  • 2
    You might want to change digitalRead(distance) >=20 to just distance >=20
    – Gerben
    Commented Feb 22, 2017 at 19:10
  • Your collection of if statements has holes which could result in no output to the lcd. For example, if Grove_Water_Sensor is LOW and distance is 10 would result in no output.
    – 6v6gt
    Commented Feb 22, 2017 at 19:17
  • I will try it Gerben, 6v6gt and thanks for the advice😃
    – user31225
    Commented Feb 22, 2017 at 19:54
  • But why it will not show any output
    – user31225
    Commented Feb 22, 2017 at 20:01
  • Voted down. Please add more content, what is not working? Commented Feb 23, 2017 at 1:50

1 Answer 1

1

Every time you instantiate digitalread(), you poll the pin. Here, you're polling pins 8 times every loop. Instead, poll the two pins at the start of the loop to update the variable, and then do your math and make your decisions afterward. Right now you're doing math with your distance variable, and then overwriting the variable during your if conditionals. Not sure if that's what you intend. You're also creating the variables duration and distance every single loop. instead, create those variables globally and just update them every loop.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.