Proper If Statement for 2 Different Sensors

Hello,

I am playing around with 2 different sensors, one being a temp/humidity and the other a water sensor. I can get the temp and humidity to display just fine, and I can get the water sensor "Water Detected" to work without the other code. What I am trying to achieve is having the temp and humidity displayed, but when water is detected to display "Water Detected" until the water goes away. Any help is appreciated.

#include <dht.h>
#include <LiquidCrystal.h>

LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

dht DHT;

#define TH 7
#define WS 8
int ws_value;

void setup(){
    Serial.begin(9600);
    lcd.clear();
    lcd.begin(16,2);
    pinMode(WS, INPUT);
}

void loop()
{
  ws_value = analogRead(WS);
  if (ws_value<=15) 
  {
    lcd.clear();
    lcd.setCursor(0,0);
    lcd.print("Water Detected!");
    delay(500);
  }
  else
  {
    int chk = DHT.read11(TH);
    delay(500);
    lcd.setCursor(0,0); 
    lcd.print("Temp: ");
    lcd.print(DHT.temperature);
    lcd.print((char)223);
    lcd.print("C");
    lcd.setCursor(0,1);
    lcd.print("Humidity: ");
    lcd.print(DHT.humidity);
    lcd.print("%");
    delay(1000);
  }
}

Analog read on pin 8? What board are you using?

Yes Pin 8.

Its a Freenove Black Board (basically an Elegoo UNO R3).

Have you run any test sketches to just read analog values so you can inspect them?

By the way, if there are any unusual details like using an uncommon board, you should say something about that in your very first post.

Yes. If I run just the temp/hum sensor it works just fine. If I run the water sensor code it works just fine. But combined, I don't think my if statement is correct.

why do you conditionally either display water or humidity info?

why not read and display both

gcjr:
why do you conditionally either display water or humidity info?

why not read and display both

  1. The LCD only has "two lines" it can display at a time, so there isn't enough room for all the data
  2. The water info isn't important until water is detected. Such as a flooded mechanical room, but having the temp and humidity display at all times is more important.

What is happening when you run the code?
Without knowing what is happening we can't really help.
The first thing I note in your code is you do not clear the display before sending the Temp/Humidity info, this can lead to scrambled characters if the the new message doesn't over write the "water detected" message completely.

The only thing that is displayed is the Temp and Humidity, if I apply water to the sensor, nothing is happening.

why don't you print the water measurement on the Serial monitor to verify that it works correctly

My thoughts exactly, in fact set up serial print for all the information and see whether the issue is with the LCD not clearing properly between displays, I would also slow it down further by delaying for at least 5 seconds per step. (I know delay should not be used in most cases, but I don't think it is an issue here).

gritpit:
The only thing that is displayed is the Temp and Humidity, if I apply water to the sensor, nothing is happening.

Did you read reply #3?

aarg:
Did you read reply #3?

Yes I responded, but didn't quote. My response was #4.

gritpit:
Yes I responded, but didn't quote. My response was #4.

But all you said was, "works real fine". That doesn't tell me much. I'd like to know what actual ADC input values you are getting. I have no visibility to the other ("water sensor is working") program either, since you didn't post it.

Kiwi_Bloke:
My thoughts exactly, in fact set up serial print for all the information and see whether the issue is with the LCD not clearing properly between displays, I would also slow it down further by delaying for at least 5 seconds per step. (I know delay should not be used in most cases, but I don't think it is an issue here).

I ended up modifying my code a bit, and adding in the 5 second delay and got it to work the way I was intending.

#include <dht.h>
#include <LiquidCrystal.h>

LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

dht DHT;

#define TH 7


void setup()
{
  Serial.begin(9600);
  lcd.begin(16,2);
}

void loop()
{
  int ws_sensor=analogRead(A1);
  Serial.println(ws_sensor);
  if (ws_sensor < 15) 
  {
    int chk = DHT.read11(TH);
    delay(5000);
    lcd.setCursor(0,0);
    lcd.clear(); 
    lcd.print("Temp: ");
    lcd.print(DHT.temperature);
    lcd.print((char)223);
    lcd.print("C");
    lcd.setCursor(0,1);
    lcd.print("Humidity: ");
    lcd.print(DHT.humidity);
    lcd.print("%");
  }
  else if (ws_sensor > 15)
  {
    lcd.setCursor(0,0);
    lcd.clear();
    lcd.print("Water Detected!");
    delay(5000);
  }
}

aarg:
But all you said was, "works real fine". That doesn't tell me much. I'd like to know what actual ADC input values you are getting. I have no visibility to the other ("water sensor is working") program either, since you didn't post it.

I'll communicate better next time. This was my first post. I apologize.

void setup(){
Serial.begin(9600);     // Communication started with 9600 baud
}
void loop(){
int sensor=analogRead(A1); // Incoming analog signal read and appointed sensor
Serial.println(sensor);   //Wrote serial port
}

...and the printed values are... ?

Can you just tell us what a typical value is printed when the sensor is A) wet, B) dry.

Also, this sketch appears to be written for a different board because it uses A0 instead of pin 8. Please explain. To be useful for troubleshooting, you would definitely have to use the same board!

The value can change from 15 - 400+ depending on the amount of water it is exposed to, so it is reporting correctly.

If you look at post #14 you'll see that I modified my code. I moved the sensor from 8 to A1.