Getting elapsed time using ds1307 module

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

Use 24 hour mode:

timeStart = (hours * 60 * 60) + (minutes * 60) + seconds; // second of the day
timeEnd =  (hours * 60 * 60) + (minutes * 60) + seconds; // second of the day
(if (timeEnd - timeStart) >=5){
// do whatever
}

add days in also if think there will be button pushes at midnight ...

Thank you for your reply! I think I got it to work. I'm going to attach the code in case someone else finds it useful.

#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");
  Serial.println();
  Serial.print("timeStart");
  Serial.print("\t");
  Serial.print("timeEnd");
  Serial.print("\t");
  Serial.print("\t");
  Serial.print("timeEnd-timeStart");
  Serial.println();
}

void loop(){
  
  buttonState = digitalRead(button);
  
  timeStart = (hour() * 60 * 60) + (minute() * 60) + second();
  
  Serial.print(timeStart);
  Serial.print("\t");
  
  if(buttonState == HIGH){
    timeEnd = ((hour() * 60 * 60) + (minute() * 60) + second())+5;
  }
  
  Serial.print(timeEnd);
  Serial.print("\t");
  Serial.print("\t");
  Serial.print(timeEnd-timeStart);
  
  if ((timeEnd-timeStart) >=5){
    digitalWrite(led, LOW);
  } else{
    digitalWrite(led, HIGH);
  }
  
  Serial.println();
  delay(1000);
}