Time and TimeAlarms Libraries – Ask here for help or suggestions

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

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

mem:

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.

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

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

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

and for the information,

With that configuration LED is always on.

Samly.

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

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

Great. That narrows it down to one of 8. I'm not into guessing games.

and the Arduino Time Library is the latest one posted in the Learning/Playground page.

Post a link. There is an Update note on that page that it looks like you didn't follow.

PaulS:
There is an Update note on that page that it looks like you didn't follow.

Silly/Lazy me. I actually started reading the last time I visited those links but it throws me off instead educating me. I got more confused. I may just have to read again and experiment with my minimal understanding.

Otherwise, my easier solution is just to rely on the delay function of the Arduino and start the sequence at a specific time of day where my program coincides with my desired timing. I'll just pray that no black-outs during my absence so the timing will not change.

Thanks anyway Paul.

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

You are close but there are some errors in your code. You are using buttonState instead of buttonPin in your digitalRead function. Also, you have declared two event variables, one at the beginning of loop and the other in the code that handles the state where the digitalRead is high. Move the time_t event declaration to the top of the sketch where your other global variables are declared and remove time_t from the line where you set event= now() . You can read the arduino reference pages on declaring variables and variable scope for more on this issue.

The issues you are having are not related to the time libraries so I suggest that start a new thread to discuss your project and post a link here so I and others can follow.

I'm having this problem : closing the Serial Monitor renders timeStatus()=timeNotSet.

I've tried this with HyperTerminal & I get the same behaviour : when I disconnect & then re-connect again, my UNO reports (timeStatus()==timeNotSet) to be TRUE.

Has anyone encountered the same problem? Or am I missing something here?

Mem,

mem:
You are close but there are some errors in your code. You are using buttonState instead of buttonPin in your digitalRead function. Also, you have declared two event variables, one at the beginning of loop and the other in the code that handles the state where the digitalRead is high. Move the time_t event declaration to the top of the sketch where your other global variables are declared and remove time_t from the line where you set event= now() . You can read the arduino reference pages on declaring variables and variable scope for more on this issue.

Thank you so to for pointing out my silly coding errors. Its now giving the duration in seconds. :slight_smile: :slight_smile: :slight_smile: :slight_smile:

The issues you are having are not related to the time libraries so I suggest that start a new thread to discuss your project and post a link here so I and others can follow.

I will open a new thread and share the link here. Thank you again for your help.

I tried your solution, but I still have errors on lines 41 and 58. Here is my line replacement :

Line 48 : PGM_P monthNames_P[] =
Line 58 : PGM_P dayNames_P[] = { dayStr0,dayStr1,dayStr2,dayStr3,dayStr4,dayStr5,dayStr6,dayStr7};
Line 59 : const char dayShortNames_P[] PROGMEM = "ErrSunMonTueWedThrFriSat";

Of course, this is not the best solution since monthNames and dayNames will be stores in RAM, but ... it works. Any better proposal is welcome !

Hope this help !

Philippe
www.lucidarme.me

Is it possible to set the time by GSM shield instead of ethernet shield since both are in a sense connected to ntp servers.

As soon as I try to verify an unmodified TimeNTP sketch I get the followiong error messages

In file included from /home/shrek/sketchbook/libraries/Time/DateStrings.cpp:11:0:
/home/shrek/sketchbook/libraries/Time/DateStrings.cpp:41:22: error: variable ‘monthNames_P’ must be const in order to be put into read-only section by means of ‘attribute((progmem))’
PGM_P monthNames_P[] PROGMEM =
^
/home/shrek/sketchbook/libraries/Time/DateStrings.cpp:58:20: error: variable ‘dayNames_P’ must be const in order to be put into read-only section by means of ‘attribute((progmem))’
PGM_P dayNames_P[] PROGMEM = { dayStr0,dayStr1,dayStr2,dayStr3,dayStr4,dayStr5,dayStr6,dayStr7};
^
/home/shrek/sketchbook/libraries/Time/DateStrings.cpp:59:24: error: variable ‘dayShortNames_P’ must be const in order to be put into read-only section by means of ‘attribute((progmem))’
char dayShortNames_P[] PROGMEM = "ErrSunMonTueWedThrFriSat";

What should I do to get the time NTP request function or feature to work. Also if it can be modified to use as external source the time message provided to all gsm enabled phones by the network via the GSM shield I would appreciate it.

I am a beginner in arduino .
I just add this time library and try to test an example .
But i found those errors
/**************************************************************************/
TimeSerial.pde:12:20: warning: Time.h: No such file or directory
TimeSerial:24: error: 'time_t' does not name a type
TimeSerial.pde: In function 'void setup()':
TimeSerial:20: error: 'requestSync' was not declared in this scope
TimeSerial:20: error: 'setSyncProvider' was not declared in this scope
TimeSerial.pde: In function 'void loop()':
TimeSerial:29: error: 'timeStatus' was not declared in this scope
TimeSerial:29: error: 'timeNotSet' was not declared in this scope
TimeSerial:31: error: 'timeSet' was not declared in this scope
TimeSerial.pde: In function 'void digitalClockDisplay()':
TimeSerial:39: error: 'hour' was not declared in this scope
TimeSerial:40: error: 'minute' was not declared in this scope
TimeSerial:41: error: 'second' was not declared in this scope
TimeSerial:43: error: 'day' was not declared in this scope
TimeSerial:45: error: 'month' was not declared in this scope
TimeSerial:47: error: 'year' was not declared in this scope
TimeSerial.pde: In function 'void processSyncMessage()':
TimeSerial:65: error: 'time_t' was not declared in this scope
TimeSerial:65: error: expected `;' before 'pctime'
TimeSerial:69: error: 'pctime' was not declared in this scope
TimeSerial:72: error: 'pctime' was not declared in this scope
TimeSerial:72: error: 'setTime' was not declared in this scope
TimeSerial.pde: At global scope:
TimeSerial:77: error: 'time_t' does not name a type
/****************************************************************************/

I am using arduino uno r3
and ide version is 1.0.5

please any one help me

Thank you

sarwarislammoon:
I am a beginner in arduino .
I just add this time library and try to test an example .
But i found those errors
/**************************************************************************/
TimeSerial.pde:12:20: warning: Time.h: No such file or directory

A bit old post, but anyway
You need to download and put the Time library where you installed the IDE (like: C:/program(x86)/Arduino/Libraries/Time
The sketch can't find this library when you try to compile and send it to the UNO .
If you have downloaded the Time library, be sure it's not put inside a second folder by the same name ( i.e Time/Time)

Good luck

It would be better to put the Time library in the correct place which is the libraries folder of the sketch folder.

Like this C:\Users\Bob\Documents\Arduino\libraries

The point about having the library folder one level too deep still stands though.

UKHeliBob:
It would be better to put the Time library in the correct place which is the libraries folder of the sketch folder.

Like this C:\Users\Bob\Documents\Arduino\libraries

The point about having the library folder one level too deep still stands though.

oops.. sorry..didn't mean to be misleading and i didn't know there were a library folder there too.. but it makes perfect sense now that you've mentioned it.

Thanks for pointing it out

Hey guys,

I happen to have a question about TimeAlarms. Can you use the TimeAlarm library to activate a servo motor at a certain time? And also, can you sync the RTC DS1307 to the TimeAlarm? If you can, how do you? Any help would be appreciated. Thanks.

i didn't know there were a library folder there too..

The libraries folder in the sketch folder is designed to hold user contributed libraries that are available to all versions of the IDE of which there may be several installed as it is upgraded. The libraries folder under the main Arduino folder is designed to hold libraries supplied with the IDE of which there could potentially be more/different versions specific to an IDE version.