Who needs a Software Real Time Clock?

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.

#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???

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

/*
  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() ;

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

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.

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!

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.

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

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

mem, i checked and it's ok!

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.

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.

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.

mem:
After an absence I got back to this project and this time was able to get it working on a different mac with no problem on arduino 11. It runs for days with no problems. Now I cannot get it to compile on arduino 12 as it consistently is giving stdlib.h errors. I have tried the newest playground files. Anything you could point me to would be appreciated.

Hi hotcarrier, the code in the playground compiles without problem under 0012 on my machine. Have you tried to compile the playground example sketch? If that compiles ok then perhaps post your sketch, it maybe something there causing the problem.

Good Luck!

I am sure that I am doing something dumb here... but I am running into a mental block or some such craziness trying to get the hour component to show up in 12hr increments rather than the 24hr setup.
The Date Time code is working quite well - so I know it is something I am not doing right - I have tried to assign the DateTime.hour like so:

hours = (DateTime.Hour);
if (hours > 12) {hours = hours - 12;}

So... I am pretty sure it is something dumb I am doing, but at this point I cant figure it out.

I have the minutes and seconds working well - I am breaking out the tens and ones of each one, and displaying on a 7 segment LED setup.

Help is greatly appreciated! :smiley:
Thanks

to convert to 12 hour format you do the following:
between 0000 and 0059, add 12 hours
between 0100 and 1159, straight conversion to AM
between 1200 and 1259, straight conversion to PM
between 1300 and 2359, subtract 12 hours

Here is a function you can put in your sketch that will convert the 24 hour format from the library to 12 hour format

byte hours24To12(byte hours){
  if( hours == 0 )
    return 12; // 12 midnight
  else if( hours  > 12)
    return hours - 12 ;
  else
    return hours ;
}

Determining AM/PM is easier, if the 24 hour value is < 12 its AM , otherwise its PM

I will add these functions to the next release of the library, let me know how you get on.

Thanks for the code :slight_smile:

edit - fixed my code, and it works great now! :smiley:

Greetings! I'm just starting to mess around with the Arduino and I ran into a problem with the SetArduinoClock example from the DateTime library. Specifically, the Processing part of the example that reads a time code back from the Arduino to display a small analog clock on screen seems to be mangling the timecode header byte. The byte is sent from the board as 255 but ends up getting mangled by the call to myPort.readStringUntil(LF);

I came up with some alternate code that works and I wanted to share it.

void serialEvent(Serial p) {
byte[] inBuffer = myPort.readBytesUntil(LF);
if(inBuffer == null || inBuffer.length == 0) {
return;
}
if(inBuffer[0] == byte(TIME_HEADER)) {
int val = 0;
long time = 0;
for(int i = 1; i <= 10; i++)
time = time * 10 + (inBuffer - '0');

  • UpdateClock(time);*
  • } else {*
  • print(new String(inBuffer));*
  • } *
    }[/font]
    I'm running Processing V. 1.03 on a Mac. I'm posing this here as the DateTime wiki page linked to this forum. I also couldn't send a direct message to the PM contact as I have a posting count of 0...until I click Post in 3, 2 , 1.... :slight_smile:

Hi Dave, what symptoms are you seeing? I am running processing 1.01 (on windows) and the playground code works fine for me. But I am happy to make the change from strings to a char array if it fixes the problem for you. I am curious however to know what exactly is the problem.

i'm working on arduino severino
it have serial input, so i changed baud rate to 9600
i'm able to upload successfully, but no output from serial port is coming