Hi everyone,
I'm trying to get the elapsed time with a ds1307. The idea is to press a button and have an LED on for 5 seconds and then turn off. I tried to add 5 seconds to the current time. Compare, and when the current time is over 5 seconds turn off LED. Problem is if I press the button at 58 seconds, add 5 seconds and it goes to 63. Time will never reach 63 so the LED will be on forever. I'm pretty new at this so pardon the noobness. Any help/tips please? Is there a better method of doing this?
#include <Time.h>
#include <Wire.h>
#include <DS1307RTC.h> // a basic DS1307 library that returns time as a time_t
const int button = 2;
const int led = 8;
int buttonState = 0;
time_t current;
time_t duration;
void setup() {
Serial.begin(9600);
pinMode(button, INPUT);
pinMode(led, OUTPUT);
while (!Serial) ; // wait until Arduino Serial Monitor opens
setSyncProvider(RTC.get); // the function to get the time from the RTC
if(timeStatus()!= timeSet)
Serial.println("Unable to sync with the RTC");
else
Serial.println("RTC has set the system time");
}
void loop(){
buttonState = digitalRead(button);
Serial.print(second());
Serial.print("\t");
Serial.print(second(current));
if(buttonState == HIGH){
current = now();
duration = second(current)+5;
if(duration>60){
duration = (duration-60);
}
}
Serial.print("\t");
Serial.print(duration);
if(duration>second()){
digitalWrite(led, HIGH);
}else{
digitalWrite(led, LOW);
duration = 0;
}
Serial.println();
delay(1000);
}