Go Down

Topic: Time and TimeAlarms Libraries – Ask here for help or suggestions (Read 82823 times) previous topic - next topic

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.

copet_pitik

#1
Jul 09, 2011, 08:03 pm Last Edit: Jul 22, 2011, 12:25 pm by copet_pitik Reason: 1
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
3. 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?

Code: [Select]

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



Quote
^_^ thanks, and sorry for my english ^_^
regards,
.cop.
Program Studi Instrumentasi Medis
Politeknik Mekatronika Sanata Dharma
Yogyakarta Indonesia

mem

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

copet_pitik

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

.cop.
Program Studi Instrumentasi Medis
Politeknik Mekatronika Sanata Dharma
Yogyakarta Indonesia

copet_pitik



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.
 
Program Studi Instrumentasi Medis
Politeknik Mekatronika Sanata Dharma
Yogyakarta Indonesia

mem

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.
Code: [Select]
#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

copet_pitik



I hope that gets you going in the right direction

Michael


Michael, thanks a lot for the fragment example,

.cop.
Program Studi Instrumentasi Medis
Politeknik Mekatronika Sanata Dharma
Yogyakarta Indonesia

copet_pitik

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().

Code: [Select]

  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:

Code: [Select]

#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.
Program Studi Instrumentasi Medis
Politeknik Mekatronika Sanata Dharma
Yogyakarta Indonesia

copet_pitik

#8
Jul 12, 2011, 08:09 pm Last Edit: Jul 12, 2011, 08:10 pm by copet_pitik Reason: 1
First important bug:
timerRepeat and alarmRepeat ==> my fault
:smiley-sad-blue: :smiley-sad-blue: :smiley-sad-blue:
Program Studi Instrumentasi Medis
Politeknik Mekatronika Sanata Dharma
Yogyakarta Indonesia

mem

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



copet_pitik


What is the purpose of the campareAlarmInt function?


my purpose for compareAlarmInt is to scan the alarm value in loop 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.

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

thanks for your guidance..

.nug.
Program Studi Instrumentasi Medis
Politeknik Mekatronika Sanata Dharma
Yogyakarta Indonesia

mem

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

Quote
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



copet_pitik

Program Studi Instrumentasi Medis
Politeknik Mekatronika Sanata Dharma
Yogyakarta Indonesia

mem

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.

viknet

#14
Jul 21, 2011, 01:31 pm Last Edit: Jul 21, 2011, 02:01 pm by viknet Reason: 1
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

Go Up