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.
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() ;
Serial.print(c);
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?
thanks,
tim
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:
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?
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?
You can convert UNIX time to whatever with the time lib but
hourFormat12()
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...
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.
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.