Resolved Active-Low Two Pushbuttons

Hello,

I'm trying to adapt this code:

int ledPin = 13; // choose the pin for the LED
int inPin = 7;   // choose the input pin (for a pushbutton)
int val = 0;     // variable for reading the pin status

void setup() {
  pinMode(ledPin, OUTPUT);  // declare LED as output
  pinMode(inPin, INPUT);    // declare pushbutton as input
}

void loop(){
  val = digitalRead(inPin);  // read input value
  if (val == HIGH) {         // check if the input is HIGH (button released)
    digitalWrite(ledPin, LOW);  // turn LED OFF
  } else {
    digitalWrite(ledPin, HIGH);  // turn LED ON
  }
}

And I want it to include two pushbuttons, not just one.

The idea is to push the button down to turn off the LED light, otherwise the LED stays on.

So I've added an OR statement to this code (val = digitalRead(inPin || inPin2); ), as well as the second int and pinMode, naming the variable inPin2.

int ledPin = 13; // choose the pin for the LED
int inPin = 4;   // choose the input pin (for a pushbutton)
int inPin2 = 7;
int val = 0;     // variable for reading the pin status

void setup() {
  pinMode(ledPin, OUTPUT);  // declare LED as output
  pinMode(inPin, INPUT);    // declare first pushbutton as input
  pinMode(inPin2, INPUT); //declare second pushbutton as input
}

void loop(){
  val = digitalRead(inPin || inPin2);  // read input value
  if (val == HIGH) {         // check if the input is HIGH (button released)
    digitalWrite(ledPin, LOW);  // turn LED OFF
  } else {
    digitalWrite(ledPin, HIGH);  // turn LED ON
  }
}

It doesn't work. :slight_smile: What have I not added. Thanks much as always! :slight_smile:

Although it compiles I see, I'm not sure what this will actually do:

val = digitalRead(inPin || inPin2);

You can't expect it to read one pin or another pin.....

My plan is to have two buttons. When one button is pressed the LED turns off, and the same with the second button. When both buttons are not pressed then the LED is turned on.

I'm wondering if the switch/case code will work. If so, can someone show me how?

Something like this....

val = digitalRead(inPin);
val2 = digitalRead(inPin2);

if (val == HIGH || val2==HIGH) //etc etc as you had it

PS, you say "active low" so presumbly those buttons have pullup resistors? If not you should declare them with the internal ones, thusly:

pinMode(inPin, INPUT_PULLUP);    // declare pushbutton as input

Great! Thank you. That did the trick.

I must be mistaken on this being Active-Low because the code:

  pinMode(inPin, INPUT_PULLUP);    // declare first pushbutton as input
  pinMode(inPin2, INPUT_PULLUP); //declare second pushbutton as input

doesn't give me the desired result.

This:

if (val == HIGH || val2 == HIGH)

works.

The tutorial shows a resistor, and describes it as a pullup.

What that means is that the pin will be HIGH with the button not pressed, and LOW when it's pressed (=active).

Nice thing about active low is that it's easy to remember, when the button is physically "high" ie unpushed, the pin is HIGH. Push it in... physically low... then the pin is LOW.