Go Down

Topic: Who needs a Software Real Time Clock? (Read 13814 times) previous topic - next topic

mem

#15
Aug 14, 2008, 04:42 pm Last Edit: Aug 14, 2008, 06:05 pm by mem Reason: 1
ytsirk, are you testing it using the Processing example sketch to set the clock or some other code. I wonder if there is some serial data being received by the arduino that is causing the symptoms you describe.

You could test your Arduino by running a simple sketch like this one to see if it survives beyond 30 minutes or so
Code: [Select]

int i = 0;
void setup(){
 Serial.begin(19200);
 pinMode(13,OUTPUT); // we flash the LED each second
}

void  loop(){
  delay(1000);
  Serial.print(i++,DEC);
}


edit: I have been running the playground example sketch for just over an hour without problem on my board.  Please let me know if the sketch in this post runs ok or not on your board.

ytsirk

#16
Aug 15, 2008, 03:10 am Last Edit: Aug 15, 2008, 03:26 am by ytsirk Reason: 1
That code worked for well over a half an hour without a hitch and I haven't had any trouble with any other code running on it before.   Your question about serial data had me thinking so I tried a modified version of the playground code to try to eliminate that possibility.  
I came up with this:
Code: [Select]
#include <DateTime.h>
#include <DateTimeStrings.h>

void setup(){
 Serial.begin(19200);
}

void  loop(){  
DateTime.sync(1218757280);   // Sync DateTime clock to the time received on the serial port
while (1){
 if(DateTime.available()) { // update clocks if time has been synced
   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

   // send our time to an app listening on the serial port
   Serial.println(DateTime.now());      
 }
}
}

void digitalClockDisplay(){
 // digital clock display of current time
 Serial.print(DateTime.Hour,DEC);  
 printDigits(DateTime.Minute);  
 printDigits(DateTime.Second);
 Serial.print(" ");
 Serial.print(DateTimeStrings.dayStr(DateTime.DayofWeek));
 Serial.print(" ");  
 Serial.print(DateTimeStrings.monthStr(DateTime.Month));  
 Serial.print(" ");
 Serial.println(DateTime.Day, DEC);  
}

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);  
}


Unfortunately I got the same problem, a freeze, and then a bit later a seemingly random date and time started up again.  If you had it running with no problems then I'm wondering if it might be a hardware issue of some sort.

edit:  I'm starting to wonder if it is indeed a random date and time it restarts at: with this code it always seems to jump to October 3rd when it starts up again... hmm.

mem

#17
Aug 15, 2008, 05:06 am Last Edit: Aug 15, 2008, 05:07 am by mem Reason: 1
Is the time after restart always exactly the same to the second ?  Not sure what that means but it may help us track things down.

It may be worth adding a print statement in setup to see if it  resets after freezing (although I don't think it has)

Also adding a few lines to the sketch you posted above will tell us if its freezing while waiting for the seconds to change:

   unsigned long prevtime = DateTime.now();
   while( prevtime == DateTime.now() )  // wait for the second to rollover
    {        // the following lines to be added to your test sketch
       Serial.print(millis(),DEC);
       delay(500); // this reduces the number of serial message sent  
   }


if the millis value doesn't change when it freezes, we can look for something that may be blocking interrupts.

FYI, the playground sketch has been running continuously now on my board for 12 hours without problem.

mlindeblom

There seems to be three issues being discussed here:

1) the need for a date, hour, minute and second time keeping feature.
2) the synchronization of this feature with the outside world.
3) should this feature be basic to the arduino?

I would suggest time is fundamental to our every day life; hence, flows over into the devices we build with the arduino.

As for synchronization, the network people have studied this problem to dead about 20+ years ago.  NTP or Network Time Protocol resulted and is extremely accurate to milliseconds.  Arduino is testing an ethernet stack, thus, access to NTP will follow.

mlindeblom

Another alternative for time syncronization other than a RTC chip is via a GPS chip which produces NMEA output.

The NMEA 0183 data stream consists of a series of "sentences" delimited by a newline character. Each sentence begins with a six character identifier, the first character of which is always "$". The NMEA 0183 standard defines dozens of sentences, but only a fraction apply directly to GPS devices. The most useful sentences include:

$GPAAM - Waypoint Arrival Alarm
$GPBOD - Bearing, Origin to Destination
$GPBWW - Bearing, Waypoint to Waypoint
$GPGGA - Global Positioning System Fix Data
$GPGLL - Geographic Position, Latitude/Longitude
$GPGSA - GPS DOP and Active Satellites
$GPGST - GPS Pseudorange Noise Statistics
$GPGSV - GPS Satellites in View
$GPHDG - Heading, Deviation & Variation
$GPHDT - Heading, True
$GPRMB - Recommended Minimum Navigation Information
$GPRMC - Recommended Minimum Specific GPS/TRANSIT Data
$GPRTE - Routes
$GPVTG - Track Made Good and Ground Speed
$GPWCV - Waypoint Closure Velocity
$GPWNC - Distance, Waypoint to Waypoint
$GPWPL - Waypoint Location
$GPXTE - Cross-Track Error, Measured
$GPXTR - Cross-Track Error, Dead Reckoning
$GPZDA - UTC Date/Time and Local Time Zone Offset
$GPZFO - UTC and Time from Origin Waypoint
$GPZTG - UTC and Time to Destination Waypoint

uncomplicatedi

I want to use this library but it doesn't compile as the download is missing files.
Specifically DateTimeStrings.h
Anyone?

mem

Just refreshed the zip with the missing file. try it now

uncomplicatedi

How long until the download is refreshed? I am still getting the old library. Thanks

mem

Quote
How long until the download is refreshed? I am still getting the old library. Thanks

I tried it right after I uploaded it and got the latest version with DateTimestrings.h.
Try it again, but here is the content for DateTimeStrings.h if you are still having trouble:
Code: [Select]

#ifndef DateTimeStrings_h
#define DateTimeStrings_h

#include <inttypes.h>
#include <DateTime.h>

#define dt_MAX_STRING_LEN 9 // length of longest string (excluding terminating null)

class DateTimeStringsClass
{
private:
     char buffer[dt_MAX_STRING_LEN+1];
public:
     DateTimeStringsClass();
     char* monthStr(byte month);
     char* dayStr(byte day);
};

extern DateTimeStringsClass DateTimeStrings;  // make an instance for the user

#endif /* DateTimeString_h */

DeanoAus

I cannot get this to work at all using the example sketch provide, word for word out of the zip or on the code page.

Uploading it and monitoring the serial window just gives me a blank screen?

mem

#25
Oct 22, 2008, 02:59 pm Last Edit: Oct 22, 2008, 03:00 pm by mem Reason: 1
Hi DeanoAus, how are you setting the time on the Arduino?

The date and time needs to be set when the sketch starts, this can be through a synchronization message from on external time source such as a computer connected via the serial port.

The  zip file update for 0012 that was in the playground did not contain the Processing sketch to set the clock. I have just uploaded  a new zip that has the example sketches. You won't see the time in the display until the clock is set so try it with the processing sketch

Good luck

DeanoAus

#26
Oct 23, 2008, 01:24 pm Last Edit: Oct 23, 2008, 01:34 pm by DeanoAus Reason: 1
EDIT : Found the file, ta.

badodds

I'm still getting the version with the missing  DateTimeStrings.h and readme file when i dl from the link on Arduino Playground.  Is the file located somewhere else?

This looks great, and i'd really like to try it out.

thanks.

hotcarrier

In your example in the Playground, is there a way to preset the time in the code before compile time and not do an external synchronization of any kind?

mem

#29
Nov 08, 2008, 10:34 am Last Edit: Nov 08, 2008, 10:46 am by mem Reason: 1
Badodds, I have refreshed the uploaded in the playground

Hotcarrier, you can set the time to a hard coded value using DateTime.sync()

Below is the example  sketch that sets the time to midnight Jan 1 1970. You can use an online unix  time calculator like this one to get any specific time: http://www.onlineconversion.com/unix_time.htm
Code: [Select]

#include <DateTime.h>
#include <DateTimeStrings.h>

#define START_TIME 0  // set this to the Unix start time you want (0 is midnight Jan1 1970 UTC)

void setup(){
 Serial.begin(19200);
 pinMode(13,OUTPUT); // we flash the LED each second
 DateTime.sync(START_TIME);
}

void  loop(){
 unsigned long  prevtime;
 if(DateTime.available()) { // update clocks if time has been synced
   digitalWrite(13,LOW);  // first flash the LED
   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
   digitalWrite(13,HIGH);
 }
 delay(100);
}

void digitalClockDisplay(){
 // digital clock display of current date and time
 Serial.print(DateTime.Hour,DEC);
 printDigits(DateTime.Minute);
 printDigits(DateTime.Second);
 Serial.print(" ");
 Serial.print(DateTimeStrings.dayStr(DateTime.DayofWeek));
 Serial.print(" ");
 Serial.print(DateTimeStrings.monthStr(DateTime.Month));
 Serial.print(" ");
 Serial.println(DateTime.Day,DEC);
}

void printDigits(byte digits){
 // utility function for digital clock display: prints preceding colon and leading 0
 Serial.print(":");
 if(digits < 10)
   Serial.print('0');
 Serial.print(digits,DEC);
}


and here is a sketch that sends the time to the serial port when a button is pressed
Code: [Select]
#include <DateTime.h>
#include <DateTimeStrings.h>

#define START_TIME 0  // set this to the Unix start time you want (0 is midnight Jan1 1970 UTC)
#define BUTTON     2  // pressing button on pin 2 sends the time to the serial port

void setup(){
 Serial.begin(19200);
 DateTime.sync(START_TIME);
}

void  loop(){
 unsigned long  prevtime;
 if(digitalRead(2) == LOW ){
    if(DateTime.available())
       digitalClockDisplay( );   // update digital clock
 }
 delay(100);  
}

void digitalClockDisplay(){
 // digital clock display of current date and time
 Serial.print(DateTime.Hour,DEC);
 printDigits(DateTime.Minute);
 printDigits(DateTime.Second);
 Serial.print(" ");
 Serial.print(DateTimeStrings.dayStr(DateTime.DayofWeek));
 Serial.print(" ");
 Serial.print(DateTimeStrings.monthStr(DateTime.Month));
 Serial.print(" ");
 Serial.println(DateTime.Day,DEC);
}

void printDigits(byte digits){
 // utility function for digital clock display: prints preceding colon and leading 0
 Serial.print(":");
 if(digits < 10)
   Serial.print('0');
 Serial.print(digits,DEC);
}


Go Up