tiny bug with my ds1302 clock

This bug only appears in midnight 12:00 am-1am, very creepy, and I can’t find it.
I am sure the problem is from the value “halfday”; because when setting the time at this hour, everytime I recall the code block to set the time; halfday is added to the equation.

Here are the values for hrx: there is no 0 (zero) for hrx;
12-1-2-3-4-5-6-7-8-9-10-11 AM
12-1-2-3-4-5-6-7-8-9-10-11 PM

Addition:
Ds1302 library from : DS1302 - Rinky-Dink Electronics
I also confirmed that this bug also appears when it’s in the noon. 0-12 is the problem.

attachment is the full code.

Here are codes to read the time data from the ds1302

rtc.halt(false);
t = rtc.getTime();
 dayx=t.dow;
 hry=t.hour;
 minx=t.min;
 secx=t.sec; 

// ds1302 chip hr range: 0-23; min range:0-59; sec range 0-59;
// convert 24 hr to 12 with am. pm. halfday=0=am
  if(hry==0 ){hrx=12; halfday=0;}  
  if(hry<12 && hry>0){hrx=hry; halfday=0;}  

  if(hry==12){hrx=12; halfday=1;}  
  if(hry>=13){hrx=hry-12; halfday=1;}

Here are the code to write the time to the ds1302

 if(setime==1){
    
    if(halfday==0){ 
    rtc.halt(false);
    rtc.writeProtect(false);  
    if(hrx<12){ rtc.setTime((hrx), (minx), (secx));}
    if(hrx==12){ rtc.setTime(0, (minx), (secx));}
    rtc.setDOW(dayx); } 
    
    if(halfday==1){ 
    rtc.halt(false);
    rtc.writeProtect(false);      
    if(hrx<12){ rtc.setTime((hrx+12), (minx), (secx)); }
    if(hrx==12){ rtc.setTime(12, (minx), (secx)); }
    rtc.setDOW((dayx)); } 
    
    setime=0;     
  }

Juno-LED_80_Clock.ino (14.8 KB)

I think I'd rename halfday to morning, and make it a boolean. true means morning, false means not morning (or afternoon).

I'd deal with the hour value being more than 13 first, then deal with the hour value being 13, then deal with the hour value being 0. There is no need to fart around with hrx and hry when ONE value is sufficient.

Thatarrangementofcodeabsolutelytodealwiththehourvalueabsolutelysucks. Some white space wouldn't kill you.

Using Tools + Auto Format before posting wouldn't hurt, either.

arduinomagbit:
This bug only appears in midnight 12:00 am-1am, very creepy, and I can’t find it.

Despite of the fact that you need a clock to display 12 hour AM/PM time format, you better

  • let the RTC run in 24 hour format
  • read time from RTC in 24 hour format only
  • write time to RTC in 24 hour format only
  • and ONLY if you need the time formatted for display, you convert it from 24 hour to 12 hour AM/PM format

The next lines of code following your comment line
// convert 24 hr to 12 with am. pm. halfday=0=am
is wrong for what the comment says.

I think fixed conversion logic from 24 hour (“hour24”) to 12 hour format (“hour12”) would be:

min12=min24; // minute never changes
sec12= sec24; // second never changes
hr12=hr24;    // assume hour is the same
if (hr24<12)   // now set AM/PM and adjust hour
{
  halfday=0;
  if (hr24==0) hr12=12;
}
else
{
  halfday=1;
  if (hr24>12) hr12= hr24-12;
}

If you need a conversion logic the other way round from 12 hour AM/PM to 24 hour, it’s a bit different logic, of course. But I’d do it anyway and not set the RTC to 12 hour time, so that the RTC can always run in 24 hour mode, no matter whether the display formatting is chosen to be in 12 hour mode.

PaulS: I think I'd rename halfday to morning, and make it a boolean. true means morning, false means not morning (or afternoon).

I'd deal with the hour value being more than 13 first, then deal with the hour value being 13, then deal with the hour value being 0. There is no need to fart around with hrx and hry when ONE value is sufficient.

Thatarrangementofcodeabsolutelytodealwiththehourvalueabsolutelysucks. Some white space wouldn't kill you.

Using Tools + Auto Format before posting wouldn't hurt, either.

when hry=13, hrx=13-12=1, halfday=1, so it's pm. checked!

when halfday=1 and hrx=1, so when setting the time, hrx+12=13, minx=minx, secx=secx, check!

when hry=0, hrx=12, halfday=0, so it's am. checked!

when halfday=0 and hrx=12, so when setting the time, hrx=0, minx=minx, secx=secx, check!

when hry=12, hrx=12, halfday=1, so it's pm. checked!

when halfday=1 and hrx=12, so when setting the time, hrx=12, minx=minx, secx=secx, check!

Because my clock uses 12 format. hrx is for the 12 format internally, and hry is for reading the 24 format from the ds1302. I read through my codes more than 10 times already. still can't find this tiny bud.

jurs:
Despite of the fact that you need a clock to display 12 hour AM/PM time format, you better

  • let the RTC run in 24 hour format
  • read time from RTC in 24 hour format only
  • write time to RTC in 24 hour format only
  • and ONLY if you need the time formatted for display, you convert it from 24 hour to 12 hour AM/PM format

The next lines of code following your comment line
// convert 24 hr to 12 with am. pm. halfday=0=am
is wrong for what the comment says.

I think fixed conversion logic from 24 hour (“hour24”) to 12 hour format (“hour12”) would be:

min12=min24; // minute never changes

sec12= sec24; // second never changes
hr12=hr24;    // assume hour is the same
if (hr24<12)   // now set AM/PM and adjust hour
{
 halfday=0;
 if (hr24==0) hr12=12;
}
else
{
 halfday=1;
 if (hr24>12) hr12= hr24-12;
}




If you need a conversion logic the other way round from 12 hour AM/PM to 24 hour, it's a bit different logic, of course. But I'd do it anyway and not set the RTC to 12 hour time, so that the RTC can always run in 24 hour mode, no matter whether the display formatting is chosen to be in 12 hour mode.

The DS1302 is running in 24 hr format, and read and write in 24 hr format. Internally, the clock runs on itself and only read the time values from the DS1302 once every minute. Everything works fine. My problem is that there is a tiny tiny bug that appears when it’s 12:00-12:59:59 am and pm and when I use the setime block of codes. I have checked many times my 24-12hr conversion codes and my setime code (writing code to the ds1302) and can’t find the problem. I appreciate that you have written this conversion codes for me, but I only need to find this tiny bug…

I have checked many times my 24-12hr conversion codes

If the problem is with setting the clock time when it is between midnight and 1:00 AM or between noon and 1:00 PM, then the 24 hour -> 12 hour conversion code is NOT the problem area.

Frankly, I can't see what the problem is. hrx, minx, and secx are derived from t.hour, t.min, and t.sec which you still have. Set the time based on them, not the f**ked with hrx, minx, and secx values.

arduinomagbit: I have checked many times my 24-12hr conversion codes and my setime code (writing code to the ds1302) and can't find the problem. I appreciate that you have written this conversion codes for me, but I only need to find this tiny bug....

What about your settime code - do you want to take a 24hour time and set the RTC to 24hour timing? - or do you want to take a 12hour time and set the RTC to 24hour timing?

In case of setting a 24hour time to the RTC running in 24hr mode, it's a no-brainer.

But in case you want to take a 12hour time and set the RTC to 24hour timing you need a conversion routine that does the opposite as the 24to12hr conversion code: You will need to have a 12to24hr conversion code!

arduinomagbit: This bug only appears in midnight 12:00 am-1am, very creepy, and I can't find it.

I think it's time to call an exterminator.

aarg: I think it's time to call an exterminator.

I'd start with exterminating the code.

The ONLY need to convert from 24 hour time to 12 hour time is when you want to display it. There is NO need to convert 12 hour time to 24 hour time.

PaulS: The ONLY need to convert from 24 hour time to 12 hour time is when you want to display it.

Or strike it.

Anyway, for the conversion, this seems like it would work:

hr12 = ((hr24 + 11) % 12) + 1;

odometer: Or strike it.

Anyway, for the conversion, this seems like it would work:

hr12 = ((hr24 + 11) % 12) + 1;

I like the simplicity of this line of code; It works for 24-12 conversion perfectly and I am replacing my old codes with this one. thank you. My old codes work as well, and require less brain cells to write. but unfortunately my tiny bug is not from the 24-12 conversion.

I've seen someone else having problems reading the clock chip similar to what your seeing. Don't use the DS1302 library. Just read the registers your self and bypass the problem source. It doesn't need a separate library for such a simple device. I don't think the library handles the added bits correctly for over 12 hours. Dwight