Arduino Clock: Setting Time

Is it possible to set the arduino time clock? If yes, how can you allow the user to enter the time so it can be set? Pls help!!!

There is a library here that provides time functions on Arduino: Arduino Playground - DateTime
Also included is a Processing sketch to set the time from a PC or Mac.

What is it you want to do?

I wanna design a GUI that allows the user to set a time when an LED should light up without changing the actual time (think of it as an alarm clock). For example, say at 4:00 pm. How would you do that?

You need to say more about what you would like to do.

What kind of gui would you like to have?

Would you like the gui on a display device connected to Arduino or running on application running on a computer connected to the serial port? (or whichever is easer to impliment?)

I'd like to use processing to develop the GUI. The GUI should take input maybe as a text field that allows the user to enter time in integer form and that input should be used to determine when the LED should light up

Is you arduino going to always be connected to the computer? Or are you just using the computer to program the Arduino, and then unplugging the Arduino and leaving it running as a stand alone, through a wallwart or battery?

If you are going to leave it connected to the computer, this is fairly easy. Your computer can do the counting and when the alarm is hit it can send a message over serial to the arduino, the arduino can then parse this message and set the LED accordingly.

If you are going to be unpluggin the arduino it gets a little complicated. I would use an external timing IC and battery back it up. I would use one that has some sort of memory like the DS1307

If you are going to be unpluggin the arduino it gets a little complicated.

not if the Arduino is battery operated or remains powered when disconnected from the seial port.

I'd like to use processing to develop the GUI. The GUI should take input maybe as a text field that allows the user to enter time in integer form and that input should be used to determine when the LED should light up

There is a processing application supplied with the download that sets the clock. You can add functionality to that Processing sketch to get an alarm time.

The DateTime library has a function to return the number of seconds that have elapsed since that start of the day. So to light an led at 8:30 in the morning for one hour you would do something like this:

time_t  alarmOnTime =  (8 * SECS_PER_HOUR) + (30 * SECS_PER_MIN);
time_t  alarmOffTime =  alarmOnTime  + (60 * SECS_PER_MIN);
time_t nextAlarm;


nextAlarm =  previousMidnight(DateTime.now())+ alarmOnTime;

if( DateTime.now() >= nextAlarm  && DateTime.now()  < nextAlarm + alarmOffTime )
    digitalWrite(ledPin), HIGH);
else
   digitalWrite(ledPin), LOW);

mem: Thanks a lot! your code worked to perfection!
darudude: thanks for your suggestion too!

But guys, how can i create a text box or anything at all to allow the user input the time the LED should light up

Buy a LCD, or a led display.

You could write a program that runs on your computer, querying the user, and sending the data over serial.

Interfacing With Software

But guys, how can i create a text box or anything at all to allow the user input the time the LED should light up

can you clarify your question? are you asking how to add it to the setArduinoClock Processing sketch? can you say more about your level of programming skill?

All i'm saying is i wanna create a GUI or something interactive where the user can actually enter the time he wants the LED to light up. So that, the user would be able to determine the values (8,30) in the example you gave me below:

time_t alarmOnTime = (8 * SECS_PER_HOUR) + (30 * SECS_PER_MIN);
time_t alarmOffTime = alarmOnTime + (60 * SECS_PER_MIN);

P.S: I'm new to Arduino. i could have done this GUI easily in C#

P.S: I'm new to Arduino. i could have done this GUI easily in C#

Look here for interfacing with c#
http://www.arduino.cc/playground/Main/InterfacingWithSoftware

Interfacing with C# is good but i'd rather not mix C# with arduino+processing except i have no other choice

If you are more comfortable with C# than Processing, there is no reason not to use it.

It should be quite easy to replace the Processing code to set the time with a C# app. it just gets the current time, converts that into the number if seconds since jan 1 1970, and sends that as as string of digits preceeded by some unique header (255 in the published version).

Write whatever gui you want in whatever language your prefer. you just need to send your alarm time as a string with some identifying header byte followed by a string of digits indicating the time_t for the alarm you want. Have a look at the Arduino example source to see how it handles this string and add you code that identifies and uses the alarm on and off times.

I hope that helps

Interfacing with C# is good but i'd rather not mix C# with arduino+processing except i have no other choice

Why not? Let the micro controller do what it does best, and let it interface with your pc for everything else. This usually results in an efficient implementation.

Your C# GUI app can send the 2 bytes (hour, minute) on serial port to arduino. What is wrong with this approach? Why do you want to include Processing in the mix?

oK. So, my project is due pretty soon and I'm already deep into using processing and that's why i rather not use C# now. If you guys are telling me, there's no way i can use arduino to get user input, that's fine

I don't think anyone has said that. If you want to get user input without an external computer, you can use an LCD or other display device. If you are ok using an application running on your PC then you can use Processing or C#. Arduino doesn't care as long as send it a value that it can recognize as an alarm time.

It seems that you have two parts to your project:

  • Processing or C# gets an alarm time from a user and sends this to arduino via the serial port
  • Arduino receives an alarm time from a PC and responds accordingly

not sure which of these you are having trouble with, but there are many ways you can implent both of the tasks.

Quote: Processing or C# gets an alarm time from a user and sends this to arduino via the serial port

Yes, that's my question- how can the user send the input time from processing over to the arduino?
This is the part of my code that gets user input from processing. h and m are the user input variables But how do you send these values to the arduino?

//Processing Code:

import javax.swing.JOptionPane;
String user_hour = (String)JOptionPane.showInputDialog(null, "Hour :","Datainputdialog", JOptionPane.PLAIN_MESSAGE);
String user_min = (String)JOptionPane.showInputDialog(null, "Minute:","Datainputdialog", JOptionPane.PLAIN_MESSAGE);
int h = Integer.parseInt(user_hour);
int m = Integer.parseInt(user_min);

You can convert the hour and minute into elapsed seconds using the logic posted in Reply#6 and send this to Arduino using similar code to the example Processing sketch that sets the time (pick a different header byte so you can differentiate the message to set the alarm from the one to set the time.

Then use the Arduino code that you have working from post#6 to trigger the alarm.