rs latch code leaves led on

I've been having some trouble making a simple rs latch with my arduino uno R3, whenever I turn on or reset the arduino, it just turns the led on and if I press either of the buttons, nothing happens. I hope someone can help point me in the right direction as everything I have tried hasn't done anything.

const int ledPin = 13;
const int button1 = 8;
const int button2 = 3;

void setup() {

  pinMode(button1, INPUT);
  pinMode(button2, INPUT);
  pinMode(ledPin, OUTPUT);
}

void loop() {
  if (digitalRead(button1) == HIGH) {
    digitalWrite(ledPin, HIGH);
  }
  if (digitalRead(button2) == HIGH) {
    digitalWrite(ledPin, HIGH);
  }
}

2021-03-30-163245.jpg

2021-03-30-163245.jpg

digitalWrite(ledPin, HIGH); //turn ON LED
digitalWrite(ledPin, LOW); // turn OFF LED

You also need a resistor with the LED
You also need to GOOGLE : How to wire a button to an Arduino

What missdrew said.

The current wiring cannot provide an intentional LOW to the switch inputs. This is because the inputs are either floating (an undesirable state) when the switch is open or connected to 3.3v when the switch is closed. Choose one of the configurations from the schematic below.

Using RS latch code for 2 buttons is quite simple and no debouncing is required. The only condition that might need handling is if the user presses both buttons at the same time. Check here for some examples.

Thanks so much, your advice has been very helpful, and I got it to work!

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.