Pages: 1 [2] 3   Go Down
Author Topic: clock with multiple alarms  (Read 4832 times)
0 Members and 1 Guest are viewing this topic.
Indonesia
Offline Offline
Newbie
*
Karma: 1
Posts: 49
ayooo,,,DAB!!!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
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
   
   
2. 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)
   
Quote
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
Code:
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();
}

Quote
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.
« Last Edit: July 25, 2011, 10:01:49 am by copet_pitik » Logged

Program Studi Instrumentasi Medis
Politeknik Mekatronika Sanata Dharma
Yogyakarta Indonesia

London
Offline Offline
Tesla Member
***
Karma: 10
Posts: 6255
Have fun!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
Code:
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
Code:
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:
Code:
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.

Logged

Indonesia
Offline Offline
Newbie
*
Karma: 1
Posts: 49
ayooo,,,DAB!!!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

still got the problems,mem..same problem.
I don't know the problems is on my code, algorithm, or me  smiley-roll-blue
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:
Code:

 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:

Code:
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 - downloaded 27 times.)
* Serial Monitor datas 1.txt (1.72 KB - downloaded 19 times.)
* Serial Monitor datas 2.txt (2.06 KB - downloaded 11 times.)
Logged

Program Studi Instrumentasi Medis
Politeknik Mekatronika Sanata Dharma
Yogyakarta Indonesia

London
Offline Offline
Tesla Member
***
Karma: 10
Posts: 6255
Have fun!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

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




Logged

Indonesia
Offline Offline
Newbie
*
Karma: 1
Posts: 49
ayooo,,,DAB!!!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

this problems is handled, just call setup again.. smiley-roll-blue
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. smiley
thanks for all the discussions..
Logged

Program Studi Instrumentasi Medis
Politeknik Mekatronika Sanata Dharma
Yogyakarta Indonesia

Offline Offline
Newbie
*
Karma: 0
Posts: 14
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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 you just had to call the setup again
from where? and how axactly?

forgive me to open up this oldie.

marco
Logged

Indonesia
Offline Offline
Newbie
*
Karma: 1
Posts: 49
ayooo,,,DAB!!!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

i think i just added
only this line of code:
Code:
...
setup()
...

but, i forget where i placed the code, just try it. sorry..  smiley-roll-sweat

i hope i wiil figure it out soon
Logged

Program Studi Instrumentasi Medis
Politeknik Mekatronika Sanata Dharma
Yogyakarta Indonesia

Offline Offline
Newbie
*
Karma: 0
Posts: 14
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Code:
#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);
  }
}
« Last Edit: July 22, 2014, 08:06:39 am by pepadew » Logged

Indonesia
Offline Offline
Newbie
*
Karma: 1
Posts: 49
ayooo,,,DAB!!!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Program Studi Instrumentasi Medis
Politeknik Mekatronika Sanata Dharma
Yogyakarta Indonesia

Indonesia
Offline Offline
Newbie
*
Karma: 1
Posts: 49
ayooo,,,DAB!!!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

you need to change the int to time_t in SetAlarm routine and disable each alarm ID
hope this help
Code:
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 (13.8 KB, 630x440 - viewed 6 times.)
« Last Edit: July 23, 2014, 12:12:45 pm by copet_pitik » Logged

Program Studi Instrumentasi Medis
Politeknik Mekatronika Sanata Dharma
Yogyakarta Indonesia

Offline Offline
Newbie
*
Karma: 0
Posts: 14
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Code:
#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);
  }
}
« Last Edit: July 24, 2014, 09:08:32 pm by pepadew » Logged

Indonesia
Offline Offline
Newbie
*
Karma: 1
Posts: 49
ayooo,,,DAB!!!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.

Code:
#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);
  }
}

}
« Last Edit: July 25, 2014, 01:21:08 am by copet_pitik » Logged

Program Studi Instrumentasi Medis
Politeknik Mekatronika Sanata Dharma
Yogyakarta Indonesia

Offline Offline
Newbie
*
Karma: 0
Posts: 14
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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 smiley-sad

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.
Logged

Indonesia
Offline Offline
Newbie
*
Karma: 1
Posts: 49
ayooo,,,DAB!!!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset


Quote
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 smiley-sad
i am sorry, i left my board on my lab.
i hope i can check it soon.

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

Logged

Program Studi Instrumentasi Medis
Politeknik Mekatronika Sanata Dharma
Yogyakarta Indonesia

Offline Offline
Newbie
*
Karma: 0
Posts: 14
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.

what im currently working on is getting the alarms set via serial.

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
Logged

Pages: 1 [2] 3   Go Up
Jump to: