2 push buttons with combination

Hi. is there anyone who is willing to help me, I have tried browsing for days but still haven't found what I expected to make a combination of 2 push buttons,the way it works is as follows:

The LED will light when the pushbutton combination (1 - 2 - 1) is in a state
led ON as long as push button 1 is pressed and OFF if button 1 is released

my plan : if this works I will replace the LED with a relay. this project I use arduino nano (old bootloader)


thanks alot for who helping me.have a nice day

Post your best attempt and we can help. Tell us what the code actually does and how that differs from what you want.

It is unlikely that anyone will write the code for you.

Include a schematic so that we know how the switches and other components are connected.

Read the how to use this forum-please read sticky to see how to properly post code and some advice on how to ask an effective question. Remove useless white space and format the code with the IDE autoformat tool (crtl-t or Tools, Auto Format) before posting code.

const int ButtonOnePin = 2;
const int ButtonTwoPin = 3;
const int RelayPin = LED_BUILTIN;

void setup() {
  pinMode(RelayPin, OUTPUT);
  pinMode(ButtonOnePin, INPUT_PULLUP);
  pinMode(ButtonTwoPin, INPUT_PULLUP);
}

void loop() {
  if (digitalRead(ButtonOnePin) == LOW) {
    if (digitalRead(ButtonTwoPin) == LOW) digitalWrite(RelayPin, LOW);
    else digitalWrite(RelayPin, HIGH);
  }
  else digitalWrite(RelayPin, HIGH);
}

groundFungus: Post your best attempt and we can help. Tell us what the code actually does and how that differs from what you want.

It is unlikely that anyone will write the code for you.

Include a schematic so that we know how the switches and other components are connected.

Read the how to use this forum-please read sticky to see how to properly post code and some advice on how to ask an effective question. Remove useless white space and format the code with the IDE autoformat tool (crtl-t or Tools, Auto Format) before posting code.

Thank you for the advice ,I attach the following

groundFungus:
Post your best attempt and we can help. Tell us what the code actually does and how that differs from what you want.

It is unlikely that anyone will write the code for you.

Include a schematic so that we know how the switches and other components are connected.

Read the how to use this forum-please read sticky to see how to properly post code and some advice on how to ask an effective question. Remove useless white space and format the code with the IDE autoformat tool (crtl-t or Tools, Auto Format) before posting code.

here my schematic

Perehama: ``` const int ButtonOnePin = 2; const int ButtonTwoPin = 3; const int RelayPin = LED_BUILTIN;

void setup() {  pinMode(RelayPin, OUTPUT);  pinMode(ButtonOnePin, INPUT_PULLUP);  pinMode(ButtonTwoPin, INPUT_PULLUP); }

void loop() {  if (digitalRead(ButtonOnePin) == LOW) {    if (digitalRead(ButtonTwoPin) == LOW) digitalWrite(RelayPin, LOW);    else digitalWrite(RelayPin, HIGH);  }  else digitalWrite(RelayPin, HIGH); }

after I tried your code, it seems that ignoring pin3 doesn't work


OP image. How to post images.

To use the code that Perehama posted you will have to re-wire your switches. His code is for switches wired to ground with no external resistors. So remove the pulldown resistors and wire the switches from ground to their respective inputs. The pullup is taken care of by the pinMode(pin, INPUT_PULLUP) in setup().

ok. but not like that , want ,I want the LEDs to turn on if sw1 and sw 2 are pressed alternately with the combination of sw1-sw2-sw1 then the led lights up, but if it starts with sw2-sw1-sw2 the led will not turn on

OK,
now I am trying to modify code from the arduino reference “StateChangeDetection” maybe this can be the way I want

 This example code is in the public domain.

  http://www.arduino.cc/en/Tutorial/ButtonStateChange
*/

// this constant won't change:
const int  buttonPin = 2;    // the pin that the pushbutton is attached to
const int  buttonPin1 = 3;
const int ledPin = 9;       // the pin that the LED is attached to

// Variables will change:
int buttonPushCounter = 0;   // counter for the number of button presses
int buttonState = 0;         // current state of the button
int lastButtonState = 0;     // previous state of the button
int buttonState1 = 0;
void setup() {
  // initialize the button pin as a input:
  pinMode(buttonPin, INPUT);
  pinMode(buttonPin1, INPUT);
  // initialize the LED as an output:
  pinMode(ledPin, OUTPUT);
  // initialize serial communication:
  Serial.begin(9600);
}


void loop() {
  // read the pushbutton input pin:
  buttonState = digitalRead(buttonPin);
  buttonState1 = digitalRead(buttonPin1);

  // compare the buttonState to its previous state
  if (buttonState != lastButtonState) {
    // if the state has changed, increment the counter
    if (buttonState == HIGH) {
      // if the current state is HIGH then the button went from off to on:
      buttonPushCounter++;
      Serial.println("on");
      Serial.print("number of button pushes: ");
      Serial.println(buttonPushCounter);
    } else {
      // if the current state is LOW then the button went from on to off:
      Serial.println("off");
    }
    // Delay a little bit to avoid bouncing
    delay(50);
  }
  // save the current state as the last state, for next time through the loop
  lastButtonState = buttonState;


  // turns on the LED every four button pushes by checking the modulo of the
  // button push counter. the modulo function gives you the remainder of the
  // division of two numbers:
  if ((buttonPushCounter % 2 == 0)&&(buttonState1 != HIGH)) {
    digitalWrite(ledPin, HIGH);
  } else {
    
    digitalWrite(ledPin, LOW);
  }

}

now the problem
buttonPushCounter ++ is always looping how to reset

Did you change the switch wiring to an active LOW configuration (wired to ground internal pullup enabled)? See reply #6. In the configuration where the switch is wired to ground, the input is HIGH when the switch is not pressed and LOW when it is pressed,

If so you need to change:

if (buttonState != lastButtonState) {
    // if the state has changed, increment the counter
    if (buttonState == HIGH) {  
      // if the current state is HIGH then the button went from off to on:

to:

if (buttonState == LOW) {  // switch is low when pressed, high when not pressed

And this comment:

// if the current state is LOW then the button went from on to off:

to:

// if the current state is HIGH then the button went from on to off:

You seem to be just blindly trying code without reading and understanding it first. Not the best way to go.

Yes, Mr groundFungus its work for me,ithink its enough, thanks alot brother