1

I found this code for a kitchen timer, it was the closest thing to a countdown. The code is here:


#include <LiquidCrystal.h>

const int rs = 12, en = 11, d4 = 5, d5 = 4, d6 = 3, d7 = 2;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);

signed short minutes, secondes;
char timeline[16];

void setup() {

  lcd.begin(16, 2);
  lcd.print("Chronometre :");


}

void loop() {

  lcd.setCursor(0, 1);
  sprintf(timeline,"%0.2d mins %0.2d secs", minutes, secondes);
  lcd.print(timeline);
  
  delay(1000);
  secondes++;
  
  if (secondes == 60)
  {
    secondes = 0;
    minutes ++;
  }

}

How do I convert it properly for it to countdown from 1 minute and 30 seconds?

10
  • How about initializing the time variables secondes and minutes with the values for 1:30 and then instead of incrementing them decrementing? And of course checking for seconds to be below zero for decrementing the minutes. Have you tried that?
    – chrisl
    Commented Mar 25, 2021 at 19:53
  • I didnt know how to initailize that. Could you offer some code? Commented Mar 25, 2021 at 20:06
  • 4
    You should really do some basic C/C++ and Arduino tutorials, when you want to do Arduino. Otherwise you will not have much fun. Also we are not a free coding service. Look at the code where the mentioned variables are defined. Give each a fitting value by writing an = after the name and then the value. In the loop() you replace the ++ after the variable names with -- to decrement them. Then in the if condition secondes < 0) and inside the if statement secondes = 59. Thats as far as I will go here. With some tutorials and experimenting you will be able to understand that code.
    – chrisl
    Commented Mar 25, 2021 at 20:39
  • I didnt know how to initailize that. ... what do you think that line such as secondes = 0; does?
    – jsotola
    Commented Mar 25, 2021 at 23:07
  • "customize this code i found" is not why we're here. I mean, this is really trivial. Break the problem up into smaller steps and ask for help when you get stuck. Use Serial.println() a lot when you start coding. a lot.
    – dandavis
    Commented Mar 25, 2021 at 23:25

2 Answers 2

1

As a start, you need to initalise the secondes and minutes variables to 30 and 1 respectively.

replace signed short minutes, secondes; with:

signed short minutes = 1;
signed short secondes = 30;

Next thing I notice is that the code you have provided counts up and you have requested that it count down.

Change secondes++; and minutes++; with:

secondes--;
minutes--;

Lastly you need to change the logic for when the secondes and minutes variables reach zero.

If the secondes variable reaches zero but the minutes variable is greater than zero, then you decrement the minutes (using the line of code provided above) and top-up the secondes with 59.

secondes = 59;

You will need to check the value of the secondes and minutes variables using a couple of if...else statements.

if (secondes == 0 && minutes == 0) {
    // insert your timer finished code here
}
else if ( secondes == 0 && minutes > 0) {
    minutes--;
    secondes = 59;
}
else {
    // Error - your code should not reach here.
    // Display an error message on the LCD
}

This if..else statement will need to go at the top of your loop instead of the end.

1
1

The first thing I recommend is that you don't use sprintf(). Your sketch "program storage space" drops from 3502 bytes down to 1928 bytes by NOT using that function call, and dynamic memory used drops from 101 to 79 bytes.

I would recommend an OOP design that would make it easy to add functionality, like the ability to start, stop and reset the timer.

// Arduino Uno, IDE version 1.8.9
// 2330 bytes (7%) of program storage space.
// Global variables use 201 bytes (9%) of dynamic memory.
class CountdownTimer{

  private:

    byte m_hour;
    byte m_minute;
    byte m_second;
    byte m_timerActive;
    byte m_timerComplete;
    unsigned long m_previousMillis;

  public:

    CountdownTimer():
      m_hour(0),
      m_minute(0),
      m_second(0),
      m_timerActive(0),
      m_timerComplete(0),
      m_previousMillis(0){}

    bool Update(){

      if(m_hour == 0 && m_minute == 0 && m_second == 0){
        m_timerComplete = 1;
        return 0;
      }

      if(m_timerActive && (millis() - m_previousMillis >= 1000)){
        m_previousMillis += 1000;
        m_second--;
        if(m_second == 255){
          m_second = 59;
          m_minute--;
          if(m_minute == 255){
            m_minute = 59;
            m_hour--;
            if(m_hour == 255){
              m_hour = 0;
            }
          }
        }
        return 1;
      }
      else{
        return 0;
      }
    }

    byte GetIsTimerComplete(){
      return m_timerComplete;
    }

    void SetHour(byte hour){
      m_hour = constrain(hour, 0, 23);
    }

    void SetMinute(byte minute){
      m_minute = constrain(minute, 0, 59);
    }

    void SetSecond(byte second){
      m_second = constrain(second, 0, 59);
    }

    void Start(){
      m_timerActive = 1;
    }

    void Stop(){
      m_timerActive = 0;
    }

    void ReStart(){
      m_timerActive = 1;
      m_timerComplete = 0;
      m_previousMillis = millis();
    }

    void PrintTimeRemaining(){
      if(m_hour < 10){Serial.print("0");}
      Serial.print(m_hour);
      Serial.print(":");
      if(m_minute < 10){Serial.print("0");}
      Serial.print(m_minute);
      Serial.print(":");
      if(m_second < 10){Serial.print("0");}
      Serial.println(m_second);
    }

};

// Make a copy of the object to work with.
CountdownTimer myCountdownTimer;
  
void setup(){

  // Built in pin 13 LED timer completed indicator.
  pinMode(LED_BUILTIN, OUTPUT);
  digitalWrite(LED_BUILTIN, LOW);

  // Test output to the serial monitor.
  Serial.begin(9600);

  // Set the countdown timer values,
  // start the countdown timer and
  // print the initial timer setting.
  myCountdownTimer.SetHour(0);
  myCountdownTimer.SetMinute(1);
  myCountdownTimer.SetSecond(30);
  myCountdownTimer.Start();
  myCountdownTimer.PrintTimeRemaining();
}

void loop(){

  // Call the Update function as fast as possible.
  if(myCountdownTimer.Update()){
    myCountdownTimer.PrintTimeRemaining();
  }

  // Turn on the Uno's Pin 13 LED once timer is complete.
  if(myCountdownTimer.GetIsTimerComplete()){
    digitalWrite(LED_BUILTIN, HIGH);
  }

}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.