[Solved] Help with START and RESET counterdown timer

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);
  
}

It's a stupid idea to use String objects as state variables. The time handling is way, way too complex and convoluted. Decide what you want to do, throw this code in the garbage can and design your own.

Show us your wiring diagram, because of your trouble with the switches. They might be wired in reverse logic polarity.

Is 'stoppin' the same as the RESET function you mentioned? I see no "reset" button in the code...

A flock of seagulls. Please use auto format in the IDE to format your code blocks in a consistent way before the next code post you make.

Hi,
I made some changes to your code and corrected the line:
stopwatch == "stop"; for stopwatch = "stop";
== is comparative.

Test and tell the result.

#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;
bool stopwatch = true;
bool action = false;
bool onstatus = false;
long initialsecond = 0;
long countdown_time = 0;
//long countdowntime_seconds = 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() {
  if (stopwatch == false) {
    action = true;
    //onstatus = 0;
  }
  if (digitalRead(stoppin) == HIGH) {
    action = false;
    stopwatch = true;
    countdown_time = 0;
    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 == true) {
    lcd.clear();
    action = true;
    stopwatch = false;
    initialsecond = 0;
    onstatus = true;
    long   hour = 0;
    long minute = 0;
    long second = 10;
    countdown_time = (hour * 3600) + (minute * 60) + second;
  }
  if (action  == true) {
    if (initialsecond == 0) {
      initialsecond = millis() / 1000;
    }
    long countdowntime_seconds = countdown_time - (millis() / 1000) + initialsecond;
    if (countdowntime_seconds >= 0 && onstatus == true) {
      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 == true ) {
        lcd.clear();
        delay(500);
        lcd.setCursor(3, 0);
        lcd.print("WORK DONE");
        lcd.setCursor(3, 1);
        lcd.print("PRESS START");
        stopwatch = true;
      }
    }
  }
  //delay(500);
}
1 Like

Thanks you so much :grin:, it works perfectly.

Hi,
if your problem was solved, do a kindness to everyone on the forum, especially to those who helped you. Write [Solved] before your topic title, so if someone searches and finds your topic, they'll know what the solution looks like.

And if it was solved by some help, please tick the one that best describes your solution.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.