Help with binary clock conversion from 24hr to 12hr

Hi,
My wife bought me a binary clock shield that fits onto an Arduino Uno for Christmas last year. The binary clock works great but it is in the 24Hr. format. I would like to have it display only 12Hr. format. I contacted the author and designer of the shield and back in January, he told me he would be able to convert the code to 12Hr. format in a couple weeks. After several more e-mails, promises of a couple weeks I still don't have the code for a 12Hr. format. I have spent hours trying to figure out how to convert it but I am not a programmer. Below is the link to the code on Github.
If anyone can help, I would really appreciate it.

Thank You,

Ron

Just a shot in the dark, but try to find and replace this function with this code:

void getAndDisplayTime() 
{
    // Read time from RTC  
    t = RTC.get();
    // Convert time to binary format and display     
    int hours = hour(t);
    if (hours > 12) hours -= 12;
    convertDecToBinaryAndDisplay(second(t), minute(t), hours);
}

No idea, but it's worth a try.

After this part:

void convertDecToBinaryAndDisplay(int bottomRow, int middleRow, int upperRow) 
{  
    bool oneBit;
    

Put:

  // Switch from 24-hour time to 12-hour time
  if (upperRow > 12)
    upperRow -= 12;

That will probably fix the display, but doesn't address user interface for setting time and alarms. Might also want to repurpose MSB LED of hours for AM/PM Indicator since only need 4 bits for that now. Will probably take several iterations to get it completely right.

Slight variation on that:

int hour = hour(t);
if (hour == 0) {
  hour = 12; // 12 midnight
}else if( hour > 12) {
  hour = hour - 12;
}

Midnight is 12 o'clock in the 12-hour system.
You could also add a flag to indicate AM/PM and use it to turn on some indicator:

int hour = hour(t);
bool pm;
if (hour == 0) {
  hour = 12; // 12 midnight
  pm = false;
}else if( hour > 12) {
  hour = hour - 12;
  pm = true;
}
if (pm) .....

Not sure whether I would do that in void convertDecToBinaryAndDisplay() or getAndDisplayTime(), but I suppose either could work for the display at least, but in the case of the latter you might want to pass the pm indicator to the former.

Thank you for the response. I added the code and it did convert to 12hr. Thank You. When I set the time, and scroll thru the hours at 12 o'clock, none of the LED light but then I scroll all thru the time (1,2,3...)and get back to 12 o'clock, the 12 is displayed. It displays every other 12 o'clock. Any ideas how to fix that. I am so happy that I don't have 24Hr. clock anymore. Thank you

Sounds like the midnight problem I mentioned. When you get to 23:59:59 on the 24hr clock it then rolls over to 00:00:00 so you won't get any LEDs light up. That's why I suggested an additional line to deal with that. Just add the following to johnwasser's example:

if (upperRow == 0)
  upperRow = 12;

It should then roll over to 12:00:00

That did it!!!! Thank you so much for all of the suggestions. This has been on my mind since last Christmas.

Thanks to all again,

Ron

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