user31222: #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");  
    }
}


user19892: 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. 

31222: <pre><code>#include &lt;LiquidCrystal.h&gt;
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 &amp;&amp; digitalRead(distance) &gt;=20  )
    {
        lcd.clear();  
        lcd.print("water_came");  
        lcd.setCursor(0, 1);
    }
    if( digitalRead(Grove_Water_Sensor) == LOW &amp;&amp; digitalRead(distance) &lt;=8 )
    {
        lcd.clear();  
        lcd.write("Tank_is_full");
    }
    if( digitalRead(Grove_Water_Sensor) == HIGH &amp;&amp; digitalRead(distance) &gt;=20 )
    {
        lcd.clear();
        lcd.print("Fill The Tank PLEASE");  
    }
    if( digitalRead(Grove_Water_Sensor) == HIGH &amp;&amp; digitalRead(distance) &lt;=8 )
    {
        lcd.clear();
        lcd.print("Tank_is_FULL");  
    }
}
</code></pre>
 19892: <p>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. </p>
