Hello, I just began learning arduino and I just want to make a simple count down timer with a single push button with a start/restart function. I successfully managed to start and restart the timer and display it to the lcd but after the timer completes, it automatically restarts and begins to count down. I do not currently know what is causing the loop, I tried changing my count value after completion but it doesn't help.
Here is my code. Thank you.
#include <LiquidCrystal.h>
#include "Countimer.h"
Countimer tDown;
Countimer tNone;
LiquidCrystal lcd(2, 3, 4, 5, 6, 7);
int switchPin = 8;
int ledPin = 13;
int count = 0;
boolean lastButton = LOW;
boolean ledon = false;
boolean currentbutton = LOW;
void setup()
{
Serial.begin(9600);
pinMode (switchPin, INPUT);
pinMode (ledPin, OUTPUT);
// Count-down timer with 21s
tDown.setCounter(0, 0, 10, tDown.COUNT_DOWN, tDownComplete);
// Call print_time2() method every 1s.
tDown.setInterval(print_time2, 1000);
// No counter
// Just call print_none() method every 2s.
// tNone.setInterval(print_none, 2000);
}
boolean debounce(boolean last)
{
boolean current = digitalRead(switchPin);
if (last != current)
{
delay (5);
current = digitalRead(switchPin);
}
return current;
}
void loop()
{
tDown.run();
// tNone.run();
currentbutton = debounce(lastButton);
if (lastButton == LOW && currentbutton == HIGH)
{
ledon = !ledon;
}
lastButton = currentbutton;
if (ledon == true)
{
count = 1;
}
else
{
count = 0;
}
switch(count)
{
case 0:
tDown.stop();
// lcd.clear();
print_time2();
// tNone.stop();
break;
case 1:
tDown.start();
// tNone.start();
break;
default:
break;
}
}
void print_time2()
{
Serial.print("tDown: ");
Serial.println(tDown.getCurrentTime());
lcd.clear();
lcd.print("Time: ");
lcd.print(tDown.getCurrentTime());
}
//
//void print_none()
//{
// Serial.print("tNone: millis(): ");
// Serial.println(millis());
//}
void tDownComplete()
{
lcd.clear();
lcd.print("COMPLETE!!");
Serial.println(count);
}