time functions such as hourFormat12()

Hi, wondering if anyone knows how to pass manually entered values into these time functions:

hourFormat12() minute()

In my learning process I'm able to use UTC values and send via serial/USB to set the clock for display. But I need to be able to set the clock using buttons. Seems a simple idea since it is in most electronics with buttons but I don't find any examples of code.

Thanks for any help!

Yes, what you want is here: http://playground.arduino.cc//Code/Time..

With sample code for the library. The TimeGPS example doesn't work but the time library works very well.


Hey Bob, thanks for the reply.
But actually that is what I have implemented now. Specifically this part takes in a stream of UTC digits via serial cables. it works great:

void processSyncMessage() {
// if time sync available from serial port, update time and return true
while(Serial.available() >= TIME_MSG_LEN ){ // time message consists of header & 10 ASCII digits
char c = Serial.read() ;
if( c == TIME_HEADER ) {
time_t pctime = 0;
for(int i=0; i < TIME_MSG_LEN -1; i++){
c = Serial.read();
if( c >= ‘0’ && c <= ‘9’){
pctime = (10 * pctime) + (c - ‘0’) ; // convert digits to a number
setTime(pctime); // Sync Arduino clock to the time received on the serial port

Am I just not seeing how to use it with button input?

If a button is pressed, My_Hour/minute/second ++. So everytime a button is pressed ,it increments the time.

if(Button_Hour == HIGH)
  if(My_Hour > 12) My_Hour = 1; //checks to see it does not go over limit, if it does, set back to 1

  else My_Hour++;

setTime(pctime);   // Sync Arduino clock to the time received on the serial port

The rest of that is overhead for reading the input from serial. Program your buttons to get hours, minutes, seconds (if you want), convert to a Unix time and pass as a parameter to the setTime() function. If you need the date set you'll have to include that in your Unix time parameter.

Hey HazardsMind, thanks! actually I have something going that is similar to your suggestion. Each time the button is pushed a variable increases by one but won't go over 12.

My issue is that I'm not sure how to then insert that value into hourFormat12().

Jimmy60, thank you too! I was wonder if I would need to convert the entered time into Unix time and then use setTime(pctime). In fact I did try something similar, I did something like:

void processSyncMessage() { pctime = 1356508790; setTime(pctime); ...

but for some reason the time in that didn't get into hourFormat12(), so that when it is supposed to go to the LCD nothing happened:

lcd.setCursor(0, 2); lcd.print("Time: ");

lcd.print(hourFormat12()); printDigits(minute()); printDigits(second());

if (isAM()) { lcd.print(" AM "); } if (isPM()) { lcd.print(" PM "); }

lcd.setCursor(0, 3); lcd.print("Date: "); lcd.print(day()); lcd.print("/"); lcd.print(month()); lcd.print("/"); lcd.print(year());

Unix time is in seconds. I took a look at the time library header and it appears that hourFormat12() should return the hour in a 12 hour format (as opposed to hour() which is 24) it also appears to have an overload to return the hour when passed Unix time.

Have you tried the hour() method to see if that works?

Personally, I only ever use a 24 hour clock.

Hey Jimmy, I agree with what you say regarding hourFormat12(), I get the 12hr format when I input the Unix time number in seconds. And yes the hour() simply gives the military format, which is what I had before playing with hourFormat12(). But you see both of these require the Unix seconds as input (I believe).

What I'm trying to do is pass to hourFormat12() for example, say the number 7, for the hour. Do you think that's possible or do you suggest converting what I input with the buttons, to Unix time seconds?

thanks again for your replies. tim

You can convert UNIX time to whatever with the time lib but


is used to return the 12 hour format for the Time library which is basically the code for a digital alarm clock that runs on an Arduino. I am currently using it in two of my projects. Your processing code could easily pass the time to it. My reason for using it is that it is pretty much in the background as far as it's operation goes and allows me to have a clock without the bother of writing one and at the same 'time' handle screen refreshing, a touch keypad and some other devices...


Thanks Bob, your alarm clock project, do you set the time with buttons or Unix time?

thx tim

I use a GPS receiver and soon it will have a Dallas DS3231 added as well as I want the clock to be safe from power line failure as it can take up to 10 minutes to acquire a GPS 'fix' to get the time.


ok then... I need to figure it out for buttons, and then NTP or GPS.

Read the whole Time thread. I've seen code to do what you want . In Liudr's Phi Prompt there is something similar to what you want to do and there are Many others that I've seen. I would be happy to give you a copy of a sketch that works with the GPS data and you can do what you wish. Let me know and I'll post it.


Hey thanks Bob, I'll definitely look up Liudr then.

and when I'm ready for GPS I'll hit you up K? 8)

thanks again. Tim