Pages: 1 ... 14 15 [16] 17   Go Down
Author Topic: Time and TimeAlarms Libraries – Ask here for help or suggestions  (Read 68885 times)
0 Members and 1 Guest are viewing this topic.
London
Offline Offline
Tesla Member
***
Karma: 10
Posts: 6250
Have fun!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Note that I updated to posted code  to  so the minute tests for greater than or equal to the day start minute.
Logged

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

I finally managed to get the TimerAlarm work for me.

Here is the code snippet from my setup function:
 Alarm.alarmRepeat (13,31,0, OneThirtyPmEveryDay);
 Alarm.timerRepeat (2, RepeatEveryTwoSeconds);

I have currently hard-coded the alarm Time. I would like to provide an LCD interface that will allow the user to change the time.
My concern is every time the user sets a different time, a call to alarmRepeat will create a new alarm. How can I reuse the existing alarm and update the changed time alone. I didn't see any function that allows me to change the time.
The option is to disable the Alarm on the current time, and create another Alarm but I'm afraid that I will needlessly waste memory as the Alarm on the old time is no longer useful but is sitting there occupying memory.
Can you help?
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48543
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Look at the alarmRepeat() and timerRepeat() methods. They return values that you might not want to discard.
Logged

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

Hi,
Thanks for the nice library! I made some changes to it (the callback parameter), so keep that in mind when reading this---I may have broken something there.

I'm having trouble with rewriting my alarms. I'm switching on 12 lamps with separate timers. Here's a snippet of my code (inside loop()):

Code:
   
} else if (command == MESSAGE_TIMER_ON) {
      int floors=message[0];
      int lamps=message[1];
      int h=message[2];
      int mi=message[3];
      int sec=message[4];
      char msg;
      if (tons[floors][lamps]==0) {
        theOnTimers[floors][lamps]=Alarm.alarmRepeat(h,mi,sec,&alarm_cb,&theOnAlarms[floors][lamps]);
        tons[floors][lamps]=1;
        msg=0x03;
        writeMessage(MESSAGE_OK,&msg,1);
      } else {
        Alarm.write(theOnTimers[floors][lamps],AlarmHMS(h,mi,sec));
        msg=0x04;
        writeMessage(MESSAGE_OK,&msg,1);
      }
    }

The first time my program reaches this point (for each lamp), everything works nicely: Alarm.alarmRepeat is used and timer works as intended (light switches on). The following times the correct code block gets called (determined from the response message 0x04), but the timer does not turn the light on.

Am I missing something obvious? Should I re-enable the alarm after Alarm.write? I'll go re-check my changes to the library if there's nothing obviously wrong in this block of my code.

Thanks for any comments,
Tevko

edit:
The id variable is set up as
Code:
AlarmID_t theOnTimers[3][4];
And is not tampered with anywhere else.
« Last Edit: March 05, 2014, 08:14:56 am by tevko » Logged

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

I'm having trouble with rewriting my alarms. I'm switching on 12 lamps with separate timers ...


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.  Alarm.write sets the alarm time and activates the alarm but it does not update the scheduler.

Otherwise, there is a lot going on there and It’s difficult to see exactly what is happening with the alarms. Can you create a simple test sketch that just handles the alarm rescheduling and see if that works as expected.
Logged

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

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.

edit:
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.
« Last Edit: March 06, 2014, 01:04:26 am by tevko » Logged

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

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:
/*
 * 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()  {
  Serial.begin(9600);
  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");
  else
     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);
   //setTime(12,44,0,3,6,14);
  digitalClockDisplay(); 
   delay(1000);
   lcd.clear();
 
 // 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();
  lcd.print("ON");
 
// 
//  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();
    lcd.print("OFF");
  }

 
  lcd.print(timse1);

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

lcd.setCursor(1, 2);
lcd.print(durhr);
lcd.print(':');
lcd.print(durmin);
lcd.print(':');
lcd.print(durse);

delay(1000);

}

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
  lcd.print(hour());
  printDigits(minute());
  printDigits(second());
 
  /*Serial.print(" ");
  Serial.print(day());
  Serial.print(" ");
  Serial.print(month());
  Serial.print(" ");
  Serial.print(year());
  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;
 
  //lcd.print(hd);
  printDigits(md);
}
*/


 



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

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

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:
time_t event = now();
// … some time later
long duration = now() – event; // the duration since the event in seconds
Logged

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

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

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48543
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

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

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


Mem,
Sorry for being unclear on what I want to test. Basically I want to count the durations of the running machine.

Our machine works every 15 minutes will work for 5 minutes. But it is not constant. It changes due to the usage. So I am developing a system to count the exact running time of the machine. like, how many minutes actually the machine operates.

so when the machine is in the work for one day I must be able to see how many hours the machine has been run actually..




Code:
time_t event = now();
// … some time later
long duration = now() – event; // the duration since the event in seconds



Thanks alot for this simple and beautiful idea. It works. Now I have to merge this duration coding with condition. When the machine is starting I have to call a event = now(); and when the machine goes off I need to call duration = now() - event;

Hope my merging will work,

Basically I am a beginner to arduino. It has been a interesting process for me,
Can you tell me how can I get the duration between a button switch on and off.?
Special thanks for your quick response.

Logged

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

Basically I am a beginner to arduino. It has been a interesting process for me,
Can you tell me how can I get the duration between a button switch on and off.?
Special thanks for your quick response.

There are many ways to do this but if an Arduino pin can monitor the state of the machine then one approach is to capture the time of the event of a pin going high or low using the code posted earlier. Subtracting the time between these events gives you the on and off times. Accumulating the on time and off times should give you the information that you need.

I suggest you experiment with a simple sketch that reads an input connected to a switch and totals the times between the high and low transitions and prints the current switch state, time since previous change of switch state, and total accumulated times in each state to the serial port. 
Logged

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

There are many ways to do this but if an Arduino pin can monitor the state of the machine then one approach is to capture the time of the event of a pin going high or low using the code posted earlier. Subtracting the time between these events gives you the on and off times. Accumulating the on time and off times should give you the information that you need.

I suggest you experiment with a simple sketch that reads an input connected to a switch and totals the times between the high and low transitions and prints the current switch state, time since previous change of switch state, and total accumulated times in each state to the serial port. 



Hi Mem,

Thanks for your valuable tips,

I tried some thing like this.. but on my lcd display i recieve only the current time and a "on"  when  the switch is not pressed it shows current time then after 1s delay a "on".
when i press the button switch for some times the current time stays on the display untill i release the switch. it doesnt changes the display to "on" when the switch is not pressed. when i release the switch old routine comes back.

i couldnt get the duration.

Pls give me your valuable suggestion....

Code:



#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 tim;
int lastState;

const int buttonPin = 8;     // the number of the pushbutton pin
const int ledPin =  13;
int buttonState = 0;   

void setup()  {
  Serial.begin(9600);
  lcd.begin(20, 4);
 
  setSyncProvider(RTC.get);   // the function to get the time from the RTC
  if(timeStatus()!= timeSet)
     lcd.println("Unable to");
  else
     lcd.println("RTC has set");     
}


void loop()
{
  time_t event;
   buttonState = digitalRead(buttonPin);
   //setTime(12,44,0,3,6,14);
  digitalClockDisplay(); 
   delay(1000);
   lcd.clear();
   
   if (digitalRead(buttonState) ==HIGH && buttonState == LOW ) {
     
     digitalWrite(ledPin, HIGH); 
  time_t event = now();
lcd.print("On");
   delay(1000);
   lcd.clear();
   buttonState=digitalRead(buttonState);
   }
   
   if (digitalRead(buttonState) ==LOW && buttonState == HIGH){
     
     
     digitalWrite(ledPin, LOW);
     // … some time later
long duration = now()- event; // the duration since the event in seconds

lcd.println("off");
delay(1000);
lcd.println(duration);

delay(2000);

   }



}


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


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

Logged

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

and for the information,

With that configuration LED is always on.

Samly.
Logged

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

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

Thank you for the reply Paul.

The code that I am referring to is the one included as sample with the library.

I am using 1.0.5-r2 and the Arduino Time Library is the latest one posted in the Learning/Playground page.
Logged

Pages: 1 ... 14 15 [16] 17   Go Up
Jump to: