Go Down

Topic: Time and TimeAlarms Libraries – Ask here for help or suggestions (Read 77 times) previous topic - next topic


Mar 05, 2014, 04:58 pm Last Edit: Mar 06, 2014, 07:04 am by tevko Reason: 1
Thanks for the response mem!

One thing you can try  is to replace the Alarm.write with a call to Alarm.free with the ID you want to change and then call Alarm.create with the new time.

That's a good idea, I'll try with Alarm.free, or I'll first try to call Alarm.enable to see if it helps.

Can you create a simple test sketch that just handles the alarm rescheduling and see if that works as expected.

I'll do that if I can't find a solution to this problem with Alarm.free.

Alarm.free+Alarm.create did just what I wanted, thanks your help! I'll see if I can find out why Alarm.write was not enough for this.


Hello Mem,

I am building a Hour Counter for my Air Compressor which works eventually. I need to keep a track on the running time in order to do a proper service.
I have the RTC module and it works fine with Time.h. I have developed the program upto some level which gives the elapsed time after 5 sec delay (That is 5 Sec) . but when I tried to do the same subtraction with a if condition corresponding to a button switch it gives 0 duration.

Can you please help me to over come this.

Code: [Select]
* TimeRTC.pde
* example code illustrating Time library with Real Time Clock.

#include <Time.h> 
#include <Wire.h> 
#include <DS1307RTC.h>  // a basic DS1307 library that returns time as a time_t
#include <LiquidCrystal.h>

LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
int se;
int minu;
int hr;
const int buttonPin = 8;     // the number of the pushbutton pin
const int ledPin =  13;      // the number of the LED pin

// variables will change:
int buttonState = 0;         // variable for reading the pushbutton status

void setup()  {
  lcd.begin(20, 4);
    // initialize the LED pin as an output:
  pinMode(ledPin, OUTPUT);     
  // initialize the pushbutton pin as an input:
  pinMode(buttonPin, INPUT);     
  setSyncProvider(RTC.get);   // the function to get the time from the RTC
  if(timeStatus()!= timeSet)
     lcd.println("Unable to");
     lcd.println("RTC has set");     

void loop()
  int timse1;
  int timse2;
  int durse;
  int timmin1;
  int timmin2;
  int durmin;
  int timhr1;
  int timhr2;
  int durhr;
  int timsev1;
  int timsev2;
  int timminv1;
  int timminv2;
  int timhrv1;
  int timhrv2;

    buttonState = digitalRead(buttonPin);

// check if the pushbutton is pressed.
  // if it is, the buttonState is HIGH:
  if (buttonState == HIGH) {     
    // turn LED on:   
    digitalWrite(ledPin, HIGH); 
  timsev1= getse();
  timminv1= getminu();
  timhrv1= gethr();
//  if (buttonState == LOW){
//   digitalWrite(ledPin, LOW);
//    timsev2 = getse();
//    timminv2 = getminu();
//    timhrv2 = gethr();
//    lcd.print("OFF");
//  }
  else {
    // turn LED off:
    digitalWrite(ledPin, LOW);
    timsev2 = getse();
    timminv2 = getminu();
    timhrv2 = gethr();


durse = timse2-timse1;
durmin = timmin2-timmin1;
durhr = timhr2-timhr1;

lcd.setCursor(1, 2);



int getse(){
    time_t t= now();
  se = second(t);
  return se;

int getminu(){
    time_t t= now();
  minu = minute(t);

  return minu;

int gethr(){
  time_t t= now();
  hr = hour(t);
  return hr; 

void digitalClockDisplay(){
  // digital clock display of the time
  /*Serial.print(" ");
  Serial.print(" ");
  Serial.print(" ");
  Serial.println(); */

/*int duration (int ms, int mp){
  //function to find the varience b/w start time n stop time
  int hd;
  int md;
// hd= hp-hs;
  md= mp-ms;


void printDigits(int digits){
  // utility function for digital clock display: prints preceding colon and leading 0
  if(digits < 10)


Hi Samly_dixon, Its not clear on what you want to do and I did not see the code that you are trying to get working. Can you say more about exactly what you want to test with the if statement when a button is pressed.
I noticed that you have created functions to return the current hours, minutes and seconds. These functions already exist as follows:   seconds(), minutes() , hours().
You have a function named duration that has the following comment:
   //function to find the variance b/w start time n stop time
But it is not clear which argument is the start time and which is the stop time. It would be clearer if you used descriptive names for your variables instead of: ms, mp,hd, md etc.

To find the difference between two times, use the time_t returned from the now()function. For example:
Code: [Select]
time_t event = now();
// … some time later
long duration = now() - event; // the duration since the event in seconds


Good day all.

Please pardon me with this noob question/request.

I am trying this Time libraries with the samples but I'm always getting the 'time_t' does not name a type and 'set_Time' was not declared in this scope errors while compiling.  Please note that I am not much into programming, however, if given a flawless sample, I think I can work through the codes and modify them to achieve what I need.

Here's my task.

Similar to the Alarm sample, I wanted to program my arduino micro paired with a 4-channel relay module to switch 4 different lights, the television and the radio at specific times of the day but differs on each day of the week.  I had a previous project that I might be able to use for the television and radio On-Off by Infrared remote control.  So all I need is to make the time and alarm work without any other hardware attached.

Could anyone please point me to the right direction as I have around 10 days to complete this project.  I am bound to go for a vacation and hoping that with this project, I can keep my house safe from breaking and entering which happened last year.

Providing me with a code to start with will be even better.

Your help will be highly appreciated.

More power to you all.


Providing me with a code to start with will be even better.

You clearly already have code that you haven't posted.

You also haven't mentioned which version of the IDE you are using, or which version of the Time library (as in where you got it).

Go Up