Go Down

Topic: Arduino timer scheduler library (Read 10146 times) previous topic - next topic

gagnon_eric

Hi,

I'm faily new in the ARduino would and more specially using the library for dateTimeAlarm, can someone tell me how I can get/read what alarm I already set ?
I would like to be able to use my ethernet shield to set alarm and to be able to read what I set and display it on my web page.
So far setting an alarm to activate/deactivate a port work just fine but how can I get/read what was set in the alarm?

Thanks,
Eric

mem

#31
Oct 07, 2010, 04:31 pm Last Edit: Oct 07, 2010, 07:32 pm by mem Reason: 1
You can get the value of an alarm using the Alarm.read method.

This  method will return the alarm value (or zero if the alarm is not allocated).

Values less than or equal to the number of seconds per day (SECS_PER_DAY) are alarms that will repeat each day (when the elapsed seconds in the day is equal to the value). Values greater than SECS_PER_DAY are treated as absolute times and will trigger once only at that given time and date.

Here is a modified version of the example sketch that illustrates the use of  Alarm.read
Code: [Select]
#include <Time.h>
#include <TimeAlarms.h>

AlarmID_t firstAlarm;
AlarmID_t secondAlarm;

void setup()
{
 Serial.begin(9600);    
 setTime(8,29,40,1,1,10); // set time to 8:29:40am Jan 1 2010

 firstAlarm = Alarm.alarmRepeat(8,30,0, MorningAlarm);  // 8:30am every day
 secondAlarm = Alarm.alarmRepeat(17,45,0,EveningAlarm);  // 5:45pm every day
}

void MorningAlarm()
{
 Serial.println("Alarm: - turn lights off");    
}

void EveningAlarm()
{
 Serial.println("Alarm: - turn lights on");          
}


void  loop()
{  
 time_t t = now();
 digitalClockDisplay(t);
 Alarm.delay(1000); // wait one second between clock display
 showAlarmTime(firstAlarm);
 showAlarmTime(secondAlarm);

}

void showAlarmTime(AlarmID_t id)
{
 time_t alarmTime = Alarm.read(id);
 if(alarmTime!= 0)
 {
   if( alarmTime <= SECS_PER_DAY)  
      Serial.print(" repeat alarm with ID ");
   else  
       Serial.print(" once only alarm with ID  ");
   Serial.print(id, DEC);    
   Serial.print(" set for ");
   digitalClockDisplay(alarmTime);
 }  
}


void digitalClockDisplay(time_t t)
{
 // digital clock display of the time
 Serial.print(hour(t));
 printDigits(minute(t));
 printDigits(second(t));
 Serial.println();
}

void printDigits(int digits)
{
 // utility function for digital clock display: prints preceding colon and leading 0
 Serial.print(":");
 if(digits < 10)
   Serial.print('0');
 Serial.print(digits);
}


the output will look like this:
Code: [Select]
8:29:40
repeat alarm with ID 0 set for 8:30:00
repeat alarm with ID 1 set for 17:45:00


(The time and companion TimeAlarms libraries can be downloaded from here: http://www.arduino.cc/playground/Code/Time)

gagnon_eric

Thanks for the quick answer   :)
I will try that tonight at home.

Eric

Go Up