London
Offline
Faraday Member
Karma: 6
Posts: 6226
Have fun!
|
 |
« on: July 09, 2011, 05:26:34 am » |
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.
|
|
|
|
|
Logged
|
|
|
|
|
Indonesia
Offline
Newbie
Karma: 0
Posts: 34
ayooo,,,DAB!!!
|
 |
« Reply #1 on: July 09, 2011, 01:03:16 pm » |
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? #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.
|
|
|
|
« Last Edit: July 22, 2011, 05:25:17 am by copet_pitik »
|
Logged
|
|
|
|
|
London
Offline
Faraday Member
Karma: 6
Posts: 6226
Have fun!
|
 |
« Reply #2 on: July 09, 2011, 02:58:34 pm » |
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
|
|
|
|
|
Logged
|
|
|
|
|
Indonesia
Offline
Newbie
Karma: 0
Posts: 34
ayooo,,,DAB!!!
|
 |
« Reply #3 on: July 09, 2011, 11:31:38 pm » |
mem, thanks for your reply.. OK, i will try your suggesttion..
.cop.
|
|
|
|
|
Logged
|
|
|
|
|
Indonesia
Offline
Newbie
Karma: 0
Posts: 34
ayooo,,,DAB!!!
|
 |
« Reply #4 on: July 11, 2011, 08:22:40 am » |
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.
|
|
|
|
|
Logged
|
|
|
|
|
London
Offline
Faraday Member
Karma: 6
Posts: 6226
Have fun!
|
 |
« Reply #5 on: July 11, 2011, 09:13:18 am » |
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
|
|
|
|
« Last Edit: July 11, 2011, 09:15:45 am by mem »
|
Logged
|
|
|
|
|
Indonesia
Offline
Newbie
Karma: 0
Posts: 34
ayooo,,,DAB!!!
|
 |
« Reply #6 on: July 11, 2011, 09:39:28 am » |
I hope that gets you going in the right direction
Michael
Michael, thanks a lot for the fragment example, .cop.
|
|
|
|
|
Logged
|
|
|
|
|
Indonesia
Offline
Newbie
Karma: 0
Posts: 34
ayooo,,,DAB!!!
|
 |
« Reply #7 on: July 12, 2011, 11:21:45 am » |
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.
|
|
|
|
|
Logged
|
|
|
|
|
Indonesia
Offline
Newbie
Karma: 0
Posts: 34
ayooo,,,DAB!!!
|
 |
« Reply #8 on: July 12, 2011, 01:09:03 pm » |
First important bug: timerRepeat and alarmRepeat ==> my fault 
|
|
|
|
« Last Edit: July 12, 2011, 01:10:36 pm by copet_pitik »
|
Logged
|
|
|
|
|
London
Offline
Faraday Member
Karma: 6
Posts: 6226
Have fun!
|
 |
« Reply #9 on: July 12, 2011, 01:31:48 pm » |
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
|
|
|
|
|
Logged
|
|
|
|
|
Indonesia
Offline
Newbie
Karma: 0
Posts: 34
ayooo,,,DAB!!!
|
 |
« Reply #10 on: July 13, 2011, 02:06:13 am » |
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.
|
|
|
|
|
Logged
|
|
|
|
|
London
Offline
Faraday Member
Karma: 6
Posts: 6226
Have fun!
|
 |
« Reply #11 on: July 13, 2011, 05:07:02 am » |
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
|
|
|
|
« Last Edit: July 13, 2011, 11:05:53 am by mem »
|
Logged
|
|
|
|
|
Indonesia
Offline
Newbie
Karma: 0
Posts: 34
ayooo,,,DAB!!!
|
 |
« Reply #12 on: July 13, 2011, 10:22:42 am » |
Thanks for your advice, mem. .nug.
|
|
|
|
|
Logged
|
|
|
|
|
London
Offline
Faraday Member
Karma: 6
Posts: 6226
Have fun!
|
 |
« Reply #13 on: July 13, 2011, 11:08:41 am » |
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.
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Jr. Member
Karma: 0
Posts: 51
|
 |
« Reply #14 on: July 21, 2011, 06:31:59 am » |
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.htmlIt heavily use the timealarm lib albeit a bit modified. regards
|
|
|
|
« Last Edit: July 21, 2011, 07:01:45 am by viknet »
|
Logged
|
|
|
|
|
|