clock with multiple alarms

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

i want to make a clock using :

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

and using this library :
#include <Time.h>
#include <TimeAlarms.h>
#include <Wire.h>
#include <DS1307RTC.h>
#include <EEPROM.h>
#include <LiquidCrystal.h>

the purpose of my sketch :

  1. Read Serial data VB from VB to set RTC if Serial Header is ‘T’ => achieved
    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 :
    I180018050001180318080001
  • then save :
  • //room 1
    • 18 to EEPROM address 200 ==> alarm start hour //room 1
    • 00 to EEPROM address 201 ==> alarm start minute //room 1
    • 18 to EEPROM address 202 ==> alarm stop hour //room 1
    • 05 to EEPROM address 203 ==> alarm stop minute //room 1
    • 00 to EEPROM address 204 ==> alarm interval hour //room 1
    • 01 to EEPROM address 205 ==> alarm interval minute //room 1
  • //room 2
    • 18 to EEPROM address 206 ==> alarm start hour //room 2
    • 03 to EEPROM address 207 ==> alarm start minute //room 2
    • 18 to EEPROM address 208 ==> alarm stop hour //room 2
    • 08 to EEPROM address 209 ==> alarm stop minute //room 2
    • 00 to EEPROM address 210 ==> alarm interval hour //room 2
    • 01 to EEPROM address 211 ==> alarm interval minute //room 2
  • desired output:
  • alarm in room 1 ring every 1 minutes since 18.00 up to 18.05
  • alarm in room 2 ring every 1 minutes since 18.03 up to 18.08

I have asked my problems on this thread:

my progress is on that thread, as summary, my sketch only handle 1 room alarm

and my next step is design 2 alarm for 2 room that can have different “start, stop, and interval” alarm value.
the example application is clock in industry,
clock for staff (from 08.00 up to 14.00 = room 1), clock for machine operator (8 hour shift/day = room 2)

So, i figure out the code will be the same as mem’s fragment before (http://arduino.cc/forum/index.php/topic,66054.msg485651.html#msg485651), but something wrong when I run it.

and this is part of my sketch:

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

AlarmID_t alarm1; 
AlarmID_t alarm2;

void setup()
{
  Serial.begin(9600);
  lcd.begin(16,2);
  setSyncProvider(RTC.get);   // the function to get the time from the RTC
  setSyncInterval(600);
  lcd.clear();
  //room 1
  Alarm.alarmRepeat(18,00,00, Start1);  //start room 1
  Alarm.alarmRepeat(18,05,30, Stop1);  // End room 1
  alarm1 = Alarm.timerRepeat( 1 * SECS_PER_MIN, doRING1); 
  //room 2
  Alarm.alarmRepeat(18,03,30, Start2);  //start room 2
  Alarm.alarmRepeat(18,08,30, EndIntSlave2);  // End room 2
  alarm2 = Alarm.timerRepeat( 1 * SECS_PER_MIN, doRING2);  
}
void Start1()
{
  Alarm.enable(alarm1); //room 1 start alarm
}

void Stop1()
{
  Alarm.disable(alarm1);//room 1 stop alarm
}

void doRING1()
{
  lcd.print("1111111111111111");//room 1 interval
}
void Start2()
{
  Alarm.enable(alarm2);//room 2 start alarm
}

void Stop2()
{
  Alarm.disable(alarm2);//room 2 stop alarm
}

void doRING2()
{
  lcd.print("2222222222222222");//room 2 interval
}

...
loop, etc

and here’s the output :
18.00.00 : room 1, start the alarm = as desired
18.00.50 : alarm 2 is ringing ======>>>> undesired output
18.01.00 : alarm 1 is ringing = as desired
18.01.50 : alarm 2 is ringing ======>>>> undesired output
18.02.00 : alarm 1 is ringing = as desired
18.02.50 : alarm 2 is ringing ======>>>> undesired output
. . .
18.03.30 : room 2, start the alarm = as desired
18.04.30 : alarm 2 is ringing = as desired
18.05.30 : room 1, stop the alarm = as desired
. . .
10.08.30 : room 2, stop the alarm = as desired

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

you need to disable the timers until the alarm has triggered. Change the setup code as follows:

  //room 1
  Alarm.alarmRepeat(18,00,00, Start1);  //start room 1
  Alarm.alarmRepeat(18,05,30, Stop1);  // End room 1
  alarm1 = Alarm.timerRepeat( 1 * SECS_PER_MIN, doRING1); 
  Alarm.disable(alarm1); // <-this timer will be enabled when the alarm triggers
  //room 2
  Alarm.alarmRepeat(18,03,30, Start2);  //start room 2
  Alarm.alarmRepeat(18,08,30, EndIntSlave2);  // End room 2
  alarm2 = Alarm.timerRepeat( 1 * SECS_PER_MIN, doRING2);  
  Alarm.disable(alarm2); // <- this timer will be enabled when the alarm triggers

everything else looks ok but I have not tried it so let me know if that fixes it

Okay, previous problem solved.
your code works, mem. thanks again.

another problems,
if have place the code (Alarm.alarmRepeat…etc) on setup,
so when i send the alarm time from VB and save it to EEPROM, then I have to reset the arduino, right?
what should I do if I don’t want to reset the arduino?

thanks for any suggestions or helps

.cop.

You don’t need to reset the Arduino, you can simply write the new alarm time to the alarm

You will need to add alarm IDs for all of the alarms you want to change - here is an example of how to do this:

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

// variables holding the IDs for alarms and timers that can be set from VB
AlarmID_t alarm1Start, alarm1Stop, alarm1Timer;  
AlarmID_t alarm2tart, alarm2top, alarm2Timer;

void setup()
{
  Serial.begin(9600);
  lcd.begin(16,2);
  setSyncProvider(RTC.get);   // the function to get the time from the RTC
  setSyncInterval(600);
  lcd.clear();

 //room 1
  alarm1Start = Alarm.alarmRepeat(18,00,00, Start1);  //start room 1
  alarm1Stop = Alarm.alarmRepeat(18,05,30, Stop1);  // End room 1
  alarm1Timer = Alarm.timerRepeat( 1 * SECS_PER_MIN, doRING1); 
  Alarm.disable(alarm1Timer); // <-this timer will be enabled when the alarm triggers

  //room 2
  alarm2Start = Alarm.alarmRepeat(18,03,30, Start2);  //start room 2
  alarm2Start = Alarm.alarmRepeat(18,08,30, EndIntSlave2);  // End room 2
  alarm2Timer = Alarm.timerRepeat( 1 * SECS_PER_MIN, doRING2);  
  Alarm.disable( alarm2Timer); // <- this timer will be enabled when the alarm triggers  
}

void Start1()
{
  Alarm.enable(alarm1Timer); //room 1 start alarm
}

void Stop1()
{
  Alarm.disable(alarm1Timer);//room 1 stop alarm
}

void doRING1()
{
  lcd.print("1111111111111111");//room 1 interval
}
void Start2()
{
  Alarm.enable(alarm2Timer);//room 2 start alarm
}

void Stop2()
{
  Alarm.disable(alarm2Timer);//room 2 stop alarm
}

void doRING2()
{
  lcd.print("2222222222222222");//room 2 interval
}

You change the alarm time by calling alarm.write with the alarm id you want to change and the new alarm value.
here is some pseudocode:

time_t newTime = AlarmHMS(17, 30,00); // variable newTime contains the time value for 17:30:00
Alarm.write(alarm1Start,newTime); // change the alarm1 start time to the given newTime

my code is not working, must be something wrong with it.

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

AlarmID_t alarm1Start, alarm1Stop, alarm1Timer;  
AlarmID_t alarm2Start, alarm2Stop, alarm2Timer;

void setup()
{
  Serial.begin(9600);
  lcd.begin(16,2);
  setSyncProvider(RTC.get);   // the function to get the time from the RTC
  setSyncInterval(600);
  lcd.clear();

 //room 1
  alarm1Start = Alarm.alarmRepeat(18,00,00, Start1);  //start room 1
  alarm1Stop  = Alarm.alarmRepeat(18,05,30, Stop1);  // End room 1
  alarm1Timer = Alarm.timerRepeat( 1 * SECS_PER_MIN, doRING1); 
  Alarm.disable(alarm1Timer); // <-this timer will be enabled when the alarm triggers

  //room 2
  alarm2Start = Alarm.alarmRepeat(18,03,30, Start2);  //start room 2
  alarm2Stop  = Alarm.alarmRepeat(18,08,30, EndIntSlave2);  // End room 2
  alarm2Timer = Alarm.timerRepeat( 1 * SECS_PER_MIN, doRING2);  
  Alarm.disable( alarm2Timer); // <- this timer will be enabled when the alarm triggers  
}

void loop()
{
  Alarm.delay(0);
  if(Serial.available())
  {
    char header = Serial.read();
    if (header == TIME_HEADER) //serial data to set the RTC
      processSyncMessage();
    else if (header == INT_SLAVE) //serial data to set the alarm
      changeValue();						//if serial available, change The Alarm Value
    else if (header == OTHERS)
      BlinkBlink();
  }
  digitalClockDisplay();
  delay(1000);
}

void changeValue()
{
	...//EEPROM.write address 200 - 211
	...
	...
		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
  		char i = EEPROM.read (206); //hourEEPROM          ==>2//START HOUR
  		char j = EEPROM.read (207); //minuteEEPROM        ==>2//START MINUTE
  		char k = EEPROM.read (208); //hourEEPROM          ==>2//STOP HOUR
  		char l = EEPROM.read (209); //minuteEEPROM        ==>2//STOP MINUTE
  		char m = EEPROM.read (210); //hourEEPROM          ==>2//INTERVAL HOUR
  		char n = EEPROM.read (211); //minuteEEPROM        ==>2//INTERVAL MINUTE
  
  		//================================================================room 1
 		time_t newStart1 = AlarmHMS (c, d, 0);
  		Alarm.write (alarm1Start, newStart1); 			// change START alarm value ROOM 1
  		time_t newEnd1   = AlarmHMS (e, f, 30);
  		Alarm.write (alarm1End, newEnd1);			// change STOP alarm value ROOM 1
  		time_t newInterval1 = AlarmHMS (g, h, 0);
  		Alarm.write (alarm1Timer, newInterval1) ;		// change TIMER value ROOM 1
 		//================================================================room 2
 		time_t newStart2 = AlarmHMS (i, j, 10);
 		Alarm.write (alarm2Start, newStart2);			// change START alarm value ROOM 2
 		time_t newEnd2   = AlarmHMS (k, l, 30);
 		Alarm.write (alarm2End, newEnd2);			// change STOP alarm value ROOM 2
 		time_t newInterval2 = AlarmHMS (m, n, 0);
 		Alarm.write (alarm2Timer, newInterval2) ;		// change TIMER value ROOM 2
}

It is right, mem?
I’m sorry, I am totally noob for this stuff. =( =( =(

you will find things will go easier of you take one step at a time.

As I suggested in the other thread, don't worry about eeprom until after you have everything else working.

The next step is to ensure that you are handling the serial messages correctly. If you print the values of variables c through n, to the serial port, are they correct? if not, you need to fix your serial input routines. If they hour minutes and interval are being received correctly then you need to say exactly what it is that is not working correctly.

mem:
The next step is to ensure that you are handling the serial messages correctly. If you print the values of variables c through n, to the serial port, are they correct?
if not, you need to fix your serial input routines. If they hour minutes and interval are being received correctly then you need to say exactly what it is that is not working correctly.

this code (below)on void setup() is working,
room 1 alarm’s rings every minute since 18:00:00 until 18:05:30
room 2 alarm’s rings every minute since 18:03:30 until 18:08:30

  //room 1
  Alarm.alarmRepeat(18,0,00, Start1);  //start room 1
  Alarm.alarmRepeat(18,5,30, Stop1);  // End room 1
  alarm1 = Alarm.timerRepeat( 1 * SECS_PER_MIN, doRING1); 
  Alarm.disable(alarm1); // <-this timer will be enabled when the alarm triggers
  //room 2
  Alarm.alarmRepeat(18,3,30, Start2);  //start room 2
  Alarm.alarmRepeat(18,8,30, EndIntSlave2);  // End room 2
  alarm2 = Alarm.timerRepeat( 1 * SECS_PER_MIN, doRING2);  
  Alarm.disable(alarm2); // <- this timer will be enabled when the alarm triggers

So, my next step is trying to combine your code in post #3 with my EEPROM sketch, but it is not working.
the undesired output are:
when I send serial data from VB at 18:03:15 (RTC time), both alarm will ring at 18:04:15 and won’t stop.

the value c through n is correct when i print to serial.
this is the code that I used:

  Serial.println (i, DEC);
  Serial.println (j, DEC);
  Serial.println (k, DEC);
  Serial.println (l, DEC);

I’ll make and try another sketches to change the value of the alarm.

If you post the sketch that prints values from the serial requests and sets the alarms that has all the eeprom code removed I will see I can help. Please say what data you are sending and how the sketch is misbehaving.

:disappointed_relieved: :disappointed_relieved: :disappointed_relieved:
it is not working again.
I’ve removed my eeprom code and still got undesired output.
this is my my sketch (this sketch using RTC library):

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

//Serial data parsing RTC Function
#define TIME_MSG_LEN  11   
#define TIME_HEADER  'T'   
//===========end============
//Serial data parsing for ALARM
#define PJG_DATA 25
#define INT_SLAVE 'I'
//===========end============

//==Serial Variable for ROOM 1==
byte hourStart1 = 0;
byte minuteStart1 = 0;
byte hourStop1 = 0;
byte minuteStop1 = 0;
byte hourInterval1 = 0;
byte minuteInterval1 = 0;
//==Serial Variable for ROOM 2==
byte hourStart2 = 0;
byte minuteStart2 = 0;
byte hourStop2 = 0;
byte minuteStop2 = 0;
byte hourInterval2 = 0;
byte minuteInterval2 = 0;
//===========end===========

//==================ALARM ID==================
AlarmID_t alarmStart1, alarmStop1, alarmTimer1; //ID for interval Slave 1
AlarmID_t alarmStart2, alarmStop2, alarmTimer2; //ID for interval Slave 2
//====================end=====================

void setup()
{
  Serial.begin(9600);
  setSyncProvider(RTC.get);   // the function to get the time from the RTC
  setSyncInterval(600);
  if(timeStatus()!= timeSet) 
    Serial.println("Can't sync RTC! ");
  else
    Serial.println(" Can sync RTC!  ");
  delay(1000);
  //ROOM 1
  alarmStart1 = Alarm.alarmRepeat(22, 40, 00, Start1); //==start alarm mode interval slave 1
  alarmStop1   = Alarm.alarmRepeat(22, 42, 30, Stop1); //==stop alarm mode interval slave 1
  alarmTimer1 =  Alarm.timerRepeat( 1 * SECS_PER_MIN, doRing1 );
  Alarm.disable(alarmTimer1);
  //ROOM 2
  alarmStart2 = Alarm.alarmRepeat(22, 41, 10, Start2); //==start alarm mode interval slave 2
  alarmStop2   = Alarm.alarmRepeat(22, 43, 30, Stop2); //==stop alarm mode interval slave 2
  alarmTimer2 =  Alarm.timerRepeat( 2 * SECS_PER_MIN, doRing2 );
  Alarm.disable(alarmTimer2);
}

void  loop(){
  if(Serial.available())
  {
    char header = Serial.read();
    if (header == TIME_HEADER)
      processSyncMessage();
    else if (header == INT_SLAVE) 
      Interval();
  }
  digitalClockDisplay();
  Alarm.delay(1000); // wait one second between clock display
}

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

void printDigits(int digits)
{
  Serial.print(":");
  if(digits < 10)
    Serial.print('0');
  Serial.print(digits);
}

//========================reading UNIX TIME==========================//
void processSyncMessage()
{
  time_t pctime = 0;
  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') ;  
    }
  }
  time_t t = pctime;
  RTC.set(t);
  setTime(t);
}
//=========CHANGE ALARM data from SERIAL MESSAGE =========//
void Interval()
{
  char setString[PJG_DATA];
  int index = 0;
  for (int i = 0; i < PJG_DATA - 1; i++)
  {
    char d = Serial.read();
    setString[index++] = d - '0';
  }
  int count = index;
  int element = 0;
  for( index = 0; index < count; index += 2)
  {
    byte val = setString[index] * 10 + setString[index+1];
    switch (element++){
      //===========for ROOM 1===========
    case 0:
      hourStart1 = val;
      break;
    case 1:
      minuteStart1 = val;
      break;
    case 2:
      hourStop1 = val;
      break;
    case 3:
      minuteStop1 = val;
      break;
    case 4:
      hourInterval1 = val;
      break;
    case 5:
      minuteInterval1 = val;
      break;
      //===========for ROOM 2===========
    case 6:
      hourStart2 = val;
      break;
    case 7:
      minuteStart2 = val;
      break;
    case 8:
      hourStop2 = val;
      break;
    case 9:
      minuteStop2 = val;
      break;
    case 10:
      hourInterval2 = val;
      break;
    case 11:
      minuteInterval2 = val;
      break;
    }
  }
  //================================================================room 1
  time_t newStart1 = AlarmHMS (hourStart1, minuteStart1, 0);
  Alarm.write (alarmStart1, newStart1);
  time_t newStop1   = AlarmHMS (hourStop1, minuteStop1, 30);
  Alarm.write (alarmStop1, newStop1);
  time_t newInterval1 = AlarmHMS (hourInterval1, minuteInterval1, 0);
  Alarm.write (alarmTimer1, newInterval1) ;
  //================================================================room 2
  time_t newStart2 = AlarmHMS (hourStart2, minuteStart2, 10);
  Alarm.write (alarmStart2, newStart2);
  time_t newStop2   = AlarmHMS (hourStop2, minuteStop2, 30);
  Alarm.write (alarmStop2, newStop2);
  time_t newInterval2 = AlarmHMS (hourInterval2, minuteInterval2, 0);
  Alarm.write (alarmTimer2, newInterval2) ;
  //check alarm data ROOM 1
  Serial.println(hourStart1, DEC);
  Serial.println(minuteStart1, DEC);
  Serial.println(hourStop1, DEC);
  Serial.println(minuteStop1, DEC);
  Serial.println(hourInterval1, DEC);
  Serial.println(minuteInterval1, DEC);
  //check alarm data ROOM 2
  Serial.println(hourStart2, DEC);
  Serial.println(minuteStart2, DEC);
  Serial.println(hourStop2, DEC);
  Serial.println(minuteStop2, DEC);
  Serial.println(hourInterval2, DEC);
  Serial.println(minuteInterval2, DEC);
}
/*==================== ALARM ROOM 1, START and STOP =================*/
//==================================================================================
void Start1(){
  Alarm.enable(alarmTimer1);
  Serial.println("ROOM 1111111 START");
  delay(1000);
}
void Stop1(){
  Alarm.disable(alarmTimer1);
  Serial.println("ROOM 1111111 STOP");
  delay(1000);
}

void doRing1(){
  Serial.println("^^^^^^one^^^^^^");
  delay(1000);
}
/*==================== ALARM ROOM 2, START and STOP =================*/
//==================================================================================
void Start2(){
  Alarm.enable(alarmTimer2);
  Serial.println("ROOM 2222222 START");
  delay(1000);
}
void Stop2(){
  Alarm.disable(alarmTimer2);
  Serial.println("ROOM 2222222 STOP");
  delay(1000);
}

void doRing2(){
  Serial.print("2222~t w o~2222 ");
  delay(1000);
}

what data you are sending?

I send this data : I225122530001225222540002
ROOM 1 : start alarm at : 22:51, stop the alarm at : 22:53, and alarm’s interval : 1 minute
ROOM 2 : start alarm at : 22:52, stop the alarm at : 22:54, and alarm’s interval : 2 minute

this is the output of serial data parsing:

Can sync RTC! 
22:50:51
22:50:52
22
51
22
53
0
1
22
52
22
54
0
2
22:50:53
22:50:54
22:50:55
22:50:56

how the sketch is misbehaving?

the alarm is not working or show that alarm for room 1/2 is start/stop.
and alarm’s timer is start counting (and never ends) since received the data ( I225122530001225222540002, arduino received data on 20:20:53)

this is the misbehaving output from Serial Monitor:

...
22:51:51
22:51:52
^^^^^^one^^^^^^
22:51:54
22:51:55
...
...
22:52:51
22:52:52
^^^^^^one^^^^^^
2222~t w o~2222 22:52:55
22:52:56
22:52:57
...
...
22:53:51
22:53:52
^^^^^^one^^^^^^22:53:54
22:53:55
22:53:56
...
...
22:54:51
22:54:52
^^^^^^one^^^^^^
2222~t w o~2222 22:54:55
22:54:56
22:54:57
...
...
22:55:51
22:55:52
^^^^^^one^^^^^^22:55:54
22:55:55
22:55:56
...
...
22:56:51
22:56:52
^^^^^^one^^^^^^
2222~t w o~222222:56:55
22:56:56
22:56:57
...
...
...
...

from this output I make a conclusion that the alarm start counting when arduino received serial data (input).
thanks for any helps.

.cop.

Incorrect question asked.

JoeO, nothings wrong when I compiled that sketch, maybe that is the alternate syntax in arduino, but I'm not sure..hehehe :%.. thanks JoeO..

Sorry - you are using a correct form of IfElse.

You are reading the serial port without first checking to ensure that all 24 characters have been received.

I modified your Interval() function to do the the check and have simplified the implementation by converting the received digits into integer values on the fly instead of stroring the characters in an array.

I have not tested this and you may need to tweak this but I hope this gets you going in the right direction.

// returns the integer value of the given number of ascii digits
int getIntFromAscii(int nbrDigits)
{
  int val = 0;
  for(int i=0; i < nbrDigits; i++) {
    val = (val *10) + Serial.read()- '0';
  }
  return  val;
}

//=========CHANGE ALARM data from SERIAL MESSAGE =========//
void Interval()
{
  unsigned long startTime = millis();
  while(Serial.available() < 24)
  {
    if(millis() - startTime > 1000)
    {
       Serial.println("got header but timed out waiting for 24 bytes");
       return;
    }
  }    

   //===========for ROOM 1===========
   hourStart1 = getIntFromAscii(2);
   minuteStart1 = getIntFromAscii(2);
   hourStop1 = getIntFromAscii(2);
   minuteStop1 =getIntFromAscii(2);
   hourInterval1 = getIntFromAscii(2);
   minuteInterval1 =getIntFromAscii(2);
   //===========for ROOM 2===========
   hourStart2 = getIntFromAscii(2);
   minuteStart2 = getIntFromAscii(2);
   hourStop2 = getIntFromAscii(2);
   minuteStop2 = getIntFromAscii(2);
   hourInterval2 = getIntFromAscii(2);
   minuteInterval2 = getIntFromAscii(2)

  //================================================================room 1
 the code to set the alarms and print the values goes here...

Mem, still not getting any this time.. the optimized code is working, but alarm can not start or stop at the time as I set. the serial output is same as my earlier post.

still tinkering and tweaking more.. :roll_eyes: :roll_eyes: :roll_eyes:


2nd try: I check the alarm ID 0 up to 5, the result is the alarms have been set as what I send in serial input data that I send : I185118530001185218540002

18:50:46
 repeat alarm with ID 0 set for 18:51:00
 repeat alarm with ID 1 set for 18:53:30
 repeat alarm with ID 2 set for 0:01:00
 repeat alarm with ID 3 set for 18:52:10
 repeat alarm with ID 4 set for 18:54:30
 repeat alarm with ID 5 set for 0:02:00
18:50:47
18:50:48
...
...

and I am still get the alarm ring every 1 and 2 minutes. this is the serial monitor output:

18:51:46
^^^^^^one^^^^^^
18:51:48
...
...
18:52:46
2222~t w o~2222 18:52:48
^^^^^^one^^^^^^
18:52:49
...
...
...

I am not clear on what is not working, how are you checking to make sure that the time you are setting are correct?

If you change the setup values to the times you are sending in your message, does the sketch work as expected?

I am not clear on what is not working,

my problems are :

  1. I want to set the value of start alarm, stop alarm, and interval alarm from serial message,
    but when I finished sending the serial data, the alarms won’t start, stop or RINGING at the time that I’ve set.
    example :
    I send serial message : I185118530001185218540002 ( this data only contains hour and minute values)
    So,
    ROOM 1 :
    Alarm with ID “alarmStart1” is set to 18:51:00 (second value is set from the sketch, is not set from serial message)
    Alarm with ID “alarmStop1” is set to 18:53:30 (second value is set from the sketch, is not set from serial message)
    Alarm with ID “alarmTimer1” is set to ring every 1 minutes (0001==>HHMM)

ROOM 2 :
Alarm with ID “alarmStart2” is set to 18:52:10 (second value is set from the sketch, is not set from serial message)
Alarm with ID “alarmStop1” is set to 18:54:30 (second value is set from the sketch, is not set from serial message)
Alarm with ID “alarmTimer1” is set to ring every 2 minutes (0002==>HHMM)

my goals are : (and this is what I said not working)
alarm ROOM 1 will ring at 18:52:00 and 18:53:00
alarm ROOM 2 will only ring at 18:54:10

  1. If I send serial message (I185118530001185218540002) when serial monitor shows “18:50:46”.
    alarm for ROOM 1 will ring at 18:51:47, 18:52:47, 18:53:47, 18:54:47, … (won’t stop)
    alarm for ROOM 2 will ring at 18:52:47, 18:54:47, … (won’t stop)

how are you checking to make sure that the time you are setting are correct?

I saw your post (here) about how to read alarm.
this is my code for interval(), showAlarmTime(), and digitalClockDisplay1()[different with digitalClockDisplay()] Functions

void Interval()
{
  unsigned long startTime = millis();
  while(Serial.available() < 24)
  {
    if(millis() - startTime > 1000)
    {
       Serial.println("got header but timed out waiting for 24 bytes");
       return;
    }
  }    

   //===========for ROOM 1===========
   hourStart1 = getIntFromAscii(2);
   minuteStart1 = getIntFromAscii(2);
   hourStop1 = getIntFromAscii(2);
   minuteStop1 = getIntFromAscii(2);
   hourInterval1 = getIntFromAscii(2);
   minuteInterval1 = getIntFromAscii(2);
   
   //===========for ROOM 2===========
   hourStart2 = getIntFromAscii(2);
   minuteStart2 = getIntFromAscii(2);
   hourStop2 = getIntFromAscii(2);
   minuteStop2 = getIntFromAscii(2);
   hourInterval2 = getIntFromAscii(2);
   minuteInterval2 = getIntFromAscii(2);
   
   //================================================================room 1
   time_t newStart1 = AlarmHMS (hourStart1, minuteStart1, 0);
   Alarm.write (alarmStart1, newStart1);
   time_t newStop1   = AlarmHMS (hourStop1, minuteStop1, 30);
   Alarm.write (alarmStop1, newStop1);
   time_t newInterval1 = AlarmHMS (hourInterval1, minuteInterval1, 0);
   Alarm.write (alarmTimer1, newInterval1);
   
   //================================================================room 2
   time_t newStart2 = AlarmHMS (hourStart2, minuteStart2, 10);
   Alarm.write (alarmStart2, newStart2);
   time_t newStop2   = AlarmHMS (hourStop2, minuteStop2, 30);
   Alarm.write (alarmStop2, newStop2);
   time_t newInterval2 = AlarmHMS (hourInterval2, minuteInterval2, 0);
   Alarm.write (alarmTimer2, newInterval2);
   
   //check alarm data ROOM 1
   Serial.println(hourStart1, DEC);
   Serial.println(minuteStart1, DEC);
   Serial.println(hourStop1, DEC);
   Serial.println(minuteStop1, DEC);
   Serial.println(hourInterval1, DEC);
   Serial.println(minuteInterval1, DEC);
   
   //check alarm data ROOM 2
   Serial.println(hourStart2, DEC);
   Serial.println(minuteStart2, DEC);
   Serial.println(hourStop2, DEC);
   Serial.println(minuteStop2, DEC);
   Serial.println(hourInterval2, DEC);
   Serial.println(minuteInterval2, DEC);
   
   showAlarmTime(alarmStart1);
   showAlarmTime(alarmStop1);
   showAlarmTime(alarmTimer1);
   showAlarmTime(alarmStart2);
   showAlarmTime(alarmStop2);
   showAlarmTime(alarmTimer2);
}

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 ");
    digitalClockDisplay1(alarmTime);
  }  
}


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

If you change the setup values to the times you are sending in your message, does the sketch work as expected?

Yes, i’ve tried it.

it will be easier to identify what is wrong if you break your functions down into smaller units.

For example, separate your interval function gets serial data and sets the alarms. if you separate them you can test each part on its own.

here is a new function that sets alarms:

void setAlarm( int alarmNumber, int start, int stop, int interval)
{
  if(alarmNumber == 1)
  {    
     Alarm.write (alarmStart1, start);
     Alarm.write (alarmStop1, stop);
     Alarm.write (alarmTimer1, interval);    
   }
   else if(alarmNumber == 1)
  {    
     Alarm.write (alarmStart2, start);
     Alarm.write (alarmStop2, stop);
     Alarm.write (alarmTimer2, interval);    
   }
   
   Serial.print("Seting alarm ");
   Serial.println(alarmNumber);
   Serial.print("Start:  ");
   digitalClockDisplay(start);
   Serial.print("Stop:  ");
   digitalClockDisplay(stop);
   Serial.print("Interval :  ");
   digitalClockDisplay(interval);
}

here is the old interval function renamed to getNewAlarmTimes, this calls the function above to actually set the alarm

void getNewAlarmTimesl() 
{  
  unsigned long startTime = millis();
  while(Serial.available() < 24)
  {
    if(millis() - startTime > 1000)
    {
       Serial.println("got header but timed out waiting for 24 bytes");
       return;
    }
  }    

   //===========for ROOM 1===========
   hourStart1 = getIntFromAscii(2);
   minuteStart1 = getIntFromAscii(2);
   hourStop1 = getIntFromAscii(2);
   minuteStop1 = getIntFromAscii(2);
   hourInterval1 = getIntFromAscii(2);
   minuteInterval1 = getIntFromAscii(2);
   
   //===========for ROOM 2===========
   hourStart2 = getIntFromAscii(2);
   minuteStart2 = getIntFromAscii(2);
   hourStop2 = getIntFromAscii(2);
   minuteStop2 = getIntFromAscii(2);
   hourInterval2 = getIntFromAscii(2);
   minuteInterval2 = getIntFromAscii(2);
   
   //================================================================room 1
   time_t newStart,newStop,newInterval;  

   newStart = AlarmHMS (hourStart1, minuteStart1, 0);
   newStop  = AlarmHMS (hourStop1, minuteStop1, 30);
   newInterval = AlarmHMS (hourInterval1, minuteInterval1, 0);
   setAlarm(1, newStart, newStop, newInterval);
   
   //================================================================room 2
   
   newStart = AlarmHMS (hourStart2, minuteStart2, 0);
   newStop  = AlarmHMS (hourStop2, minuteStop2, 30);
   newInterval = AlarmHMS (hourInterval2, minuteInterval2, 0);
   setAlarm(2, newStart, newStop, newInterval);     
}

if your digitalClockDIsplay function does not take a paramater, use this one:

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

Let me know If the printed alarm times are as expected but the alarms are not starting or stopping correctly, if not we can write a test sketch that uses setLarm to set some hard coded times and see if we can get that working.

still got the problems,mem…same problem.
I don’t know the problems is on my code, algorithm, or me :roll_eyes:
but, when I tried our code in post #16,
when I send serial data that the hour < 9 (ex: I083008320001083108330002) the alarm is set, but the won’t ring at the time that I set.
If I send serial data that the hour >= 9 (ex: I174117430001174217440002) the alarms is misbehaving : the alarm is not set as the data that I sent
and not ring at the time that I set.

then, i check each alarm ID and find something misbehaving…I don’t really know why.
this is what i got:

serial data input : I083008320001083108330002
output:

 time is synced!  
8:29:41						//I send T to read all alarm IDs
 repeat alarm with ID 0 set for 20:27:00	// the alarm with ID 0-5 is set from setup() Function
 repeat alarm with ID 1 set for 20:29:30
 repeat alarm with ID 2 set for 0:01:00
 repeat alarm with ID 3 set for 20:28:10
 repeat alarm with ID 4 set for 20:30:30
 repeat alarm with ID 5 set for 0:02:00
8:29:42
8:29:43
8:29:44
8:29:45
8:29:46
8:29:47
8:29:48
8:29:49
8:29:50
8:29:51						//I send this data I083008320001083108330002
Seting alarm 1
Start:  8:30:00		//just as I expected				
Stop:  8:32:30		//just as I expected
Interval :  0:01:00		//just as I expected
30600	//Serial.print on Function getNewAlarmTimesl()  for variable "newStart" : hour = 8, minute = 30 [(8*3600)+(30*60)=30600]  
30750	//Serial.print on Function getNewAlarmTimesl() for variable "newStop"  : hour = 8, minute = 32
60	//Serial.print on Function getNewAlarmTimesl() for variable "newInterval" : hour = 0, minute = 1
Seting alarm 2
Start:  8:31:00		//just as I expected
Stop:  8:33:30		//just as I expected
Interval :  0:02:00		//just as I expected
30660
30810
120
8:29:52
8:29:53
8:29:54
8:29:55
8:29:56
8:29:57						//I send T to make sure that the alarms
 repeat alarm with ID 0 set for 8:30:00		
 repeat alarm with ID 1 set for 8:32:30		// alarms with ID 0-5 as I expected
 repeat alarm with ID 2 set for 0:01:00	
 repeat alarm with ID 3 set for 8:31:00		
 repeat alarm with ID 4 set for 8:33:30		
 repeat alarm with ID 5 set for 0:02:00		
8:29:58
8:29:59
8:30:00

serial data input : I174117430001174217440002
output:

 time is synced!  
8:29:41
8:29:42						//I send T to read all alarm IDs
 repeat alarm with ID 0 set for 20:27:00	// the alarm with ID 0-5 is set from setup() Function
 repeat alarm with ID 1 set for 20:29:30
 repeat alarm with ID 2 set for 0:01:00
 repeat alarm with ID 3 set for 20:28:10
 repeat alarm with ID 4 set for 20:30:30
 repeat alarm with ID 5 set for 0:02:00
8:29:43
8:29:44
8:29:45
8:29:46
8:29:47
8:29:48
8:29:49
8:29:50
8:29:51						//I send this data I174117430001174217440002
Seting alarm 1
Start:  5:57:00		//Expected value at : 17:41:00
Stop:  5:59:30		//Expected value at : 17:43:30
Interval :  0:01:00		//just as I expected
63660		//Serial.print on Function getNewAlarmTimesl()  for variable "newStart" : hour = 17, minute = 41 [(17*3600)+(41*60)=63660]  
63810		//Serial.print on Function getNewAlarmTimesl()  for variable "newStart" : hour = 17, minute = 43
60		//Serial.print on Function getNewAlarmTimesl()  for variable "newStart" : hour = 0, minute = 1
Seting alarm 2
Start:  5:58:00		//Expected value at : 17:42:00
Stop:  6:00:30		//Expected value at : 17:434:30
Interval :  0:02:00		//just as I expected
63720
63870
120
8:29:52
8:29:53
8:29:54
8:29:55
8:29:56
8:29:57
8:29:58
8:29:59
8:30:00
8:30:01						//I send T to make sure that the alarms
 once only alarm with ID  0 set for 5:57:00		//it should be repeat alarm. and have different value with data that I sent
 once only alarm with ID  1 set for 5:59:30		//it should be repeat alarm. and have different value with data that I sent
 repeat alarm with ID 2 set for 0:01:00
 once only alarm with ID  3 set for 5:58:00		//it should be repeat alarm. and have different value with data that I sent
 once only alarm with ID  4 set for 6:00:30		//it should be repeat alarm. and have different value with data that I sent
 repeat alarm with ID 5 set for 0:02:00
8:30:02
8:30:03

I don’t really know how to explain this problems,
but I attach my sketch and my serial output data.
this sketch only contains Time and TimeAlarm library.

twoIntervalAlarms.pde (6.54 KB)

Serial Monitor datas 1.txt (1.72 KB)

Serial Monitor datas 2.txt (2.06 KB)

From you post it looks like you be setting alarm times that are not correct which would indicate a problem with your serial decoding

Start: 5:57:00 //Expected value at : 17:41:00 Stop: 5:59:30 //Expected value at : 17:43:30

if you wanted to send an alarm start time of 17:41 but actually got 5:57 then you need to focus on the serial message handling.

I suggest you write a test sketch that has no Alarm functionality - just interpret the values you get for start, stop and interval from the serial message see if they make sense. This will help you determine if the problem caused by giving TimerAlarm incorrect value or if the correct values are being set but the alarm library is not handling them as you expect.

this problems is handled, just call setup again.. :roll_eyes: the serial data is okay..now, it just work don't wanna be the perfect one..not enough time to figure it out.. if i have another questions, I hope get another helps, especially from you, mem. :) thanks for all the discussions..