How to set a timer for a function and be able to reset it in the middle of the process? Hello me and my friend have to do an assignment for school but we need some help. We have to make a LED turn on and of with a button and a relay. But when I press the button the LED should be on for 10 minutes and then go off unless we press the button inside those 10 minutes then the LED should turn off. Then repeat this process when we press the button again.  

const int relay = 6; 
const int button = 5;
int stateold = 1;
int statenew = 0;
int LED = 0;

void setup() 
{   
  pinMode(relay,OUTPUT);   
  pinMode(button,INPUT); 
  time = millis();
} 

void loop() 
{   
  statenew = digitalRead(knop);
  if(stateold==0 &amp;&amp; statenew==1){
    if(LED==0){
      digitalWrite(relay,HIGH);
      LED = 1;
      }    
    }
    else{
       digitalWrite(relay,LOW);
        LED = 0;
    }
  }
 stateold=statenew;

}
Below code is an example of what you described; pressing the button will turn on the LED and it will only turn off if 10 minutes passed or button is pressed again.

Read the comments in the code:

#define btn 5
#define rly 6

bool btnState;
bool ledState;
unsigned long timer = 0;

void setup() {
  pinMode(btn, INPUT_PULLUP);
  pinMode(rly, OUTPUT);
}

void loop() {
  btnState = digitalRead(btn); // Read the button status (default is 1)

  if (!btnState) { // If button state == 0 (pressed)
    ledState = !ledState; // Toggle this boolean
    timer = millis(); // and reset the timer
  }

  if (ledState) { // If LED state == 1
    digitalWrite(rly, HIGH); // Turn on the Relay
  } else { // Otherwise turn it off
    digitalWrite(rly, LOW);
  }

  if (ledState &amp;&amp; millis() - timer &gt; 600000) { // If 10 minutes passed turn off the relay
    digitalWrite(rly, LOW);
    ledState = !ledState;
  }
}


You should also de-bounce the button and you may want to turn on/off the LED when button is pressed and released, not just when it's pressed.