Go Down

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

badodds

i have the files now.  Thank you mem!

hotcarrier

mem:

I tried your suggested sketch as below:

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

#define START_TIME 1226243400  // set this to the Unix start time you want (0 is midnight Jan1 1970 UTC)
//get unix time code from  http://www.onlineconversion.com/unix_time.htm
void setup(){
 Serial.begin(9600);
 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
       ;
 //{        // the following lines to be added to your test sketch
  //    Serial.print(millis(),DEC);
      delay(1500); // this reduces the number of serial message sent  
 // }

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

I used the latest library from the playground and 0011. I tried both an older NG board as well as a Deicimilla board. In all cases the program compiles and runs for anywhere from a minute to several minutes, freezes and usually restarts again at some random date. It seems to run longer with the 1.5 second delay inserted after the second rollover while loop. Any suggestions? Thanks.

mem

#32
Nov 11, 2008, 06:15 pm Last Edit: Nov 11, 2008, 07:59 pm by mem Reason: 1
Hotcarrier, I ran that sketch for 10 minutes without problems, I will keep it running and see how it goes.

It would be worth you trying it using the latest arduino version. Although the DateTime library was designed and tested using 0011, I am now running 0012 and it may be easier to identify the problem if we are both running the current version.

Does the playground sketch run ok?  if not, you could run this sketch. It will test if the problem has something to do with the Serial port or with accessing the millis function (which the library uses to get the current time).


void setup(){
Serial.begin(9600);
pinMode(13,OUTPUT); // we flash the LED each second
}
void  loop(){
  digitalWrite(13,LOW);  // first flash the LED
  delay(1500); // this reduces the number of serial message sent  
  digitalWrite(13,HIGH);
  unsigned long time = millis();
  Serial.println(time, DEC);
delay(100);
}

-----------------
[glow]Update[/glow]

edit: I wonder if this is a RAM problem.

Could you do a test with the sketch you posted modified so that DateTimeStrings is not used

To do this, comment out the second line as follows:
// #include <DateTimeStrings.h>

And comment out the two places in digitalClockDisplay() where the strings are used:

// Serial.print(DateTimeStrings.dayStr(DateTime.DayofWeek));
Serial.print(" ");
// Serial.print(DateTimeStrings.monthStr(DateTime.Month));

Let me know if this runs ok,  

[glow]Another Update[/glow]
I just tried the sketch you posted with 0011 and its been running ok for the last ten minutes hour. If you don't wan't to update to 0012 then don't worry, I can test using 0011

piecurus

Hello everybody.
I'm using version 0012 with DateTime libraries. The following code doesn't run but if I comment "#include DateTimeStrings" everything is ok.

Quote


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

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

void loop(){
 Serial.print("ciao bello...");
}



I look at the C++ code but it seems ok! What happen???

mem

#34
Nov 12, 2008, 09:24 am Last Edit: Nov 12, 2008, 12:30 pm by mem Reason: 1
Hi piecurus, I wonder if you could do a test for me and try the folowing. Please replace DateTimeStrings.cpp in the  library directory with the code below, delete the DatetimeStrings.o file, recompile and run the sketch to see if this fixes it.

hotcarrier, you may also want to try this code

Code: [Select]
/*
 DateTimeSrings.cpp - Arduino Date and Time library string routines

 Test version 1.0b
*/

// uncomment one of these month and day defines as appropriate for your application
#define dt_LONG_MONTH_STRINGS true
#define dt_LONG_DAY_STRINGS true
//#define dt_SHORT_DAY_STRINGS
//#define dt_SHORT_MONTH_STRINGS  

extern "C" {
 // AVR LibC Includes
     #include <avr/pgmspace.h>
}

#include "DateTimeStrings.h"
#include <string.h>

// if you change the long strings below, make sure none are greater then the constant dt_MAX_STRING_LEN defined in DateTimeStrings.h
// the short strings for each day or month must be exactly 3 characters

#define dt_SHORT_STR_LEN  3 // the length of short strings

#if defined dt_LONG_MONTH_STRINGS
char monthStr0[] PROGMEM = "January";
char monthStr1[] PROGMEM = "February";
char monthStr2[] PROGMEM = "March";
char monthStr3[] PROGMEM = "April";
char monthStr4[] PROGMEM = "May";
char monthStr5[] PROGMEM = "June";
char monthStr6[] PROGMEM = "July";
char monthStr7[] PROGMEM = "August";
char monthStr8[] PROGMEM = "September";
char monthStr9[] PROGMEM = "October";
char monthStr10[] PROGMEM = "November";
char monthStr11[] PROGMEM = "December";

PGM_P monthNames_P[] PROGMEM =
{
   monthStr0,monthStr1,monthStr2,monthStr3,monthStr4,monthStr5,
     monthStr6,monthStr7,monthStr8,monthStr9,monthStr10,monthStr11
};

#elif defined dt_SHORT_MONTH_STRINGS
char monthNames_P[] PROGMEM = "JanFebMarAprMayJunJulAugSepOctNovDec";
#endif

#if defined dt_LONG_DAY_STRINGS
char dayStr0[] PROGMEM = "Sunday";
char dayStr1[] PROGMEM = "Monday";
char dayStr2[] PROGMEM = "Tuesday";
char dayStr3[] PROGMEM = "Wednesday";
char dayStr4[] PROGMEM = "Thursday";
char dayStr5[] PROGMEM = "Friday";
char dayStr6[] PROGMEM = "Saturday";

PGM_P dayNames_P[] PROGMEM =
{
   dayStr0,dayStr1,dayStr2,dayStr3,dayStr4,dayStr5,dayStr6
};

#elif defined dt_SHORT_DAY_STRINGS
char dayNames_P[] PROGMEM = "SunMonTueWedThrFriSat";
#endif


//******************************************************************************
//* DateTime Public Methods
//******************************************************************************

DateTimeStringsClass::DateTimeStringsClass()
{
  buffer[dt_MAX_STRING_LEN] = 0;  // ensure buffer is null terminated  // was +1
}

char* DateTimeStringsClass::monthStr(byte month)
{
#if defined dt_SHORT_DAY_STRINGS
//  strncpy_P(buffer, (PGM_P)pgm_read_word(&(monthNames_P[(month*dt_SHORT_STR_LEN)])),dt_SHORT_STR_LEN );  // todo !!!!
  for (int i=0; i < dt_SHORT_STR_LEN; i++)      
     buffer[i] = pgm_read_byte(&(monthNames_P[i+ (month*dt_SHORT_STR_LEN)]));  
  buffer[dt_SHORT_STR_LEN] = 0;
#else
   strcpy_P(buffer, (PGM_P)pgm_read_word(&(monthNames_P[month])));
#endif
     return buffer;
}

char* DateTimeStringsClass::dayStr(byte day)
{
#if defined dt_SHORT_DAY_STRINGS
//  strncpy_P(buffer, (PGM_P)pgm_read_word(&(dayNames_P[(day*dt_SHORT_STR_LEN)])),dt_SHORT_STR_LEN );  // todo !!!!

  for (int i=0; i < dt_SHORT_STR_LEN; i++)      
     buffer[i] = pgm_read_byte(&(dayNames_P[i+(day*dt_SHORT_STR_LEN)]));  
  buffer[dt_SHORT_STR_LEN] = 0;

#else
   strcpy_P(buffer, (PGM_P)pgm_read_word(&(dayNames_P[day])));
#endif
  return buffer;
}

// make one instance for the user
DateTimeStringsClass DateTimeStrings = DateTimeStringsClass() ;

piecurus

Sorry Mem,
looking at the code you wrote, it seems to be DateTime.cpp and not DateTimeStrings.cpp!!
is it ok?

mem

#36
Nov 12, 2008, 12:32 pm Last Edit: Nov 12, 2008, 12:40 pm by mem Reason: 1
sorry for the confusion piecurus, that is the correct code for DateTimeStrings.cpp.
The comment in that code was a typo,  I edited the post above to correct it.

piecurus

now it works!
thankyou!

Only one more comment. I'm using a Mac OS Leopard.
In the 0012 version, the example sketches have to be placed in a folder named "examples" inside the principal directory.
For instance, the DateTime sketch has to be put inside the folder "/hardware/libraries/DateTime/examples/DateTime/". In this way, the examples can be opened from "Open -> Examples -> Library-DateTime".
That's all!



mem

Thanks for the feedback. I have uploaded the new code to the playground. Please do give it a try and also verify if the directory structure for the example works for you.

hotcarrier

mem:

I will try the new DateTimeStrings.cpp, but with the older version commented out as you suggested my sketch still freezes after running a short time and then jumps to some new random time. The other sketch you asked me to try runs for 24 hours without problems. I am using a Mac for development. This is all still on 0011

mem

#40
Nov 12, 2008, 09:04 pm Last Edit: Nov 12, 2008, 09:05 pm by mem Reason: 1
Hi hotcarrier, Let me know how you get on with the DateTimeStrings code in the playground. It should be ok with either 0011 or 0012

piecurus


hotcarrier

mem:
I tried the latest libraries from the Playground and the behavior in the same sketch is the same. Runs for while, freezes and jumps to new random time restart.

mem

Hotcarrier, did you check that the latest version is being compiled?

Go to the DateTimeStrings library directory on your computer and open the file DateTimeStrings.cpp to verify that it has a comment indicating Version 1.0b 12 Nov 2008

Delete the DateTimeStrings.o file so that the code will be rebuilt and recompile your sketch and try again.

hotcarrier

mem:
Good news. I had done what you suggested and it still did not run on 0011. But I moved to 0012 and it just ran correctly overnight! I will investigate and see if I see anything further on 0011, but probably just
move to 0012 for this project. Thanks for your help.

Go Up