I'm trying to use a ds1307 module to use time with my project. The following is my code to make an LED light up for 5 seconds when I press a push button. Problem is I have to hold the button for a second because of the "delay(1000)" line. Eventually I plan on showing the time on an LCD screen so if I remove the delay I wont be able to display the time. How do I make it so that the button can just be pressed and let go, without holding it down?
#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 timeStart;
time_t timeEnd;
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);
timeStart = (hour() * 60 * 60) + (minute() * 60) + second();
if(buttonState == HIGH){
timeEnd = ((hour() * 60 * 60) + (minute() * 60) + second())+5;
}
if ((timeEnd-timeStart) >=5){
digitalWrite(led, LOW);
} else{
digitalWrite(led, HIGH);
}
delay(1000);
}