break out loop/delay with push button

I have 2 buttons and 2 leds. If I push button 1, led1 light for 10 seconds. when button 2 is pressed it light led 2 for 10 seconds. So far it works. If led 1 is on, I want it turn it off, the moment I press button 2, even it is still in the 'delay' time. But as long it is the 'delay' the board respond on nothing. I have no idea how to solve this. Anyone a clue?

int switchstate1 = 0;
int switchstate2 = 0;

void setup() {
  // declare the pins 
  
  pinMode(4, OUTPUT);
  pinMode(5, OUTPUT);

  // declare the switch pin as an input
  pinMode(2, INPUT);  //switch 1 
  pinMode(3, INPUT); //switch 2 
  
}


void loop() {
 switchstate1 = digitalRead(2);
 switchstate2 = digitalRead(3);

  if (switchstate1 == HIGH) {
    digitalWrite(4, HIGH); // turn pin 4 on
    delay(10000);
    digitalWrite(4, LOW); // off 
  }
  if (switchstate2 == HIGH) {
    digitalWrite(5, HIGH); // pin 5 on
    delay(10000);
    digitalWrite(5, LOW); // 
  }
}

You can use an external interrupt which is triggered by a button to escape out of all those delays you've built into your code. The recommended approach is, however, not to use the delay function at all and instead set timers using the millis() function and test these perioically in your loop(). See the standard sketch "blink without delay" for an example of this.

richard7777777: But as long it is the 'delay' the board respond on nothing.

Yes, that's the idea behind the Arduino "idiots command " delay(): Block every program execution ("busy waiting") until the time has passed, then execute the next line of code.

What you want to do requires a special kind of programming logic.

One possibility would be the programming logic of a "Finite-State-Machine (FSM), which provides one "state function" for each operating state.

From your description your machine has three states of operation:

BOTHLEDSOFF, LED1ON, LED2ON

Should be an easy task to create three state functions for that, create an array of three function pointers and call the state function accordingly to the current operating state several thousand times per second.The state functions for LED1ON and LED2ON states should include a ten second timeout, so that after ten seconds those states automatically change to BOTHLEDSOFF. Correctly?

Or do you want to operate both LEDs independently and need a fourth state BOTHLEDSON, so that both LEDs will be set to ON in case both buttons are pressed within a period of less than ten seconds?

Do you need an example program for such a FSM programming logic, two push buttons and two LEDs?

This sounds like a scenario where interrupts should be used.

trevortjes: This sounds like a scenario where interrupts should be used.

'can be used' is a far better term. And it will probably not work because once the isr is finished, the code will happily continue doing what it was doing (e.g. the delay)

There is no need for interrupts, just a code based on e.g. robin2's several things at the same time.

jurs: Yes, that's the idea behind the Arduino "idiots command " delay(): Block every program execution ("busy waiting") until the time has passed, then execute the next line of code.

What you want to do requires a special kind of programming logic.

One possibility would be the programming logic of a "Finite-State-Machine (FSM), which provides one "state function" for each operating state.

From your description your machine has three states of operation:

BOTHLEDSOFF, LED1ON, LED2ON

Should be an easy task to create three state functions for that, create an array of three function pointers and call the state function accordingly to the current operating state several thousand times per second.The state functions for LED1ON and LED2ON states should include a ten second timeout, so that after ten seconds those states automatically change to BOTHLEDSOFF. Correctly?

Or do you want to operate both LEDs independently and need a fourth state BOTHLEDSON, so that both LEDs will be set to ON in case both buttons are pressed within a period of less than ten seconds?

Do you need an example program for such a FSM programming logic, two push buttons and two LEDs?

I think FSM is too complicated and not what I'm looking for. But I'm always interested in an example.

If you think a FSM is to complicated then I would stop programming... The name and all the in depth theory might seem daunting but it really isn't that hard. The base of it is just "do different stuff based on the value of a single variable, the state".