how to set alarm of rtc clock by sending serial command to arduino

hi everyone !
actually i am working on a project in which i am stuck in one task ...and the task is to set the alarm time of rtc through my smart phone here is the code for this task ..
#include <Time.h>
#include <TimeAlarms.h>
#include <virtuabotixRTC.h>
virtuabotixRTC myRTC(6, 7, smiley-cool;
int led=13;
char h=8 ;
char m=30;
char s=0;
void setup()
{
Serial.begin(9600);
pinMode(led,OUTPUT);
setTime(8,0,0,16,7,14);
myRTC.setDS1302Time(00, 00, 8, 3, 14, 7, 2014);}
void loop()
{
myRTC.updateTime();

// Start printing elements as individuals
Serial.print("Current Date / Time: ");
Serial.print(myRTC.dayofmonth);
Serial.print("/");
Serial.print(myRTC.month);
Serial.print("/");
Serial.print(myRTC.year);
Serial.print(" ");
Serial.print(myRTC.hours);
Serial.print(":");
Serial.print(myRTC.minutes);
Serial.print(":");
Serial.println(myRTC.seconds);
Alarm.delay(1000); // wait one second between clock display
if (Serial.available() >0)
{ h= Serial.read(); //dont know how to make
m=Serial.read();// these three commands to
s=Serial.read();//work properly through proper programming command
Alarm.alarmRepeat(h,m,s, morningalarm); // alarm set through smartphone
}
else{
Serial.println('nothing have been sent');
}}
// functions to be called when an alarm triggers:
void morningalarm(){
Serial.println("Alarm: - turn lights off");
digitalWrite(led, HIGH);
}
plz do help me out in this ...how should i receive hour minute and seconds at a time serially and put them all together in alarm command so the alarm is set to a new time which is specified by the user through his smart phone

clk_alarm.ino (2.4 KB)

What exactly is sending the time? An App on the phone that you have written? If
so you have freedom to send the info in many formats. If not then you need to ascertain
the format used.

ash124:
plz do help me out in this ...how should i receive hour minute and seconds at a time serially and put them all together in alarm command so the alarm is set to a new time which is specified by the user through his smart phone

How is the smartphone going to communicate with the Arduino?

http://forum.arduino.cc/index.php?topic=148850.0

this might help (us)

step 7 is the key.

@ash124, do not cross-post. Duplicates removed.

PeterH .. i am sending command through smart phone using blueterm to arduino which is using bluetooth module B- HC06

Did you look at the TimeAlarmExample sketch?

I suggest you run that sketch to see how the alarms are set and activated. Then you can add code to receive alarm times from the serial monitor. When you have that working, you can replace serial monitor input with bluetooth. When you have that working, you can add in the RTC.

have fun!

i have tried this code to set the alarm but got error in this ..:frowning:
can i use this code to set alarm time

//Program

[size=10pt][size=10pt][size=10pt][size=10pt][size=10pt]#include <Time.h>
#include <TimeAlarms.h>
#include <virtuabotixRTC.h>   
virtuabotixRTC myRTC(6, 7, 8);
char requestBuffer[100];
int requestIdx=0;
int hour=8 ;
int minute=0;
int second=0;
int input;
void setup()
{
  Serial.begin(9600);
//  pinMode(led,OUTPUT);
 setTime(8,0,0,16,7,14); 
   myRTC.setDS1302Time(00, 00, 8, 3, 14, 7, 2014);}
void loop()
{
  myRTC.updateTime();   
 Serial.print("Current Date / Time: ");                                                                 
  Serial.print(myRTC.dayofmonth);                                                                        
  Serial.print("/");                                                                                     
  Serial.print(myRTC.month);                                                                              
  Serial.print("/");                                                                                     
  Serial.print(myRTC.year);                                                                               
  Serial.print("  ");                                                                                     
  Serial.print(myRTC.hours);                                                                              
  Serial.print(":");                                                                                      
  Serial.print(myRTC.minutes);                                                                         
  Serial.print(":");                                                                                      
Serial.println(myRTC.seconds);  
// Start printing elements as individuals                                                                   
                                                                        
  Alarm.delay(1000); // wait one second between clock display
//if (Serial.available() >0)
 hi ();
 Alarm.alarmRepeat(hour,minute,second, morningalarm);
}
void hi ()
{
   if(Serial.available()){  // extremely primitive command decoder
    char c = Serial.read();
    switch(c){
      case '\r':  // carriage return, process string
        requestBuffer[requestIdx++] = c;  // save the character
        requestBuffer[requestIdx] = '\0';  // terminate the request with a null so the various routines work
        Serial.println(requestBuffer);  // this just shows you what you have
        // now do something with the request string
        processRequest(requestBuffer, strlen(requestBuffer));
        //start collecting the next request string
        memset(requestBuffer,0,sizeof(requestBuffer));  //start off empty
        requestIdx = 0; // and at the beginning
        break;
      default:
        requestBuffer[requestIdx] = c;  // this saves it one character at a time
        if(requestIdx < sizeof(requestBuffer))  // if they typed in too many characters
          requestIdx++;
        else{  //the buffer filled up with garbage
          memset(requestBuffer,0,sizeof(requestBuffer));  //start off empty
          requestIdx = 0; // and at the beginning
        }
        break;
    }
  }

void processRequest(char *buf){
    hour  = strtok(buf, ",");  // get the first comma delimited item from the request
    minute = strtok(0, ",");    // and the next one, which should be month
    second = strtok(0,"," );
  }
void morningalarm(){
  
  Serial.println("Alarm: - turn lights off"); 
//  digitalWrite(led, HIGH);  
}
[/size][/size][/size][/size][/size]

// i got the following errors

set_alarm:7: error: 'int hour' redeclared as different kind of symbol
C:\Users\Aisha aslam\Documents\Arduino\libraries\Time/Time.h:100: error: previous declaration of 'int hour(time_t)'
set_alarm:8: error: 'int minute' redeclared as different kind of symbol
C:\Users\Aisha aslam\Documents\Arduino\libraries\Time/Time.h:108: error: previous declaration of 'int minute(time_t)'
set_alarm:9: error: 'int second' redeclared as different kind of symbol
C:\Users\Aisha aslam\Documents\Arduino\libraries\Time/Time.h:110: error: previous declaration of 'int second(time_t)'
set_alarm.ino: In function 'void loop()':
set_alarm:37: error: no matching function for call to 'TimeAlarmsClass::alarmRepeat(<unresolved overloaded function type>, <unresolved overloaded function type>, <unresolved overloaded function type>, void (&)())'
C:\Users\Aisha aslam\Documents\Arduino\libraries\TimeAlarms/TimeAlarms.h:69: note: candidates are: AlarmID_t TimeAlarmsClass::alarmRepeat(time_t, void (*)())
C:\Users\Aisha aslam\Documents\Arduino\libraries\TimeAlarms/TimeAlarms.h:70: note:                 AlarmID_t TimeAlarmsClass::alarmRepeat(int, int, int, void (*)())
C:\Users\Aisha aslam\Documents\Arduino\libraries\TimeAlarms/TimeAlarms.h:71: note:                 AlarmID_t TimeAlarmsClass::alarmRepeat(timeDayOfWeek_t, int, int, int, void (*)())
set_alarm.ino: In function 'void hi()':
set_alarm:9: error: too many arguments to function 'void processRequest(char*)'
set_alarm:49: error: at this point in file
set_alarm:66: error: a function-definition is not allowed here before '{' token
set_alarm:71: error: a function-definition is not allowed here before '{' token
set_alarm:75: error: expected `}' at end of input

Some variables in the code to read the RTC are conflicting with names used by the Time and TimeAlarms library. If you are new to writing arduino code then I suggest you simplify things and build up the functionality piece by piece to help you understand how the code works. My previous post has suggestions for the steps you can take to get things going.

Mem i have searched a lot and also tried different programs a lot to sort out this problem but i am not getting out of it . i have tried a new code plz check that can i use this code

Did you try a simple version based on the TimeAlarm example with code to set the alarm time from the serial monitor?

nop:(

If you are looking for someone to write the code for you then perhaps someone reading this thread will oblige. If you are seeking to create the application yourself, then the step-by-step approach suggested earlier is a good way to learn and make progress.

now i have a code to set the alarm but dont know why its not working .here is my code

[code][code]
#include <Time.h>
#include <TimeAlarms.h>
#include <virtuabotixRTC.h>   
virtuabotixRTC myRTC(6, 7, 8);
char requestBuffer[100];
int requestIdx=0;
int myhour=8 ;
int myminute=0;
int mysecond=0;
int myyear = 0;
int myday = 0;
int mymonth = 0;
int input;
void setup()
{
  Serial.begin(9600);
//  pinMode(led,OUTPUT);
 setTime(8,0,0,16,7,14); 
   myRTC.setDS1302Time(00, 00, 8, 3, 14, 7, 2014);}
void loop()
{
  myRTC.updateTime();   
 Serial.print("Current Date / Time: ");                                                                 
  Serial.print(myRTC.dayofmonth);                                                                        
  Serial.print("/");                                                                                     
  Serial.print(myRTC.month);                                                                              
  Serial.print("/");                                                                                     
  Serial.print(myRTC.year);                                                                               
  Serial.print("  ");                                                                                     
  Serial.print(myRTC.hours);                                                                              
  Serial.print(":");                                                                                      
  Serial.print(myRTC.minutes);                                                                         
  Serial.print(":");                                                                                      
Serial.println(myRTC.seconds);  
// Start printing elements as individuals                                                                   
                                                                        
  Alarm.delay(1000); // wait one second between clock display
//if (Serial.available() >0)
 hi ();
 Alarm.alarmRepeat(myhour,myminute,mysecond, morningalarm);
}
void hi ()
{
   if(Serial.available()){  // extremely primitive command decoder
     char c = Serial.read();
     switch(c){
       case '\r':  // carriage return, process string
         requestBuffer[requestIdx++] = c;  // save the character
         requestBuffer[requestIdx] = '\0';  // terminate the request with a null so the various routines work
         Serial.println(requestBuffer);  // this just shows you what you have
         // now do something with the request string
         processRequest(requestBuffer, strlen(requestBuffer));
         //start collecting the next request string
         memset(requestBuffer,0,sizeof(requestBuffer));  //start off empty
         requestIdx = 0; // and at the beginning
         break;
       default:
         requestBuffer[requestIdx] = c;  // this saves it one character at a time
         if(requestIdx < sizeof(requestBuffer))  // if they typed in too many characters
           requestIdx++;
         else{  //the buffer filled up with garbage
           memset(requestBuffer,0,sizeof(requestBuffer));  //start off empty
           requestIdx = 0; // and at the beginning
        }
        break;
      }
    }
}

void processRequest(char *buf, int len){
  // Remember, when you get the user input, it's in ascii.  In c++, there is no automatic
  // conversion to a number like other languages.  You have to get the ascii text and convert
  // it to an integer (or whatever) yourself.
  char *data;
  
  data  = strtok(buf, ",");  // get the first comma delimited item from the request
  myyear = atoi(data);
  data = strtok(0, ",");    // and the next one, which should be month
  mymonth = atoi(data);
  data = strtok(0, "," );
  mysecond = atoi(data);
}

void morningalarm(){
  
  Serial.println("Alarm: - turn lights off"); 
//  digitalWrite(led, HIGH);  
}

[/code][/code]

what should be sent actually from my cell phone so the alarm should be set to 8:30:00 like
myhour=08;
myminute=30;
mysecond=00;
Alarm.alarmRepeat(myhour,myminute,mysecond, morningalarm);

ash124:
now i have a code to set the alarm but dont know why its not working .here is my code

what should be sent actually from my cell phone so the alarm should be set to 8:30:00 like
myhour=08;
myminute=30;
mysecond=00;
Alarm.alarmRepeat(myhour,myminute,mysecond, morningalarm);

Yes, that would set the alarm. However, the alarms will only function when used with the companion Time library.

The example sketch for using an RTC with the Time library does not work with the RTC you have. However, you can set the time by calling the setTime function in the time library with the values obtained with your RTC, see:
setTime(hr,min,sec, day, month, yr);

To get the time library working with your RTC, try modifying the TimeSerial example sketch by replacing the call to
setSyncProvider( requestSync); //set function to call when sync required
with code to call setTime with the time elements from your RTC. When you have that working you can add in the alarm code.

hi mem!
i have tried the code for time serial and here is that code

/* 
#include <Time.h>  

#define TIME_HEADER  "T"   // Header tag for serial time sync message
#define TIME_REQUEST  7    // ASCII bell character requests a time sync message 

void setup()  {
  Serial.begin(9600);
  while (!Serial) ; // Needed for Leonardo only
  pinMode(13, OUTPUT);
  setSyncProvider( requestSync);  //set function to call when sync required
  Serial.println("Waiting for sync message");
}

void loop(){    
  if (Serial.available()) {
    digitalClockDisplay();
    //processSyncMessage();
  }
  if (timeStatus()!= timeNotSet) {
   
    Serial.println("aisha"); 
  }
  if (timeStatus() == timeSet) {
    digitalWrite(13, HIGH); // LED on if synced
  } else {
    digitalWrite(13, LOW);  // LED off if needs refresh
  }
      // digitalClockDisplay();
  delay(1000);
}

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

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


void processSyncMessage() {
  unsigned long pctime;
  const unsigned long DEFAULT_TIME = 1357041600; // Jan 1 2013

  if(Serial.find(TIME_HEADER)) {
     pctime = Serial.parseInt();
     if( pctime >= DEFAULT_TIME) { // check the integer is a valid time (greater than Jan 1 2013)
       setTime(pctime); // Sync Arduino clock to the time received on the serial port
     }
  }
}
//
time_t requestSync()
{
  Serial.write(TIME_REQUEST);  
  return 0; // the time will be sent later in response to serial mesg
}

but one thing i am not getting in this is that how should i give this time i-e T1357041600 or other time

and after that i have tried the code for alarm setting and here is that one

 #include <Time.h>
#include <TimeAlarms.h>
#include <virtuabotixRTC.h>   
virtuabotixRTC myRTC(6, 7, 8);
#define PJG_DATA 13
#define INT_SLAVE 'I'
byte hourStart1 = 0;
byte minuteStart1 = 0;
AlarmID_t alarmStart1; //ID for interval Slave 1
void setup()
{
  Serial.begin(9600);
   setTime(8,0,0,16,7,14); 
   myRTC.setDS1302Time(00, 00, 8, 3, 14, 7, 2014);
  alarmStart1= Alarm.alarmRepeat(22, 40, 00, Start1); //==start alarm mode interval slave 1
}

void  loop(){ 
   myRTC.updateTime();   

  if(Serial.available())
  {
    char header = Serial.read();
     if (header == INT_SLAVE) 
      Interval();}
       Serial.print("Current Date / Time: ");                                                                 
  Serial.print(myRTC.dayofmonth);                                                                        
  Serial.print("/");                                                                                     
  Serial.print(myRTC.month);                                                                              
  Serial.print("/");                                                                                     
  Serial.print(myRTC.year);                                                                               
  Serial.print("  ");                                                                                     
  Serial.print(myRTC.hours);                                                                              
  Serial.print(":");                                                                                      
  Serial.print(myRTC.minutes);                                                                         
  Serial.print(":");                                                                                      
Serial.println(myRTC.seconds);                                                                                                        
  Alarm.delay(1000);
  }
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++){
    case 0:
      hourStart1 = val;
      break;
    case 1:
      minuteStart1 = val;
      break;
    }
  }
  time_t newStart1 = AlarmHMS (hourStart1, minuteStart1, 0);
  Alarm.write (alarmStart1, newStart1);
  Serial.println(hourStart1, DEC);
  Serial.println(minuteStart1, DEC);
}
void Start1(){
  Serial.println("alarm time is going to set now...");
}

same problem in this code that what type of input should i give it , like i want to set the alarm time to 8:30 o'clock.. plz help me out in this..

but one thing i am not getting in this is that how should i give this time i-e T1357041600 or other time

There are a number of ways to set the time using the time library. The one you need to use with your RTC is mentioned in my previous post:

set the time by calling the setTime function in the time library with the values obtained with your RTC, see:
setTime(hr,min,sec, day, month, yr);

To get this code to work, remove the setSyncProvider function call and add in code to read the time elements from you RTC and then use them to call setTime.

Hi mem!
i am trying to fix all the problems which i am encountring in my code to set alarm time with your help ...one thing which i am not understanding is that
T1357041600
i know t is the time header but what does rest of mean ?? i mean 1357041600 stands for what and how ??i know these are ascii character but how this is equal to Jan 1 1970

Mem !
i have successfully set the time of my RTC clock with your help and guidance ... now i have to set the alarm time which is probably most important task for me i have send you the code yesterday now i am sending it again

#include <Time.h>
#include <TimeAlarms.h>
#include <virtuabotixRTC.h>   
virtuabotixRTC myRTC(6, 7, 8);
#define PJG_DATA 11
#define INT_SLAVE 'I'
byte hourStart1 = 0;
byte minuteStart1 = 0;
AlarmID_t alarmStart1; //ID for interval Slave 1
void setup()
{
  Serial.begin(9600);
   setTime(8,0,0,23,7,14); 
   myRTC.setDS1302Time(00, 00, 8, 3, 23, 7, 2014);
  alarmStart1= Alarm.alarmRepeat(8, 5, 00, Start1); //==start alarm mode interval slave 1
}

void  loop(){ 
   myRTC.updateTime();   

  if(Serial.available())
  {
    char header = Serial.read();
     if (header == INT_SLAVE) 
      Interval();}
       Serial.print("Current Date / Time: ");                                                                 
  Serial.print(myRTC.dayofmonth);                                                                        
  Serial.print("/");                                                                                     
  Serial.print(myRTC.month);                                                                              
  Serial.print("/");                                                                                     
  Serial.print(myRTC.year);                                                                               
  Serial.print("  ");                                                                                     
  Serial.print(myRTC.hours);                                                                              
  Serial.print(":");                                                                                      
  Serial.print(myRTC.minutes);                                                                         
  Serial.print(":");                                                                                      
Serial.println(myRTC.seconds);                                                                                                        
  Alarm.delay(1000);
  }
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++){
    case 0:
      hourStart1 = val;
      break;
    case 1:
      minuteStart1 = val;
      break;
    }
  }
  time_t newStart1 = AlarmHMS (hourStart1, minuteStart1, 0);
  Alarm.write (alarmStart1, newStart1);
  Serial.println(hourStart1, DEC);
  Serial.println(minuteStart1, DEC);
}
void Start1(){
  Serial.println("alarm time is going to set now...");
}

now in this i am sending the command I1406104200 through my serial monitor to set the alarm time to 08:30:00, but its not working . i think this problem is due to the fact that i am sending the unix time which include 23 july 2014 and time 08:30:00 . and according to my program it is expacting only the hour and the minute for this i am sending I-2211723000 which is the unix time for 08:30 to make it work properly but still its not working ... will you please tell me the mistake which i am doing over here and i am unaware of that one

hi Mem!
sorry to disturb you again .... now i have successfully set the alarm time too:)
but one problem which i am encountring is that when i set the new alarm time , the alarm time has been set to new value but in addition to new alarm time program is displaying the old alarm too like in the statement

  alarmStart1= Alarm.alarmRepeat(8,1,00, Start1); //==start alarm mode interval slave 1

i have set the time to 08:01 but when i pass the serial command for setting the alarm time to new value like I08:30
then the alarm will turn on first at 08:01 o'clock then also at 08:30
plz tell me about it why i am encountring this problem and how can i overcome this