Count down days, hours, minutes and secunds

Here is my code:

#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 16, 2);
long hour = 23, minute = 59, second = 59;
long countdown_time = (hour*3600) + (minute * 60) + second;
void setup() {
  lcd.init();
  lcd.backlight();
  lcd.setCursor(4, 0);
  lcd.print("HH:MM:SS");
}

void loop() {
  long countdowntime_seconds = countdown_time - (millis() / 1000);
  if (countdowntime_seconds >= 0) {
    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);
  }
  delay(500);
}

Can you help me to add days to this.
Thx in advance

If you are trying to expand this to count down by days, don't bother. The Arduino is rather bad at keeping time, instead you should get a Real Time Clock (RTC) module. They will save you the headache of having to deal with time slipping and offsetting any additional code you may add later.

2 Likes

Use the same approach as the minute counter for the hour counter.
Calculate days like you count hours now.

Hint: % gives you the remainder...
27/5=5
27%5=2

Did you write that code yourself and of not do you understand what it does?


Side note

With a long for countdown_time

You do not match millis() returned type of an unsigned value. Doing maths with signed and unsigned is tricky… it would be better to use unsigned long everywhere (of course it won’t go below 0 as a result - so take that into account in the code)

Thx for suggestion. My code is slightly modified from somewhere on internet.
You are obsoletely right. In fact in my project I have DS3231 and I use 1Hz oscillator as second trigger. Once again thank you

Thx for hint.
I modified my cod as follow

  unsigned long countdown_day = countdowntime_seconds / 86400;  
  unsigned long left_after_count_day = countdowntime_seconds % 86400;

  unsigned long countdown_hour =   left_after_count_day / 3600;
  unsigned long left_after_count_hour = left_after_count_day % 3600;

  unsigned long countdown_minute = left_after_count_hour/60;
  unsigned long left_after_count_minute = left_after_count_hour%60;

  unsigned long countdown_sec = left_after_count_minute / 1;

I also change long to unsigned long.
Thx again

Thx - I change to unsigned long. But in fact, in my project I do not use millis. I use DS3231 with 1Hz oscillator instead of millis.

You never mentioned that in your first post.

...:man_shrugging:
:man_shrugging: :man_shrugging:

Sorry J-M-L. As you can see, I post here my code. My code is for test only for future use.

In test code I use millis, but My project it is something deferent. In my project I do not use millis.

My project code is very long and complex, so I did not use it here as a reference. For reference I use auxiliary test code.

Sorry for any inconvenience it may cues.

Why not just write test code using the DS3231 module? You will get different results if you don't use the same setup for your tests.

well how can we help then ?

if you have an RTC I'm not sure why you want to use the 1Hz oscillator... just read the time and use the DateTime and TimeSpan classes to do what you want

2 Likes

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