Timer with an on off Button

Hello,
I’m struggling with my project.

I have one push Input Button Pin 2 and one Output Relay Pin 13.

The function should be like this.
if i push the Button the relay should switch on and stay on until
a Time of 15minutes has pasted or the Button is pressed again.

Somehow all my approaches are failing, maybe someone can help me?

Welcome to the Forum. Please read the post:

How to use this forum (short version)

then post your approaches and explain in what way they fail.

Post your code here in code tags. Easier to help if we can see what you've tried and where you need help.

Regarding the information You give, it's hard or impossible to help You.

Read the first topics like "How to use this Forum". They tell what information You need to supply.

Silver bullet approach?

#include <mechButton.h>
#include <timeObj.h>

#define BUTTON_PIN   2
#define RELAY_PIN    13 // We'll do the LED for now.


mechButton ourButton(BUTTON_PIN);      // Hook a button between pin2 & gnd.
timeObj     stopTimer(5 * 1000.0);     // 5 second timer. use (15 * 60000.0) for 15 minutes.
bool        relaySet;                  // A boolean to track the state of the relay.


void setup(void) {

   pinMode(RELAY_PIN, OUTPUT);            // Set up our output pin.
   setRelay(false);                       // Relay (LED) off.
   ourButton.setCallback(buttonCLicked);  // Setup callback for button clicks.
}


// When the button is clicked or un-clicked, this will be called.
void buttonCLicked(void) {

   if (!ourButton.trueFalse()) {    // If the button was clicked (Pushed not released)..
      if (relaySet) {               // If the relay is set (active).
         setRelay(false);           // Turn it off.
      } else {                      // Else, relay was NOT set..
        setRelay(true);             // Turn it On.
      }
   }
}


// We'll use this to turn the relay on or off.
void setRelay(bool onOff) {

   if (onOff) {                        // If they want the relay turned on..
      digitalWrite(RELAY_PIN,HIGH);    // Turn relay (LED) on!
      relaySet = true;                 // Track it.
      stopTimer.start();               // Fire up the timer.
   } else {                            // Else, they want the relay turned off..
      digitalWrite(RELAY_PIN,LOW);     // Turn relay (LED) off.
      relaySet = false;                // Track it.
   }
}


void loop(void) {

   idle();                             // Let the background tasks do their thing.
   if (relaySet && stopTimer.ding()) { // If the relay is set (active) and our timer has expired..
      setRelay(false);                 // Shut down the relay.
   }
}

If you would like to try this approach, you will need to install LC_baseTools from the library manager.

-jim lee