LCD shows distance as 0, while sensor does not

#include <LiquidCrystal.h>
#include <Servo.h>
Servo myservo;  
int pos = 0;  
const int rs = 12, en = 11, d4 = 5, d5 = 4, d6 = 3, d7 = 2;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);
const int trigPin1 = 9;
const int echoPin1 = 8;
const int sensorPin = A0; 
int sensorValue;  
int limit = 950; 
float moisture;
long duration;
long distance;
bool wet = false, obs = false;

void setup() 
{
  lcd.begin(16,2);
  lcd.print("Waste Segregator");
  myservo.attach(10);
  pinMode(trigPin1, OUTPUT);
  pinMode(echoPin1, INPUT);
}

bool obstacle(int trigPin1, int echoPin1, String s1){
  
    digitalWrite(trigPin1, HIGH);
    delayMicroseconds(1000);
    digitalWrite(trigPin1, LOW);
    duration = pulseIn(echoPin1, HIGH);
    distance = duration*0.034/2;
    Serial.print(distance);
    Serial.println("CM");
    delay(10);
    
    if(distance<=12) return true;
    else return false;
  }

bool wetSense(){
 
       sensorValue = analogRead(sensorPin); 
       moisture = (100 - (sensorValue/1023.0)*100);
       Serial.println("Analog Value : ");
       Serial.println(sensorValue);
       Serial.println("Moisture : ");
       Serial.print(moisture);
       Serial.println("%");
       if(sensorValue < limit){
          return true;
       }
       delay(500);
       return false;
}

void display(String s){
  delay(1000);
  lcd.clear();
  lcd.print(s);
  delay(2000);
}

void loop() {
  myservo.write(pos);
  
  Serial.begin(9600);
  
  display("Monitoring...");
  
  lcd.clear();
  lcd.print("Distance: ");
  lcd.setCursor(0, 1);
  lcd.print(distance);
  lcd.print(" CM");
  
  delay(2000);
  
  lcd.clear();
  lcd.print("Moisture: ");
  lcd.setCursor(0, 1);
  lcd.print(moisture);
  lcd.print(" %");
  
   if(obstacle(trigPin1, echoPin1,"one") == true){
      delay(3000);
      obs = obstacle;
      delay(200);
      if (obs==true){
      
      wet = (wetSense());
   }
 }
      if(obs==true && wet==true){
        
        myservo.write(180);
        display("Wet Waste");
      }
      else if(obs==true && wet==false){
        myservo.write(90);
        display("Dry Waste");
      }
      else {
        myservo.write(pos);
      }
      delay(2000);
}

This is the code. I don't know what to do anymore. I tried everything that I could think of. LCD doesn't match sensors. What seems to be the problem?

Does distance always show 0 on LCD? If you wait 5 to 10 seconds, is it still zero?

What value do you see on serial monitor?

It only shows zero at first. Afterwards, it has the same distance that the sensor and serial monitor shows.

Forgive me if I am missing something; I've been up all night and decided to take one last look at the forums after taking my night meds.

I"m betting that if you change

lcd.print(distance);

To

lcd.print(distance);
Serial.print(distance);

You'll see a 0 in the serial output as well. I'm not seeing where you assign a value to distance in the beginning of the void loop, and that's where you are displaying the distance.

You're right. 0 showed up in the serial monitor.

Yes, that's what your code tells the Arduino to do.

The distance variable is zero initially. Then your code prints it to the lcd. After that, your code calls the function obstacle() which calculates a new value for distance and prints it to serial monitor. After all your delay(), of which there are many, distance is printed to the LCD again and now it is no longer zero.

Oh. That made so much sense. How can I fix it?

You'll need to copy the code that calculates the distance and paste it in the void loop before you display distance.

Edit: I'd like to add that I'm a relative beginner as well, so there may be a better way to handle this.

It worked! Thank you so much. Also I'm a beginner too.

No, copying code is not a good idea. If your code contains the same code more than once, or even code that is almost the same more than once, then you need to re-structure it. It's almost always true to say that less code is better than more code, and copying code means more code which could be shorter and better.

Oops

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.