Hi, i'm helping a friend with a proyect that need a counterdown timer but i'm having issues with the code, first i founded a code for the counterdown in the website mechatrofice.com, so i added two push buttons, one for the START and other for the RESET. When you turn on the arduino the first message in the LCD is "COUNTERDOWN HH:MM:SS" then when you press START, it starts to counterdown, when it's finished, in the LCD shows "WORK DONE, PRESS START", so you have to press again start so the counter starts, if you press start and you need to reset the counterdown, you press the RESET button, then the LCD shows "COUNTERDOWN STOPPED, PRESS START", so you always have to press start to initiate the counterdown. The problems that i'm having are:
-When you press START , the LCD shows the message "WORK DONE".
-The counterdown starts when you turn on the arduino.
- Can't get a good result with the RESET button.
So that's how the Code must work and the problems that i'm having, so if anyone can help, i'll grateful.
#include <LiquidCrystal.h>
//Crear el objeto LCD con los números correspondientes (rs, en, d4, d5, d6, d7)
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
int indication = 10; //Connect LED or Buzzer to digital pin 10
const int startpin= 7, stoppin = 6;
String stopwatch = "stop", start_pause = "low";
String action;
int onstatus = 0;
long initialsecond = 0;
long countdown_time = 0;
void setup() {
pinMode(indication, OUTPUT);
pinMode(startpin, INPUT);
pinMode(stoppin, INPUT);
lcd.begin(16, 2);
lcd.setCursor(1, 0);
lcd.print("COUNTERDOWN");
lcd.setCursor(4, 1);
lcd.print("HH:MM:SS");
Serial.begin(9600);
}
void loop(){
long ontime = 0;
if (stopwatch == "run"){
action = "start_countdown";
onstatus = 0;
}
if (digitalRead(stoppin) == HIGH){
action = "stop_countdown";
stopwatch == "stop";
lcd.clear();
delay(500);
lcd.setCursor(0, 0);
lcd.print("COUNTDOWN");
lcd.setCursor(3, 1);
lcd.print("STOPPED");
delay(1000);
lcd.clear();
lcd.setCursor(1, 1);
lcd.print("PRESS START");
}
if (digitalRead(startpin) == HIGH && stopwatch == "stop"){
lcd.clear();
action = "start_countdown";
stopwatch = "run";
initialsecond = 0;
onstatus = 1;
long hour = 0;
long minute = 0;
long second = 10;
long countdown_time = (hour*3600) + (minute * 60) + second;
}
if (action == "start_countdown") {
if (initialsecond == 0) {
initialsecond = millis() / 1000;
}
long countdowntime_seconds = countdown_time - (millis() / 1000) + initialsecond;
if (countdowntime_seconds >= 0 && onstatus == 1) {
long countdown_hour = countdowntime_seconds / 3600;
long countdown_minute = ((countdowntime_seconds / 60) % 60);
long countdown_sec = countdowntime_seconds % 60;
lcd.setCursor(4, 1);
if (countdown_hour < 10) {
lcd.print("0");
}
lcd.print(countdown_hour);
lcd.print(":");
if (countdown_minute < 10) {
lcd.print("0");
}
lcd.print(countdown_minute);
lcd.print(":");
if (countdown_sec < 10) {
lcd.print("0");
}
lcd.print(countdown_sec);
Serial.println(countdowntime_seconds);
if (countdowntime_seconds == 0 && action == "start_countdown" ) {
lcd.clear();
delay(500);
lcd.setCursor(3, 0);
lcd.print("WORK DONE");
lcd.setCursor(3, 1);
lcd.print("PRESS START");
}
}
}
delay(500);
}