Hi! so Im curently in a project that requires arduino to repeat countdown and show it on i2c lcd. But the code that i run doesn't repeated itself and after the countdown reach 00:00 it stops. is there any ways to repeat the countdown after it reach 00:00? Im working with arduino uno, and this is the code.
That line is just making countdowntime_seconds more and more negative; there's nothing to make it >=0 again so this test if (countdowntime_seconds >= 0) continues to fail.
not knowing how long it will take to learn these things
code seems pretty unrelated to the repeating-problem
all in all a high probability to just drop the suggestion
waiting for an easier to understand code
my suggestion with some comments that shall explain how the code works.
The code compiles. But I haven't tested it on real hardware.
Your original constructor to create the I2C_LCD-object uses parameters like for a NON-I2C-LCD which seems strange to me
It might be that you are using a different I2C-LCD-library
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
// Define I2C Address - change if reqiuired
const int i2c_addr = 0x27;
// an I2C-LCD just needs I2C-adress columms and lines to initialise
LiquidCrystal_I2C lcd(i2c_addr,16,2); //, en, rw, rs, d4, d5, d6, d7, bl, POSITIVE);
unsigned long hour = 0;
unsigned long minute = 0;
unsigned long second = 59;
unsigned long countdown_period = (hour * 3600UL) + (minute * 60) + second;
// easy to use helper-function for non-blocking timing
boolean TimePeriodIsOver (unsigned long &startOfPeriod, unsigned long TimePeriod) {
unsigned long currentMillis = millis();
if ( currentMillis - startOfPeriod >= TimePeriod ) {
// more time than TimePeriod has elapsed since last time if-condition was true
startOfPeriod = currentMillis; // a new period starts right here so set new starttime
return true;
}
else return false; // actual TimePeriod is NOT yet over
}
unsigned long MyTimer = 0; // Timer-variables MUST be of type unsigned long
unsigned long countdown_seconds_left;
void setup() {
lcd.begin(16, 2);
lcd.backlight();
lcd.setCursor(0, 0);
lcd.print("HH:MM:SS");
countdown_seconds_left = countdown_period; // initialise variable that is counting down to zero
MyTimer = millis(); // initialise timer-variable with actual value of function millis()
}
void count() {
if (countdown_seconds_left > 0) { // check if variable that counts down to zero is still ABOVE zero
countdown_seconds_left--; // if above zero decrement by one
}
else { // variable ciunting down has reached ZERO
countdown_seconds_left = countdown_period; // initialise variable that is counting down to zero
// what makes the countdown counting down again
}
if (countdown_seconds_left >= 0) {
unsigned long countdown_hour = countdown_seconds_left / 3600;
unsigned long countdown_minute = ((countdown_seconds_left / 60) % 60);
unsigned long countdown_sec = countdown_seconds_left % 60;
lcd.setCursor(0, 0);
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);
}
}
void loop() {
if ( TimePeriodIsOver(MyTimer, 1000) ) {
// if 1000 milliseconds have passed by
count(); // ==> function "count()" is only called ONCE per second
}
}