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?
2 Answers 2
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.
-
Thank you so much!!James Christaldi– James Christaldi2021年03月26日 12:33:22 +00:00Commented Mar 26, 2021 at 12:33
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);
}
}
secondes
andminutes
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?=
after the name and then the value. In theloop()
you replace the++
after the variable names with--
to decrement them. Then in the if conditionsecondes < 0)
and inside the if statementsecondes = 59
. Thats as far as I will go here. With some tutorials and experimenting you will be able to understand that code.I didnt know how to initailize that.
... what do you think that line such assecondes = 0;
does?