Time and TimeAlarms Libraries – Ask here for help or suggestions

Time and TimeAlarms are libraries for handling time and time based tasks on Arduino. The code can be found here.

This thread is for help on how to use these libraries and suggestions for future improvements.

A thread specifically for discussing issues relating to updates in the beta test version can be found here (that thread will be closed after the beta testing is completed.

First, i am noob in arduino and just tinkering arduino.

i make a clock using :

  1. Arduino UNO
  2. RTC DS1307
  3. LCD 16*2 character

the purpose of my sketch:

  1. Read Serial data VB from VB to set RTC if Serial Header is 'T'
    example : T1310258400 set RTC to July 10th 2011. 00:40:00
  2. Read Serial data VB from VB to write EEPROM if Serial Header is 'I'
    example :
    I080014000045
  • then save :
    • 08 to EEPROM address 200 ==> alarm start hour
    • 00 to EEPROM address 201 ==> alarm start minute
    • 14 to EEPROM address 202 ==> alarm end hour
    • 00 to EEPROM address 203 ==> alarm end minute
    • 00 to EEPROM address 204 ==> alarm interval hour
    • 45 to EEPROM address 205 ==> alarm interval minute
  • desired output:
  • alarm ring every 45 minutes since 08.00 up to 14.00
  1. Comparing RTC time and alarm data to ring the alarm

problem:
if i compare RTC time and EEPROM value and start the alarm, alarm time is late.
example :
alarm is set like above
alarm ring at 08.45.26 (hh.mm.ss), my desired output : alarm ring at 08.45.00
alarm ring every 45 minutes and approx. 25 seconds
alarm ring at 14.14.30 (hh.mm.ss), my desired output : alarm didn't ring at that time

questions:

  1. any mistakes in my sketch?
  2. how to simplyfy this sketch?
#include <Time.h>
#include <TimeAlarms.h>
#include <Wire.h> 
#include <DS1307RTC.h>  // a basic DS1307 library that returns time as a time_t
#include <EEPROM.h>
#include <LiquidCrystal.h>
LiquidCrystal lcd(8, 7, 13, 12, 11, 10);

//RTC variable
#define TIME_MSG_LEN  11   // time sync to PC is HEADER followed by unix time_t as ten ascii digits
#define TIME_HEADER  'T'   // Header tag for serial time sync message
//===========end============

//variable for InternalSlave1() function
#define PJG_DATA 13 //the length of the alarm data
#define INT_SLAVE 'I'//alarm interval mode header
#define OTHERS 'B'//nothing
//===========end===========

//variable for EEPROMread() function
int address = 200;
byte value;
//===========end===========
void setup()
{
  Serial.begin(9600);
  lcd.begin(16,2);
  pinMode(13,OUTPUT);
  setSyncProvider(RTC.get);   // the function to get the time from the RTC
  setSyncInterval(600);
  if(timeStatus()!= timeSet)
    lcd.println("Can't sync RTC! ");
  else
    lcd.println(" Can sync RTC!  ");
  delay(1000);
  lcd.clear();
}

void loop()
{
  compareAlarmInt();
  Alarm.delay(0);
  if(Serial.available())
  {
    char header = Serial.read();
    if (header == TIME_HEADER) //ex : T1309734393
      processSyncMessage();
    else if (header == INT_SLAVE) //ex : I080012000045
      IntervalSlave1();
  }
  digitalClockDisplay();
  delay(1000);
}


/*====================read VB serial data to set RTC===================*/
void processSyncMessage()
{
  time_t pctime = 0;
  // return the time if a valid sync message is received on the serial port.
  while (Serial.available() > TIME_MSG_LEN);
  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   
    }
  }
  time_t t = pctime;
  RTC.set(t);
  setTime(t);
  //return;
}

//read ALARM (mode INTERVAL for SLAVE 1) from VB (Serial data) and save to EEPROM ARDUINO//
void IntervalSlave1()
{
}

/*====================COMPARING TIME data and ALARM data =======================*/
void compareAlarmInt(){
  char y = hour();
  char z = minute();
  char c = EEPROM.read (200); //hourEEPROM    ==>alarm start hour
  char d = EEPROM.read (201); //minuteEEPROM  ==>alarm start minute
  char e = EEPROM.read (202); //hourEEPROM    ==>alarm end hour
  char f = EEPROM.read (203); //minuteEEPROM  ==>alarm end minute
  if ( y >= c && z >= d && y <= e && z <= f ){
    //Serial.println("time data equal with alarm data");
    char g = EEPROM.read (204); //hourEEPROM    ==>alarm interval hour
    char h = EEPROM.read (205); //minuteEEPROM  ==>alarm interval minute
    Alarm.timerRepeat(g, h, 0, intSlave1);
  }
  else
    Serial.println("===========================");
}
/*====================INTERVAL SLAVE 1 COMMAND=================*/
void intSlave1(){
  Serial.println("bip...bipp..bippp");
}

void digitalClockDisplay()
{
  //display the clock
  //same with mem's example
}

^^ thanks, and sorry for my english ^^
regards,
.cop.

coppet_pitik, if I understand the purpose of your sketch, you don’t need to compare the eeprom time to the time, the TimeAlarm library does that for you.
To simplify things, try and get the sketch working without the eeprom code and without the serial messages to set the alarms. There are a number of ways to achieve that, perhaps the simplest Is to create a timer with the interval minute and two alarms – one to enable the timer at your start time and the other to disable the timer. After you have the hard coded version working you can add the code to set the interval and the start and end alarms from the serial message.

The example sketch provided with the library shows how to create the two alarms and some timers.
You can simplify that sketch because you only need one timer. When you are satisfied that it works you can try changing the alarm times and timer durations in the code.

Then add the serial message handling. When that is working you can add code to store the alarm times in eeprom.

I hope that helps.

Michael

mem, thanks for your reply..
OK, i will try your suggesttion..

.cop.

mem:
The example sketch provided with the library shows how to create the two alarms and some timers.

Michael

mem,
the example is refers to TimeAlarmExample Sketch, didn't it?
reffering to my example i need the Alarm on:
I080014000045
So, the alarm start on 08.00.00
the 2nd alarm start on 08.45.00
the 3rd alarm start on 09.30.00
the 4th alarm start on 10.15.00
the 5th alarm start on 11.00.00
...
the last alarm start on 14.00.00

Did you suggest me to use "Alarm.alarmRepeat or Alarm.timerOnce" to start and terminate (end) the alarm and use "Alarm.timerRepeat" to set the interval?
just wanna be sure about what i'm gonna do.
Sorry, i am kinda rough this 2 days, didn't tinkering on my UNO at all..

.cop.

Here is a (untested) fragment that shows a modified version of the TimerAlarm example with changes to turn on and off a timer that will repeat at intervals you can set.

#include <Time.h>
#include <TimeAlarms.h>

AlarmID_t repeatTimer; // the alarm id so you can change the period on the fly
 
void setup()
{
  
  setTime(8,29,40,1,1,10); // set time to 8:29:40am Jan 1 2010 

  Alarm.alarmRepeat(8,30,0, MorningAlarm);  // 8:30am every day
  Alarm.alarmRepeat(17,45,0,EveningAlarm);  // 5:45pm every day 
 
  // set the repeat timer for 45 minutes
  repeatTimer = Alarm.timerRepeat( 45 * SECS_PER_MIN, RepeatTask);  

}

void MorningAlarm()
{
  // turn on the repeat timer
   Alarm.enable(repeatTimer); 
}

void EveningAlarm()
{
   Alarm.disable(repeatTimer);        
}

void RepeatTask()
{
   // this is called every 45 minutes if the alarm is on     
}

loop and other code goes here....

to change the value of the repeat to 60 minutes you would make this call:
Alarm.write(repeatTimer, 60 * SECS_PER_MIN) ; // set the repeat period to 60

I hope that gets you going in the right direction

Michael

mem:
I hope that gets you going in the right direction

Michael

Michael, thanks a lot for the fragment example,

.cop.

mem, your sketch works great.
But, when I add your sketch to my sketch and add EEPROM library there are something wrong.
Can I place this code (below) in void loop()? not in void setup().

  Alarm.alarmRepeat(19,54,0, MorningAlarm);
  Alarm.alarmRepeat(19,55,30,EveningAlarm);   
  repeatTimer = Alarm.timerRepeat( 1 * SECS_PER_MIN, RepeatTask);

my purpose to place the code in void loop() are i can change "start, interval and stop alarm" parameters from VB anytime and save it to EEPROM.
So, if Arduino Power is OFF I don't have to set "start, interval and stop alarm" parameters.
I have tried my idea and the alarm can't be disabled.

this is the part of my sketch:

#include <Time.h>
#include <TimeAlarms.h>
#include <Wire.h>  
#include <DS1307RTC.h>  // a basic DS1307 library that returns time as a time_t
#include <EEPROM.h>
#include <LiquidCrystal.h>
LiquidCrystal lcd(8, 7, 13, 12, 11, 10);

AlarmID_t doRingIntS1;

void loop()
{
  compareAlarmInt();
  Alarm.delay(0);
  ....
}

void compareAlarmInt()
{
  char c = EEPROM.read (200); //hourEEPROM    ==>1//START HOUR
  char d = EEPROM.read (201); //minuteEEPROM  ==>1//START MINUTE
  char e = EEPROM.read (202); //hourEEPROM    ==>1//STOP HOUR
  char f = EEPROM.read (203); //minuteEEPROM  ==>1//STOP MINUTE
  char g = EEPROM.read (204); //hourEEPROM    ==>1//INTERVAL HOUR
  char h = EEPROM.read (205); //minuteEEPROM  ==>1//INTERVAL MINUTE
  Alarm.timerRepeat(c, d, 0, StIntSlave1); //==start alarm mode interval slave 1
  Alarm.timerRepeat(e, f, 0, EndIntSlave1); //==End alarm mode interval slave 1
  g = g * 60;
  h = h + g;
  alarmIntervalSlave1 =  Alarm.timerRepeat( h * SECS_PER_MIN, doRingIntS1 );
}
/*====================INTERVAL ALARM SLAVE 1, START and STOP/END =================*/
void StIntSlave1(){
  Alarm.enable(alarmIntervalSlave1);
}
void EndIntSlave1(){
  Alarm.disable(alarmIntervalSlave1);
}
/*====================INTERVAL SLAVE 1 COMMAND=================*/
void doRingIntS1(){
  lcd.setCursor(0,1);
  lcd.print("bip..bipp..bip..");
  delay(3000);
  lcd.clear();
}
...
...
...

any suggestion,mem?

thanks,

.nug.

First important bug:
timerRepeat and alarmRepeat ==> my fault
:disappointed_relieved: :disappointed_relieved: :disappointed_relieved:

Hi copet_pitik,

What is the purpose of the campareAlarmInt function?

If you want to save alarm values in eeprom so you they can be read when starting up then you don't need to compare.
Read the eeprom values in setup to set the alarms.
In loop you can check for serial messages and if you get a request to change an an alarm value you can write that to eeprom and update the alarm value.

As I suggested earlier, ignore the eeprom functionality for now.

First get the the alarms responding to serial commands. When that is fully tested and working it should be easy to add the eeprom code.

Michael

mem:
What is the purpose of the campareAlarmInt function?

my purpose for compareAlarmInt is to scan the alarm value in loop function.

mem:
If you want to save alarm values in eeprom so you they can be read when starting up then you don't need to compare.
Read the eeprom values in setup to set the alarms.
In loop you can check for serial messages and if you get a request to change an an alarm value you can write that to eeprom and update the alarm value.

mem,
when I send serial messages, then Arduino will reset automatically?

thanks for your guidance..

.nug.

my purpose for compareAlarmInt is to scan the alarm value in loop function.

That does not describe a purpose, it says how you want to achieve a purpose but not why.
What do you want to achieve when you scan the alarm value in loop?

when I send serial messages, then Arduino will reset automatically?

You said above that you wanted to send VB messages to set the alarm. If you need help with aspects of your application that are not specific to the TimeAlarms library then its best to start a new thread so that the content of this one is focused on specific help with TimeAlarms.

I suggest your create a new thread and post a link here so I and others interested in your application can help.

Michael

Thanks for your advice, mem.
.nug.

happy to help. If you have questions on the TimerAlarms library ask here. Post a link to a new thread if you have other questions on your application.

for those needing a full solution for scheduling daily / weekly start/stop of output, I started a new thread with my project.
http://arduino.cc/forum/index.php/topic,67127.0.html

It heavily use the timealarm lib albeit a bit modified.

regards

mem:
I suggest your create a new thread and post a link here so I and others interested in your application can help.

mem,here is my next question about my project, hope you can help

No errors were reported with the the beta code so the playground download has been updated.

Check my project at http://arduino.cc/forum/index.php/topic,67127.0.html

I javel thé sketch and à sérial interface also have a jquery web interface

mem:
Time and TimeAlarms are libraries for handling time and time based tasks on Arduino. The code can be found here.

This thread is for help on how to use these libraries and suggestions for future improvements.

A thread specifically for discussing issues relating to updates in the beta test version can be found here (that thread will be closed after the beta testing is completed.

Hello mem,
I needed to use DS1307 Real-Time Clock on a project and saw the Time library, but I thought it was too big for what I needed (just to know day, month, year, hour, minute and second from DS1307), so I decided to create a new, simple library to achieve this task and its code is at GitHub:

I think we should integrate it, maybe creating a "driver interface" (just some conventions) so we can create drivers for many RTC chips and use the same code. What do you think?

Other thing I think that should be changed in Time library is the namespace of functions. Maybe using Time.hour(), Time.day() etc. instead of directly hour(), day() etc.

alvarojusten:
Hello mem,
I needed to use DS1307 Real-Time Clock on a project and saw the Time library, but I thought it was too big for what I needed (just to know day, month, year, hour, minute and second from DS1307), so I decided to create a new, simple library to achieve this task and its code is at GitHub:

GitHub - turicas/DS1307: Arduino library for reading and setting date and time for the DS1307 Real-Time Clock IC

I think we should integrate it, maybe creating a "driver interface" (just some conventions) so we can create drivers for many RTC chips and use the same code. What do you think?

Other thing I think that should be changed in Time library is the namespace of functions. Maybe using Time.hour(), Time.day() etc. instead of directly hour(), day() etc.

Hi alvarojusten

Thank you for sharing your library. I have seen many DS1037 Arduino libraries and yours is one of the simplest (that’s a compliment). It does use less flash memory than the Time library but it actually seems to use more data (your sketch uses 540 bytes of RAM, the Time library code posted below uses 482 bytes of RAM).

/*
 *  A simple sketch to display time from a DS1307 RTC
 * 
 */

#include <Time.h>  
#include <Wire.h>  
#include <DS1307RTC.h>  // a basic DS1307 library that returns time as a time_t

char dateTime[20];

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

void loop()
{
   setTime(RTC.get());
   sprintf(dateTime, "%4d-%02d-%02d %02d:%02d:%02d", year(),
            month(), day(), hour(),minute(), second()) ;
    Serial.print(dateTime);
    Serial.print(" - day of week: ");
    Serial.println(dayStr(weekday()));

   delay(1000);
}

Here are some advantage of using the Time library version:

  • You can easily calculate the difference between two times
  • The time format is based on a standard that almost every programming platform can handle
  • The same sketch code can be used with a selection of time sources such as RTC, NTP( internet time standards), Radio Clocks, GPS …
  • You can use the TimeAlarms library to add timers and time of day alarms.