My Arduino countdown timer will randomly start subtracting by tens when its set to ones why?

I made a little small scale hydroponics system controlled by my Arduino mega it's supposed to count down from 300 seconds (5 mins) and when it hits 0 activate a pump for one minute then reset and do the cycle all over again. But after a couple of cycles it will randomly start counting down by tens or hundreds. Here is the code:

#include <LiquidCrystal.h>
LiquidCrystal lcd(8, 9, 4, 5, 6, 7);
int pump = 53;
int time = 300;
void setup(){
  pinMode(pump, OUTPUT);
  lcd.begin(16,2);
  lcd.print("PLANT WATERER =D");
  delay(5000);
  lcd.clear();
}
void loop(){
   if(time == 0){
  lcd.clear();
  lcd.print("WATERING");
  digitalWrite(pump, HIGH);
  delay(60000);
  lcd.clear();
  lcd.print("FINISHED");
  delay(800);
  lcd.clear();
  digitalWrite(pump,LOW);
    (time = 300);
  }
(time --);
  delay(1000);
  lcd.setCursor(0,0);
  lcd.print("watering in:");
  lcd.setCursor(0,1);
  lcd.print(time);
 
}

Help would be greatly appreciated. thanks a bunch:
     Harry

sort by: active | newest | oldest

One problem may be that you have the time -- and time = 300 in parentheses. No need for that. Also moving the time-- to the end of that LCD update may help too.

Here is another way you may want to code it. I prefer to display things on the screen before delaying and before changing the count. Then you have the full 300 seconds displayed.

#include <LiquidCrystal.h>

LiquidCrystal lcd(8, 9, 4, 5, 6, 7);

int pump = 53;

int time = 300;

void setup()

{

pinMode(pump, OUTPUT);

lcd.begin(16,2);

lcd.print("PLANT WATERER =D");

delay(5000);

lcd.clear();

}

void loop()

{

if (time > 0)

{

lcd.setCursor(0,0);

lcd.print("watering in:");

lcd.setCursor(0,1);

lcd.print(time);

delay(1000);

time --;

}

else

{

lcd.clear();

lcd.print("WATERING");

digitalWrite(pump, HIGH);

delay(60000);

lcd.clear();

lcd.print("FINISHED");

delay(800);

lcd.clear();

digitalWrite(pump,LOW);

time = 300;

}

}

harry88 (author)  mpilchfamily3 years ago

It works!!! =D Thanks a bunch you were a huge help.

harry88 (author)  harry883 years ago

wait nope nevermind here is what its doing.

IMG_0867.MOV(516x290) 17 KB

Did you make changes to your original code like i suggested or did you use the code i posted?

During the countdown portion it would be a good idea to clear the screen before setting the cursor and printing anything on it. What your seeing may be the result of a miss alignment of characters as it prints over the existing characters.

harry88 (author)  mpilchfamily3 years ago

I used the code you posted. OK thanks I'll edit the code when i get a chance today.

harry88 (author)  harry883 years ago

OK now with one more "lcd.clear();" the program works great thanks a bunch to mpilchfamily, and steveastrouk for the help.

Does it screw up if the pump isn't connected?

harry88 (author)  steveastrouk3 years ago

It screws up either way I have the Arduino hooked to a mosfet hooked to the pump.