clock with multiple alarms

can i digg up this old post to ask on how u actually solved it? i used your sketch and adapted it so i can understand it(im a beginner). in the end i run into the problem that if i send a set time command I+24ascii chars i run into the exact problem ur having at the end of the thread. since im a beginner i would love to hear the steps to solve the wrong serial data comming in. you were saying [u]you just had to call the setup again[/u] from where? and how axactly?

forgive me to open up this oldie.

marco

I am so sorry, actually I can't exactlly remember how i solved it.

i think i just added only this line of code:

...
setup()
...

but, i forget where i placed the code, just try it. sorry.. :cold_sweat:

i hope i wiil figure it out soon

the problem occurs not when i input this into serial data input : I083008320001083108330002.
this sets alarms 0, 1 and 2 to 08:30 on 08:32 off with interval of 1 minute
and sets alarm 3, 4, and 5 to 08:31on 08:33 off with 2 minutes interval.
this works fine.

but when i do this I174117430001174217440002
it all goes wrong.
this sets alarm times wich are not valid and are handled wrong by the serial.

i put up the code i have so far.

#include <Time.h>
#include <TimeAlarms.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);
  setTime(8,29,40,1,1,10); // set time to 8:29:40am Jan 1 2010 
  if(timeStatus()!= timeSet) 
    Serial.println("Time isn't synced! ");
  else
    Serial.println(" time is synced!  ");
  delay(1000);
    //ROOM 1
  alarmStart1 = Alarm.alarmRepeat(20, 27, 00, Start1); //==start alarm mode interval slave 1
  alarmStop1   = Alarm.alarmRepeat(20, 29, 00, Stop1); //==stop alarm mode interval slave 1
  alarmTimer1 =  Alarm.timerRepeat( 1 * SECS_PER_MIN, doRing1 );
  Alarm.disable(alarmTimer1);
  //ROOM 2
  alarmStart2 = Alarm.alarmRepeat(20, 28, 00, Start2); //==start alarm mode interval slave 2
  alarmStop2   = Alarm.alarmRepeat(20, 30, 00, 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)
   {
   showAlarmTime(alarmStart1);
   showAlarmTime(alarmStop1);
   showAlarmTime(alarmTimer1);
   showAlarmTime(alarmStart2);
   showAlarmTime(alarmStop2);
   showAlarmTime(alarmTimer2);;//processSyncMessage();
   }
    else if (header == INT_SLAVE) 
      getNewAlarmTimesl();
  }
  time_t t = now();
  digitalClockDisplay(t);
  
  Alarm.delay(1000); // wait one second between clock display
}

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)
{
  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 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 == 2)
  {    
     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);
   
}

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);
   Serial.println (newStart);
   Serial.println (newStop);
   Serial.println (newInterval);
   //================================================================room 2
   
   newStart = AlarmHMS (hourStart2, minuteStart2, 0);
   newStop  = AlarmHMS (hourStop2, minuteStop2, 30);
   newInterval = AlarmHMS (hourInterval2, minuteInterval2, 0);
   setAlarm(2, newStart, newStop, newInterval);
   Serial.println (newStart);
   Serial.println (newStop);
   Serial.println (newInterval);
}

int getIntFromAscii(int nbrDigits)
{
  int val = 0;
  for(int i=0; i < nbrDigits; i++) {
    val = (val *10) + Serial.read()- '0';
  }
  return  val;
}

/*==================== ALARM ROOM 1, START and STOP =================*/
//==================================================================================
void Start1(){
  digitalWrite(13, HIGH);
  Serial.println("Licht = Aan");
  delay(1000);
}
void Stop1(){
  digitalWrite(13, LOW);
  Serial.println("Licht = Uit");
  delay(1000);
}

void doRing1(){
  Serial.println("interval = 1");
  delay(1000);
}
/*==================== ALARM ROOM 2, START and STOP =================*/
//==================================================================================
void Start2(){
  Alarm.enable(alarmTimer2);
  Serial.println("Pomp = Aan");
  delay(1000);
}
void Stop2(){
  Alarm.disable(alarmTimer2);
  Serial.println("Pomp = Uit");
  delay(1000);
}

void doRing2(){
  Serial.print("Interval = 2");
  delay(1000);
}

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);
  }
}

i am sorry for the delayed response.. i will try to check my code again and hope i can solve the problem..

that code is wrong for some reason. 1. if you send a serial data, it will trigger the alarm for rooom 1 and room 2. 2. new alarm activation is failed. the serial handling routine is work just fine, but when it is failed to set the new alarm time.

i think, the problem is how to enable and disable the alarms, and how to set the new alarm time.

you need to change the int to time_t in SetAlarm routine and disable each alarm ID
hope this help

void setAlarm( time_t alarmNumber, time_t yukstart, time_t yukstop, time_t yukinterval)
{
  if(alarmNumber == 1)
  {    
    Alarm.write (alarmStart1, yukstart);
    Alarm.disable (alarmStart1);
    Alarm.write (alarmStop1, yukstop);
    Alarm.disable (alarmStop1);
    Alarm.write (alarmTimer1, yukinterval);
    Alarm.disable (alarmTimer1);
  }
  else if(alarmNumber == 2)
  {    
    Alarm.write (alarmStart2, yukstart);
    Alarm.disable (alarmStart2);
    Alarm.write (alarmStop2, yukstop);
    Alarm.disable (alarmStop2);
    Alarm.write (alarmTimer2, yukinterval);    
    Alarm.disable (alarmTimer2);
  }

  Serial.print("Seting alarm ");
  Serial.println(alarmNumber);
  Serial.print("Start:  ");
  digitalClockDisplay(yukstart);
  Serial.print("Stop:  ");
  digitalClockDisplay(yukstop);
  Serial.print("Interval :  ");
  digitalClockDisplay(yukinterval);

}

2014-07-24 00_09_47-COM1.png

thank you so much!
that working ok now.
heres the next problem…
the alarms don’t trigger.
but when i do not change the alarm time and just upload new sketch and let it flow, then it will trigger/execute.

if been looking trough the code below for the past day but couldn’t figure it out.
maybe somebody has an idea when looking at it
i also work on arduino yun wich has wifi and access to internet to getting time from it sould be pretty easy.
also couldnt get that to work.

#include <Time.h>
#include <TimeAlarms.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);
  setTime(8,29,40,1,1,10); // set time to 8:29:40am Jan 1 2010 
  if(timeStatus()!= timeSet) 
    Serial.println("Time isn't synced! ");
  else
    Serial.println(" time is synced!  ");
  delay(1000);
    //ROOM 1
  alarmStart1 = Alarm.alarmRepeat(20, 27, 00, Start1); //==start alarm mode interval slave 1
  alarmStop1   = Alarm.alarmRepeat(20, 29, 00, Stop1); //==stop alarm mode interval slave 1
  alarmTimer1 =  Alarm.timerRepeat( 1 * SECS_PER_MIN, doRing1 );
  Alarm.disable(alarmTimer1);
  //ROOM 2
  alarmStart2 = Alarm.alarmRepeat(20, 28, 00, Start2); //==start alarm mode interval slave 2
  alarmStop2   = Alarm.alarmRepeat(20, 30, 00, 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)
   {
   showAlarmTime(alarmStart1);
   showAlarmTime(alarmStop1);
   showAlarmTime(alarmTimer1);
   showAlarmTime(alarmStart2);
   showAlarmTime(alarmStop2);
   showAlarmTime(alarmTimer2);;//processSyncMessage();
   }
    else if (header == INT_SLAVE) 
      getNewAlarmTimesl();
  }
  time_t t = now();
  digitalClockDisplay(t);
  
  Alarm.delay(1000); // wait one second between clock display
}

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)
{
  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 setAlarm( time_t alarmNumber, time_t start, time_t kstop, time_t interval)
{
  if(alarmNumber == 1)
  {    
    Alarm.write (alarmStart1, start);
    Alarm.disable (alarmStart1);
    Alarm.write (alarmStop1, kstop);
    Alarm.disable (alarmStop1);
    Alarm.write (alarmTimer1, interval);
    Alarm.disable (alarmTimer1);
  }
  else if(alarmNumber == 2)
  {    
    Alarm.write (alarmStart2, start);
    Alarm.disable (alarmStart2);
    Alarm.write (alarmStop2, kstop);
    Alarm.disable (alarmStop2);
    Alarm.write (alarmTimer2, interval);    
    Alarm.disable (alarmTimer2);
  }

  Serial.print("Seting alarm ");
  Serial.println(alarmNumber);
  Serial.print("Start:  ");
  digitalClockDisplay(start);
  Serial.print("Stop:  ");
  digitalClockDisplay(kstop);
  Serial.print("Interval :  ");
  digitalClockDisplay(interval);

}
   


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);
   Serial.println (newStart);
   Serial.println (newStop);
   Serial.println (newInterval);
   //================================================================room 2
   
   newStart = AlarmHMS (hourStart2, minuteStart2, 0);
   newStop  = AlarmHMS (hourStop2, minuteStop2, 30);
   newInterval = AlarmHMS (hourInterval2, minuteInterval2, 0);
   setAlarm(2, newStart, newStop, newInterval);
   Serial.println (newStart);
   Serial.println (newStop);
   Serial.println (newInterval);
}

int getIntFromAscii(int nbrDigits)
{
  int val = 0;
  for(int i=0; i < nbrDigits; i++) {
    val = (val *10) + Serial.read()- '0';
  }
  return  val;
}

/*==================== ALARM ROOM 1, START and STOP =================*/
//==================================================================================
void Start1(){
  digitalWrite(13, HIGH);
  Serial.println("Licht = Aan");
  delay(1000);
}
void Stop1(){
  digitalWrite(13, LOW);
  Serial.println("Licht = Uit");
  delay(1000);
}

void doRing1(){
  Serial.println("interval = 1");
  delay(1000);
}
/*==================== ALARM ROOM 2, START and STOP =================*/
//==================================================================================
void Start2(){
  Alarm.enable(alarmTimer2);
  Serial.println("Pomp = Aan");
  delay(1000);
}
void Stop2(){
  Alarm.disable(alarmTimer2);
  Serial.println("Pomp = Uit");
  delay(1000);
}

void doRing2(){
  Serial.print("Interval = 2");
  delay(1000);
}

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);
  }
}

You are welcome.
i dont know about arduino Yun.,
but i guess you shoud see mem’s example about how to set time via NTP Server. Network Time Protocol.
NTP server uses Unix Time Format. So, you have to understand how to convert Unix Time to hh:mm:ss format.

try this code. i guess because i disable the alarmStart1, stop1, start12, and stop2.

#include <Time.h>
#include <TimeAlarms.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);
  setTime(8,29,40,1,1,10); // set time to 8:29:40am Jan 1 2010 
  if(timeStatus()!= timeSet) 
    Serial.println("Time isn't synced! ");
  else
    Serial.println(" time is synced!  ");
  delay(1000);
    //ROOM 1
  alarmStart1 = Alarm.alarmRepeat(20, 27, 00, Start1); //==start alarm mode interval slave 1
  alarmStop1   = Alarm.alarmRepeat(20, 29, 00, Stop1); //==stop alarm mode interval slave 1
  alarmTimer1 =  Alarm.timerRepeat( 1 * SECS_PER_MIN, doRing1 );
  Alarm.disable(alarmTimer1);
  //ROOM 2
  alarmStart2 = Alarm.alarmRepeat(20, 28, 00, Start2); //==start alarm mode interval slave 2
  alarmStop2   = Alarm.alarmRepeat(20, 30, 00, 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)
   {
   showAlarmTime(alarmStart1);
   showAlarmTime(alarmStop1);
   showAlarmTime(alarmTimer1);
   showAlarmTime(alarmStart2);
   showAlarmTime(alarmStop2);
   showAlarmTime(alarmTimer2);;//processSyncMessage();
   }
    else if (header == INT_SLAVE) 
      getNewAlarmTimesl();
  }
  time_t t = now();
  digitalClockDisplay(t);
  
  Alarm.delay(1000); // wait one second between clock display
}

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)
{
  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 setAlarm( time_t alarmNumber, time_t start, time_t kstop, time_t interval)
{
  if(alarmNumber == 1)
  {    
    Alarm.write (alarmStart1, start);
    Alarm.write (alarmStop1, kstop);
    Alarm.write (alarmTimer1, interval);
    Alarm.disable (alarmTimer1);
  }
  else if(alarmNumber == 2)
  {    
    Alarm.write (alarmStart2, start);
    Alarm.write (alarmStop2, kstop);
    Alarm.write (alarmTimer2, interval);    
    Alarm.disable (alarmTimer2);
  }

  Serial.print("Seting alarm ");
  Serial.println(alarmNumber);
  Serial.print("Start:  ");
  digitalClockDisplay(start);
  Serial.print("Stop:  ");
  digitalClockDisplay(kstop);
  Serial.print("Interval :  ");
  digitalClockDisplay(interval);

}
   


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);
   Serial.println (newStart);
   Serial.println (newStop);
   Serial.println (newInterval);
   //================================================================room 2
   
   newStart = AlarmHMS (hourStart2, minuteStart2, 0);
   newStop  = AlarmHMS (hourStop2, minuteStop2, 30);
   newInterval = AlarmHMS (hourInterval2, minuteInterval2, 0);
   setAlarm(2, newStart, newStop, newInterval);
   Serial.println (newStart);
   Serial.println (newStop);
   Serial.println (newInterval);
}

int getIntFromAscii(int nbrDigits)
{
  int val = 0;
  for(int i=0; i < nbrDigits; i++) {
    val = (val *10) + Serial.read()- '0';
  }
  return  val;
}

/*==================== ALARM ROOM 1, START and STOP =================*/
//==================================================================================
void Start1(){
  digitalWrite(13, HIGH);
  Serial.println("Licht = Aan");
  delay(1000);
}
void Stop1(){
  digitalWrite(13, LOW);
  Serial.println("Licht = Uit");
  delay(1000);
}

void doRing1(){
  Serial.println("interval = 1");
  delay(1000);
}
/*==================== ALARM ROOM 2, START and STOP =================*/
//==================================================================================
void Start2(){
  Alarm.enable(alarmTimer2);
  Serial.println("Pomp = Aan");
  delay(1000);
}
void Stop2(){
  Alarm.disable(alarmTimer2);
  Serial.println("Pomp = Uit");
  delay(1000);
}

void doRing2(){
  Serial.print("Interval = 2");
  delay(1000);
}

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);
  }
}

}

when i set the alarm via serial monitor, the alarms still do not go off. when i do not edit sketch and just upload ur code above the alarms do trigger. same problem as my post above :(

also is it possible to set 1 intervals? i want to be able to set 1 to run a pin high for like 15/30 minutes. and then another interval to get 75/90 minutes of pin low.

when i set the alarm via serial monitor, the alarms still do not go off. when i do not edit sketch and just upload ur code above the alarms do trigger. same problem as my post above :(

i am sorry, i left my board on my lab. i hope i can check it soon.

also is it possible to set 1 intervals? i want to be able to set 1 to run a pin high for like 15/30 minutes. and then another interval to get 75/90 minutes of pin low.

it is the same pin?can you explain the detail what do you want to do?

im currently making a hydroponics grow controller that in the end will be controlled via a website hosted on my yun.

plant do not take up water via rootsystem when its dark. so when the lichts are on from 12:00 tot 00:00 i wat the pump to be on for 15 minutes en then turn it off for 45 minutes. that means i have 12 cycles when lights are on.

[u]what im currently working on is getting the alarms set via serial.[/u]

i already have humidity, 2x ds18b20, reporting to serial monitor when temp gets to 29C fans turn on, when the hit 29C again they go off aswell as i have a 4 relay board connected im getting time from yuns NTP server

what im planning on next is: ph sensor, lux sensor, and water level sensor

the problem before it is caused by enable/disable the timer.
the another problem shows up: clearing the previously set alarms, so from this post:

Noch:
When set Timer get their AlarmID_t like this:

AlarmID_t aTimer = Alarm.timerRepeat(10, RepeatTest);

and then when you want to unschedule just disable/free (I am not test what is happening if you not disable before free it but for safe just disable and free) like this:

Alarm.disable(aTimer);
Alarm.free(aTimer);

we have to disable and set the alarm free.

hope this code below works for you

#include <Time.h>
#include <TimeAlarms.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=====================
//error when I send I174117430001174217440002 --FIxed 
// I174117430001174217440002
// I083008320001083108330002
// I082708300001083108330002
// I083508380001083608400002

void setup()
{
  Serial.begin(9600);
  //  setSyncProvider(RTC.get);   // the function to get the time from the RTC
  //  setSyncInterval(600);
  // setTime(8,29,40,1,1,10); // set time to 8:29:40am Jan 1 2010 
  setTime(8,26,40,1,1,10);
  if(timeStatus()!= timeSet) 
    Serial.println("Time isn't synced! ");
  else
    Serial.println(" time is synced!  ");
  delay(1000);
  //ROOM 1
  alarmStart1 = Alarm.alarmRepeat(8, 27, 00, Start1); //==start alarm mode interval slave 1
  alarmStop1  = Alarm.alarmRepeat(8, 29, 30, Stop1); //==stop alarm mode interval slave 1
  alarmTimer1 = Alarm.timerRepeat( 1 * SECS_PER_MIN, doRing1 );
  Alarm.disable(alarmTimer1);
  //ROOM 2
  alarmStart2 = Alarm.alarmRepeat(8, 27, 10, Start2); //==start alarm mode interval slave 2
  alarmStop2  = Alarm.alarmRepeat(8, 29, 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)
    {
      showAlarmTime(alarmStart1);
      showAlarmTime(alarmStop1);
      showAlarmTime(alarmTimer1);
      showAlarmTime(alarmStart2);
      showAlarmTime(alarmStop2);
      showAlarmTime(alarmTimer2);
      //processSyncMessage();
    }
    else if (header == INT_SLAVE) 
      getNewAlarmTimesl();
  }
  time_t t = now();
  digitalClockDisplay(t);
  Alarm.delay(1000); // wait one second between clock display
}

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)
{
  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 setAlarm( time_t alarmNumber, time_t start, time_t stop, time_t interval)
{
  if(alarmNumber == 1)
  {    
    Alarm.disable (alarmStart1);
    Alarm.disable (alarmStop1);
    Alarm.disable (alarmTimer1);
    Alarm.free (alarmStart1);
    Alarm.free (alarmStop1);
    Alarm.free (alarmTimer1);
    alarmStart1 = Alarm.alarmRepeat(start, Start1);
    alarmStop1  = Alarm.alarmRepeat(stop, Stop1); 
    alarmTimer1 = Alarm.timerRepeat( interval, doRing1);
    Alarm.disable (alarmTimer1);
  }
  else if(alarmNumber == 2)
  {    
    Alarm.disable (alarmStart2);
    Alarm.disable (alarmStop2);
    Alarm.disable (alarmTimer2);
    Alarm.free (alarmStart2);
    Alarm.free (alarmStop2);
    Alarm.free (alarmTimer2);
    alarmStart2 = Alarm.alarmRepeat(start, Start2);
    alarmStop2  = Alarm.alarmRepeat(stop, Stop2); 
    alarmTimer1 = Alarm.timerRepeat( interval, doRing2); 
    Alarm.disable (alarmTimer2);
  }

  Serial.print("Seting alarm ");
  Serial.println(alarmNumber);
  Serial.print("Start:  ");
  digitalClockDisplay(start);
  Serial.print("Stop:  ");
  digitalClockDisplay(stop);
  Serial.print("Interval :  ");
  digitalClockDisplay(interval);

}

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);
  Serial.println (hourStart1);
  Serial.println (minuteStart1);
  Serial.println (hourStart2);
  Serial.println (minuteStart2);

  //================================================================room 2

  newStart = AlarmHMS (hourStart2, minuteStart2, 0);
  newStop  = AlarmHMS (hourStop2, minuteStop2, 30);
  newInterval = AlarmHMS (hourInterval2, minuteInterval2, 0);
  setAlarm(2, newStart, newStop, newInterval);
  Serial.println (hourStart1);
  Serial.println (minuteStart1);
  Serial.println (hourStart2);
  Serial.println (minuteStart2);
  //setup();
}

int getIntFromAscii(int nbrDigits)
{
  int val = 0;
  for(int i=0; i < nbrDigits; i++) {
    val = (val *10) + Serial.read()- '0';
  }
  return  val;
}

/*==================== 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.println("2222~t w o~2222 ");
  delay(1000);
}

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);
  }  
}

plant do not take up water via rootsystem when its dark. so when the lichts are on from 12:00 tot 00:00
i wat the pump to be on for 15 minutes en then turn it off for 45 minutes. that means i have 12 cycles when lights are on.

you can use many alarm IDs to enable/disable your pin. here’s the idea (use Alarm.alarmRepeat):
alarm_ID_1: 12.01 - 12.15 → enable pin D3
alarm_ID_2: 12.16 - 13.00 → disable pin D3
alarm_ID_3: 13.01 - 13.15 → enable pin D3
alarm_ID_4: 13.16 - 14.00 → disable pin D3


alarm_ID_XX: 23.01 - 23.15 → enable pin D3
alarm_ID_XX: 23.16 - 00.00 → disable pin D3

to make the alarm ID more that 6, mem’s said

You can change the number of alarms by changing the value of dtNbr_ALARMS in TimeAlarms.h as follows:

#define dtNBR_ALARMS XX

XX=number of alarms you need

maybe other member can help to make it simpler.

thatnks thats working ok now.
now i have the other intervals working also
with Alarm.timerOnce()
this takes input in seconds.
is there a possibility that it takes minutes instead.

also i tried setting these Alarm.timerOnce(), via serial mon without luck.

below is a(part)of the code i tried.

#define PJG_DATA 9
#define INTERVAL_HDR 'I'
byte minuteStart3 = 0;
byte minuteStop3 = 0;

AlarmID_t alarmIntervalaan, alarmIntervaluit;




void setup()
{

  alarmIntervaluit = Alarm.timerOnce(1*60 pompintervalAan);             // called once after 1 minute 
  alarmIntervalaan = Alarm.timerOnce(1*60*2 pompintervalUit);             // called once after 2 minutes 
  Alarm.disable(alarmIntervalaan);
  Alarm.disable(alarmIntervaluit);
}
void  loop(){

    if (header == INTERVAL_HDR) {
      getNewAlarmTimesPINTERVAL();
    }
}


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

  //===========for ROOM 1===========
  minuteStart3 = getIntFromAscii(2);
  minuteStop3 = getIntFromAscii(2);
  //================================================================room 1
  time_t newStart,newStop;  
  
  newStart = AlarmHMS (0, minuteStart3, 0);
  newStop  = AlarmHMS (0, minuteStop3, 0);
  setAlarmPINTERVAL(1, newStart, newStop);
  Serial.println (minuteStart3);
  Serial.println (minuteStop3);
}


void setAlarmPINTERVAL( time_t alarmNumber, const int MStart, const int MStop)
{
  if(alarmNumber == 4)
  {    
    Alarm.disable (alarmIntervaluit);
    Alarm.free (alarmIntervaluit);
    alarmIntervalaan  = Alarm.timerOnce(MStop, pompintervalUit); 

  }
  if(alarmNumber == 5)
  {    
    Alarm.disable (alarmIntervalaan);
    Alarm.free (alarmIntervalaan);
    alarmIntervaluit = Alarm.timerOnce(MStart, pompintervalAan);

  }

  Serial.print("Seting alarm ");
  Serial.println(alarmNumber);
  Serial.print("Start:  ");
  digitalClockDisplay(MStart);
  Serial.print("Stop:  ");
  digitalClockDisplay(MStop);

}

how you send it (IXXXXXX)? and what is for? why the alarmNumber == 4?

why Mstart and Mstop in integer?

void setAlarmPINTERVAL( time_t alarmNumber, const int MStart, const int MStop)

is there a possibility that it takes minutes instead.

yes. my code also take minute as input, just try to adapted from it.

hello again,

i have got it working. now next problem. maybe you could help me
i have a website were i can put in data and store it into a string(msg).
i have tried to set the alarm times from that line but cant get the handeling right.

this is my code

#include <Bridge.h>
#include <YunServer.h>
#include <YunClient.h>
#include <TimeAlarms.h>
#include <Time.h>


YunServer server;
String msg;

byte hourStart1 = 0;
byte minuteStart1 = 0;
byte hourStop1 = 0;
byte minuteStop1 = 0;

AlarmID_t alarmStart1, alarmStop1; //ID for interval licht

void setup () {

  Serial.begin(9600);
  Bridge.begin();
  server.listenOnLocalhost();
  server.begin();

  setTime(8,26,40,1,1,10);
  if(timeStatus()!= timeSet) 
    Serial.println("Time isn't synced! ");
  else
    Serial.println(" time is synced!  ");
  delay(1000);
  
  alarmStart1 = Alarm.alarmRepeat(8, 27, 00, lichtStart); //==start alarm mode interval slave 1
  alarmStop1  = Alarm.alarmRepeat(8, 29, 30, lichtStop); //==stop alarm mode interval slave 1
}

void loop () {
  //*********Read new message from the client**************
  YunClient client = server.accept(); //check new clients
   
  if(client) {
    String command = client.readStringUntil('/');  //read the incoming data
    if (command == "sd") { 
      msg = client.readStringUntil('/');             // read the incoming data
    
     getalarms();
    }
    client.stop();  
  } 
    time_t t = now();
  digitalClockDisplay(t);
  Alarm.delay(1000); // wait one second between clock display
   
   
}

void getalarms(){
  //===========for ROOM 1===========
  hourStart1 = getIntFromAscii(2);
  minuteStart1 = getIntFromAscii(2);
  hourStop1 = getIntFromAscii(2);
  minuteStop1 = getIntFromAscii(2);




  //================================================================room 1
  time_t newStart,newStop,newInterval;  


  newStart = AlarmHMS (hourStart1, minuteStart1, 0);
  newStop  = AlarmHMS (hourStop1, minuteStop1, 0);
  setAlarm(1, newStart, newStop);
  Serial.println (hourStart1);
  Serial.println (minuteStart1);

}
 int getIntFromAscii(int nbrDigits)
{
    YunClient client = server.accept(); //check new clients

  int val = 0;
  for(int i=0; i < nbrDigits; i++) {
    val = (val *10) + client.read()- '0';
  }
  return  val;
} 


void setAlarm( time_t alarmNumber, time_t start, time_t stop)
{
  if(alarmNumber == 1)
  {    
    Alarm.disable (alarmStart1);
    Alarm.disable (alarmStop1);
    Alarm.free (alarmStart1);
    Alarm.free (alarmStop1);
    alarmStart1 = Alarm.alarmRepeat(start, lichtStart);
    alarmStop1  = Alarm.alarmRepeat(stop, lichtStop); 

  }


  Serial.print("Seting alarm ");
  Serial.println(alarmNumber);
  Serial.print("Start:  ");
  digitalClockDisplay(start);
  Serial.print("Stop:  ");
  digitalClockDisplay(stop);

}
void lichtStart(){
  Serial.println("LICHT AAN");
  digitalWrite(13, HIGH);
  delay(1000);
}
void lichtStop(){
  Serial.println("LICHT UIT");
  digitalWrite(13, LOW);
  delay(1000);
}

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)
{
  Serial.print(":");
  if(digits < 10)
    Serial.print('0');
  Serial.print(digits);
}

it doesnt matter what i put in as data i always get this output white the sketch above.

8:34:25
8:34:26
8:34:27
8:34:28
8:34:29
8:34:30
Seting alarm 1
Start:  16:49:00
Stop:  16:49:00
229
229
8:34:32
8:34:33
8:34:34
8:34:35
8:34:36
8:34:37

i did 12341234

check your ‘val’ variable using serial.print
does the val variable has the same with the number that you sent?.

 int getIntFromAscii(int nbrDigits)
{
    YunClient client = server.accept(); //check new clients

  int val = 0;
  for(int i=0; i < nbrDigits; i++) {
    val = (val *10) + client.read()- '0';
[u] Serial.println (val);[/u]
  }
  return  val;
}

when i do that i can see that the val is having wrong value.

-49
-539
-49
-539
-49
-539
-49
-539

could you maybe help me find were the problem is and maybe how to fix that?

above is serial monitor output when i give it a time like 12341234

pepadew: could you maybe help me find were the problem is and maybe how to fix that?

above is serial monitor output when i give it a time like 12341234

i dont know how to fix that, sorry. :sweat_smile: try to check yor msg variable using serial.print. if the serial output gives the right value, then you need to adapted the working code to set the alarm

the msg string gives me the right value/output on the serial monitor.

do i change the value of the string to ints? and then assign the ints to the correct position(eg hourstart1, minutestart1, hourstop1, minutestop1). thats were i get stuck. maybe you could point me to an tutorial?

pepadew: the msg string gives me the right value/output on the serial monitor.

do i change the value of the string to ints? and then assign the ints to the correct position(eg hourstart1, minutestart1, hourstop1, minutestop1). thats were i get stuck. maybe you could point me to an tutorial?

i am so sorry, i am afraid i cant help.