arduino moisture sensor values

hey guys i have made this program but im a little concerned because my values from my moisture sensor jump around quite a bit
and if the sensor values are wrong for to long it may keep the heaters on for too long which can cause a fire
i did use an if statment instead of while im just trying differnt ways
here is my code...

#include <LiquidCrystal.h>
LiquidCrystal lcd(7,8,9,10,11,12);
int sen = 5;
int hOn = 6;
int mot = 3;
void setup()
{
  Serial.begin(9600);
  lcd.begin(16,2);
  pinMode(hOn, OUTPUT);
  pinMode(sen, OUTPUT);
  pinMode(mot, OUTPUT);
  lcd.print("Start Up");
  lcd.setCursor(0,1);
  lcd.print("Complete");
}

void loop()
{
  digitalWrite(hOn, HIGH);
  digitalWrite(mot, HIGH);
  digitalWrite(sen, HIGH);
  analogRead(4);
  delay(5000);
  digitalWrite(sen, LOW);
 
  int val = analogRead(4);
  int good = 451;
  int bad = 450;
  lcd.clear();
   while(val <= bad)
  {
    lcd.clear();
    lcd.setCursor(0,0);
    lcd.print("Heaters On.");
    lcd.setCursor(0,1);
    lcd.print(val);
    lcd.setCursor(4,1);
    lcd.print("UpT:");
    lcd.print(millis()/1000);
    digitalWrite(hOn, LOW);
    Serial.print (val);
    Serial.println (" Moisture Level Is Bad ");
    digitalWrite(mot, LOW);
    delay(120000);
    digitalWrite(mot, HIGH);
    delay(300000);
    digitalWrite(mot, LOW);
    delay(120000);
    digitalWrite(mot, HIGH);
    delay(60000);
  }
  while(val >= good)
  {
    lcd.clear();
    lcd.setCursor(0,0);
    lcd.print("Heaters Off.");
    lcd.setCursor(0,1);
    lcd.print(val);
    lcd.setCursor(4,1);
    lcd.print("UpT:");
    lcd.print(millis()/1000);
    Serial.print (val);
    Serial.println (" All Is Good ");
    digitalWrite(hOn, HIGH);
    digitalWrite(mot, LOW);
    delay(120000);
    digitalWrite(mot, HIGH);
    delay(300000);
    digitalWrite(mot, LOW);
    delay(120000);
    digitalWrite(mot, HIGH);
    delay(60000);
  }
}

So are you having a problem with the code? Does it work?

In programming there is always more than one way to to do things. So if it's working than it works. If the reading is jumping around so much than add a delay before reading the analog pin. I doubt you need the system to check things multiple time a second.

yaene21 (author)  mpilchfamily3 years ago

and ive also got rid of the 2 if statments and just have and if and else statment

yaene21 (author)  mpilchfamily3 years ago

sorry i didnt word it right im having problems with the values because

how i have

if (val <= 450)
{
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Heaters On.");
lcd.setCursor(0,1);
lcd.print(val);
lcd.setCursor(4,1);
lcd.print("UpT:");
lcd.print(millis()/1000);
digitalWrite(hOn, LOW);
Serial.print (val);
Serial.println (" Moisture Level Is Bad ");
digitalWrite(mot, LOW);
delay(120000);
digitalWrite(mot, HIGH);
delay(300000);
digitalWrite(mot, LOW);
delay(120000);
digitalWrite(mot, HIGH);
delay(60000);

my program is takes its reading and it comes back with 357 and its saying heaters is of why is this when it is not greater than or equal to 450