Hello,..
I'am just starting with Arduino. I have a problem with printing time. What I want is just the actual time (example: 01-01-2009 13:05:00) The date is not needed, the time is essential. I studied the tutorail : arduino.cc/playground/Code/DateTime
over and over again, but it's still not working. Installing files no problem. No error messages.
I'am asking for time in the script with this line: digitalClockDisplay();
And get as aswer anytime: 0:00:00 Sunday January 0
I've no idea what to do next. I'am running Arduino 12 on a Mac PPC.
You have to set the time (calling DateTime.sync() ) before the clock will start ticking, and you must call DateTime.available() to update the time properties (hour, minute, sec etc).
Here is a cut down version of the playground code that sets the clock to midnight on new years eve
#include <DateTime.h>
void setup(){
Serial.begin(19200);
DateTime.sync(1230768000); // midnight on new years day 2009
}
void loop(){
unsigned long prevtime = DateTime.now();
while( prevtime == DateTime.now() ) // wait for the second to rollover
;
DateTime.available(); //refresh the Date and time properties
digitalClockDisplay( ); // update digital clock
}
void digitalClockDisplay(){
// digital clock display of current time
Serial.print(DateTime.Hour,DEC);
printDigits(DateTime.Minute);
printDigits(DateTime.Second);
Serial.println();
}
void printDigits(byte digits){
// utility function for digital clock display: prints colon and leading 0
Serial.print(":");
if(digits < 10)
Serial.print('0');
Serial.print(digits,DEC);
}
Are you running the Processing code on the Mac host? Because the Arduino has no internal real-time clock, and the ClockDisplay sketch relies on the host's clock. The Processing code sends a coded form of the time and date to the Arduino, and all the Arduino does is display it.
Thanks for your respons. If I change the setup and place: DateTime.sync(1230768000); // midnight on new years day 2009, The 'clock' is going to work fine, but started at (as given by the UNIX time ) 0:00:00 Thursday January 1.
OK that's fine, but not what I want. Every time a made an change it start again at 0:00:00 01 01. But what I realy want is that he use the 'real' time.
I did some analyses. What's not working is this part in the script where is asked for the PC time, there he get nothing back. So the only thing he can do is using the Unix time which is given in the setup. It's too complex for my, I don't see where it goes wrong. I think it's this part of the script that doesn't work on my computer:
boolean getPCtime() {
// if time sync available from serial port, update time and return true
while(Serial.available() >= TIME_MSG_LEN ){ // time message consists of a header and ten ascii digits
if( Serial.read() == TIME_HEADER ) {
time_t pctime = 0;
for(int i=0; i < TIME_MSG_LEN -1; i++){
char c= Serial.read();
if( c >= '0' && c <= '9'){
pctime = (10 * pctime) + (c - '0') ; // convert digits to a number
}
}
DateTime.sync(pctime); // Sync Arduino clock to the time received on the serial port
return true; // return true if time message received on the serial port
}
}
return false; //if no message return false
}
and
void loop(){
unsigned long prevtime;
if( getPCtime()) { // try to get time sync from pc
Serial.print("Clock synced at: ");
Serial.println(DateTime.now(),DEC);
etc etc
I don't get back:Clock synced at: So there was no succesfull communication!
There are two similar applications on the PC or Mac. One is the "Arduino IDE", and one is the "Processing IDE." You only need the Arduino program to write sketches that you upload to the Arduino board. However, a lot of people like to interface with the Processing program to send or receive live data to the Arduino board once the board has been prepared.
The "getPCtime()" code in the Arduino sketch is a routine that accepts live data from the PC or Mac. There's a separate program running in Processing IDE to feed the current time to the board, so the board knows what to display.
Just to be more clear, there is no realtime clock on the Arduino board. Every time you power it up, it is blissfully unaware that any time has passed while it was off. The Arduino can try to measure the passage of time but it's not very accurate. This program depends on the PC to tell it what time it began, and even what time it is later.
mug, my guess is that you are not sending the time sync data on the correct serial port from processing. When you run the Processing application 'SetArduinoClock' you should see a listing of the available com ports followed by the line:
Connecting to -> COM1 (or whatever your first available com port is called)
If your arduino is not connected to this port, you need to change the Processing code so it us using the correct port by changing portIndex so it is selecting the correct port.
The Arduino can try to measure the passage of time but it's not very accurate
Halley, This is not the case when using a typical Arduino or clone board with a crystal oscillator. Once synced, my boards are accurate to within a seconds of so per day.
mem, it's all relative. Sometimes, staying within a minute per day is fine. Sometimes, being off by a few milliseconds per day is not good enough. The Arduino is fine-- but the crystals are usually rated at 0.5% tolerance, so every Arduino has a different clock rate. A PC generally drifts about the same, and many PCs use daemons like NTP to correct for drift. Again, not trying to be hardnosed but I'd call it "not very accurate." I wouldn't make a bedside clock from it, in just a month it could gain or lose a minute. This code is re-sync every loop(), so it's exactly as good as the PC which is tending to it.
As I recall, the typical crystals used in Arduino boards are rated at 35ppm. This is consistent with the actual performance of the boards I have tested. How do you come by that .5% figure?
Well (350ppm X 16 ) / 1,000,000 = .0056 or .56%, so sounds like he is about correct unless I made another software error
Lefty
Lefty, I think you made another software error
35/1,000,000 = .000035 = .0035%
But it's a 16mhz crystal not a 1 mhz crystal. The 35ppm part needs to be normalized to the specific crystal frequency being used, no?
So (35ppm X 16) / 1,000,000 = .06% ? I'm sure we will zero in on this error factor sooner or later
Lefty
35ppm is a measure of the accuracy of the crystal, it's a ratio so whatever the frequency it should be within 35/1000000th of its nominal frequency over its specified temperature range. .0035% of a day is around 3 seconds and the three Arduino boards I have measured (at more or less room temperature) have drifted less than 2 seconds per day.
Fine discussion, but punctuality is not so important for my. The arduino board is used in a short flight in an rocket and al I want is the data measured is direct connect with the (real) time. And not just time intervals.
Mem wrote :
..."changing portIndex"....
How can I do this?
I thought I can write and read the Arduino, so the there's good connection between Arduino and computer.
It's still not working, suggestions?
The port that the Processing sketch is using is set with the variable portIndex
Look in the Processing sketch and you will find this line:
public static final short portIndex = 0; // select the com port, 0 is the first port
Post #5 above describes how you can tell which port the Processing is using, if this is not the same port you have selected in the arduino IDE then you need to change portIndex so that it will use the correct port.
If this is still not clear, cut and past the text in the window below the Processing IDE that shows the list of ports and the one selected.
Thanks Mem,
I understand a bit more. Found http://www.processing.org/ and have to experiment with that also to make it work
later...
Still don't understand but get the right time now on my display !
0:39:39 Friday January 2
It's working now fine. I get the right time & date, shown in the processing software.
If I upload an script to the Arduino chip, it's stored 'inside' and works over and over again.
But now I realise that the actual time I put in the chip with the processing software, is not stored in the Arduino chip.
So if I stop the processing software, the time is not continue in the chip itself. So I can't use the arduino chip "Stand Alone" and I can't transmitting wireless the realtime from Arduino to my computer.
Please confirm the limitations I found.
So if I stop the processing software, the time is not continue in the chip itself
No, the only limitation is the need to maintain power once synced. The arduino should continue to keep time as long as its powered. It does not need the connection to Processing once the clock has been synced.
If the example sketch maintains time but yours doesn't, then there is something in your code we need to fix to make it work. If you post your code, I will have a look
If you post your code...
I use the script I download here:
http://www.arduino.cc/playground/Code/DateTime
(9th phrase from the start, you see the download ZIP) I did what wrote in the README. Placed DateTime.pde in the Arduino software. And SetArduinoClock.pde in processing software . Then I upload DateTime.pde to the Arduino chip board.
Then I start SetArduinoClock. The right time is shown and update every second.
Start also DateTime in Arduino software.
When I stop SetArduinoClock with the stop button everything stops. When I stop SetArduinoClock by stop in the Mac OS X dock, the time still runs in Arduino software. If I stop monitoring in Arduino software and start again the time is 'gone'
I have to stop the program, to disconnect the USB kabel.
The Arduino print board I use is a bit different version what common seen on internet. There is an atmega168 chip on it, but les other components. See picture I places here:
http://www.xs4all.nl/~cmug/cansat/picture_print/
The power is always on the printboard.
Sounds like Processing is resetting the Arduino when you press stop. You can find ways of disabling serial reset if you search the forums, but does it matter for your application. Why not just let the Processing application keep running, or disconnect the Serial line?
Thanks Again! Great tips. With the words you've given I did some google search. Disabling the hardware reset seems the solution. Found info here: Arduino Playground - DisablingAutoResetOnSerialConnection
And used it for my (different) board. I placed an 100 Ohm resistor between reset and 5 Volt pin and now 1X time set, it stays in memory of the Arduino chip. Exactly as I wanted.
I think it could also be done this way (you've also suggest). Upload DateTime.pde to the Arduino chip board. Stop Arduino. Then start SetArduinoClock in Processing. Stop SetArduinoClock by stop in the Mac OS X dock. Disconnect the USB kabel. Check if the data that comming wireless from Arduino to the computer is with the correct realtime.
Only one problem is that I have no radio contact now available. Next week I start to buy that piece of equipment. So it did it with te hardware disabling option now.