Problem with building a countdown timer

Hi there, it's my first time to post here and I look forward to become active :smiley:
So I am building a countdown timer using an Arduino Uno, 16x2 blue LCD and a RTC DS3231.

My concept is to take the time now (using RTC), and then get the difference between now and the user's input to be able to display a nice HH:MM:SS:MSC formated remaing time.

So I am able to get everything working except for when the endTime is in the next day.
For example at 19:35:00:000 (now) the countdown tells me that there is 5:25:00:000 till the clock ticks 1:00:00:000
But in the case when I input 2:00:00:000 as the deadline, the countdown displays 00:25:00:000.

BUT WHY IS IT WORKING FOR SOME HOURS AND FOR OTHERS NOT?

I've been working on this issue for the past 3 days and I couldn't find the solution.
Any help? please? Any thing would be greatly appreciated

Here is the part of the code where difference gets calculated

int displayCountdown(){
  // if (time's up)
  //  display 00.00.00.000 and lcd.print("Time's up."); at lcd.setCursor(3,1);
  //else
  //  display running time and animation()
  DateTime now = rtc.now();
  hourNow = now.hour();
  minuteNow = now.minute();
  secondNow = now.second();

  int minutes = (hourNow*60) + minuteNow;
  int secondsStart = (minutes*60) + secondNow; //start timer here

  int minuteEnd = (hour*60) + minute;
  int secondsEnd = (minuteEnd*60) + second;    //end timer here

  //difference is the remaining time
  //if difference <0  =>  end time is in the next day
  if(secondsEnd - secondsStart < 0){
      int temp = abs(secondsEnd - secondsStart);
      deltaTimeSeconds = 86400 - (temp);
    }else if(secondsEnd - secondsStart == 0){
      deltaTimeSeconds = 86400;
    }else{
      deltaTimeSeconds = secondsEnd - secondsStart;
    }

  if(deltaTimeSeconds !=0){
    int convertedSecond = (deltaTimeSeconds % 60);
    int convertedMinute = (deltaTimeSeconds % 3600) / 60;
    int convertedHour = (deltaTimeSeconds % 86400) / 3600;
    int convertedMillisecond = 0;

And attached is the full 444-lined code:

OS_Countdown.ino (10.3 KB)

I did not look at your code, but, for me, any timing program would be to convert EVERYTHING to seconds for processing.

For display purposes, only, convert your seconds back to hours, minutes, seconds. You only need to do this once per second.

Paul

How about using Unix time for both the current and end times ? Subtract one from the other, convert the difference into HH:MM:SS or whatever for display purposes.

Paul_KD7HB:
I did not look at your code, but, for me, any timing program would be to convert EVERYTHING to seconds for processing.

For display purposes, only, convert your seconds back to hours, minutes, seconds. You only need to do this once per second.

Paul

Yes that's what I did

UKHeliBob:
How about using Unix time for both the current and end times ? Subtract one from the other, convert the difference into HH:MM:SS or whatever for display purposes.

Oh that's a very good idea actually it didn't come to my mind but thinking of it, I will no more have problems between am pm and between days and accuracy will be insured !!

Thank you very much :smiley: Very helpful

  if(convertedMillisecond<100 && convertedMillisecond<10){

For what values of convertedMillisecond will this statement evaluate to true? Why do you need a compound if statement to distinguish those values?