switch button and LED

Hi

I am using two switch buttons and two LEDs.

I need the code to represent the following:

When button1 is pressed led 1 should turn on for 10 seconds even though the button 2 is pressed and vice versa.

Please help me to do this.....Thanks

How much code has you managed to write yourself so far?

void loop() {
 

  if (digitalRead(buttonPin1) == HIGH) {
    // turn LED on:
    digitalWrite(ledPin, HIGH);
   delay(10000);
}
 if (digitalRead(buttonPin2) == HIGH) {
    // turn LED on:
    digitalWrite(ledPin2, HIGH);
 delay(10000);
}
}

because of the delay it is not working

explore several things at the same time

When button1 is pressed led 1 should turn on for 10 seconds even though the button 2 is pressed and vice versa.

This is an incomplete specification. what needs to happen when Led1 is on and button2 is pressed afterwards? is this a valid transition?

With 2 LEDs you have 4 possible states (the circles). You have given us some ideas about the possible transitions between states but it's very incomplete. Are the other purple transitions possible? and what are the conditions for those transitions?

Once you have this defined, then it should not be to difficult to implement without delay.

when button2 is pressed LED2 turn on for 10 ten seconds same time the LED1 should not turn off

so

button1 triggers LED1 for 10 seconds
button2 triggers LED2 for 10 seconds
No two LEDs can be on at the same time

is that correct?

sorry two LEDs on at same time

So you have the state machine defined, the rest is easy... :slight_smile:

EDIT - SO YOU HAVE NOW MODIFIED YOUR ANSWER ABOVE... bad practice so You are on your own to implement something matching your need with two LEDS on (You will need two time trackers)

So this code should do the state machine as posted in #5,

enum {LedsOFF, Led1ON_Led2OFF, Led1OFF_Led2ON} state;

const byte BUTTON_PRESSED = LOW;

const byte button1Pin = 4;
const byte button2Pin = 5;

const byte ledP1Pin = 8;
const byte ledP2Pin = 9;

unsigned long lastTimeTurnedOn;

void setStateLedsOff()
{
  state = LedsOFF;
  digitalWrite(ledP1Pin, LOW);
  digitalWrite(ledP2Pin, LOW);
}

void setStateLed1ON_Led2OFF()
{
  state = Led1ON_Led2OFF;
  digitalWrite(ledP1Pin, HIGH);
  digitalWrite(ledP2Pin, LOW);
  lastTimeTurnedOn = millis();
}

void setStateLed1OFF_Led2ON()
{
  state = Led1OFF_Led2ON;
  digitalWrite(ledP1Pin, LOW);
  digitalWrite(ledP2Pin, HIGH);
  lastTimeTurnedOn = millis();
}

void setup() {
  pinMode(button1Pin, INPUT_PULLUP);
  pinMode(button2Pin, INPUT_PULLUP);
  pinMode(ledP1Pin, OUTPUT);
  pinMode(ledP2Pin, OUTPUT);
  setStateLedsOff();
}

void loop() {

  switch (state) {
    //===============================
    case LedsOFF:
      if (digitalRead(button1Pin) == BUTTON_PRESSED) {
        delay(20); // poor's man debouce :)
        setStateLed1ON_Led2OFF();
      }
      if (digitalRead(button2Pin) == BUTTON_PRESSED) {
        delay(20); // poor's man debouce :)
        setStateLed1OFF_Led2ON();
      }
      break;
    //===============================
    case Led1ON_Led2OFF:
      if ( millis() - lastTimeTurnedOn >= 10000ul) {
        setStateLedsOff();
      }
      if (digitalRead(button2Pin) == BUTTON_PRESSED) {
        delay(20); // poor's man debouce :)
        setStateLed1OFF_Led2ON();
      }
      break;
    //===============================
    case Led1OFF_Led2ON:
      if ( millis() - lastTimeTurnedOn >= 10000ul) {
        setStateLedsOff();
      }
      if (digitalRead(button1Pin) == BUTTON_PRESSED) {
        delay(20); // poor's man debouce :)
        setStateLed1ON_Led2OFF();
      }
      break;
  }
}

So you modify your answer after my answer? how can anyone follow then?

what you need to code is something like this - please post your work.

Ok, you press button 1 and led1 turn on. What about if you press button 1 again after lets say 8 seconds, when led1 still is on?

1, Do nothing, ignore the press.
2, Restart the 10 second timer so led1 will actually be on for 18 seconds total.
3, turn off led1.

Gabriel_swe:
Ok, you press button 1 and led1 turn on. What about if you press button 1 again after lets say 8 seconds, when led1 still is on?

1, Do nothing, ignore the press.
2, Restart the 10 second timer so led1 will actually be on for 18 seconds total.
3, turn off led1.

fair points, there are other possible transitions which I ignored in the schema above