2 buttons,1 LED

I'm very very new to Arduino and I want to make one simple project,nothing too hard.

So I got an Arduino Nano for my job project.I want to light on LED with 2 buttons when they are pressed.First I searched forum for similar project but I didn't get lucky.

I'm having problem which I don't understand.I don't know what to do about the code,it looks fine to me.I'm currently not in situation to check if it works until tomorrow so if it helps I will thank you later.

Buttons are on pins 10 and 11,I use builtin LED so it's on pin 13,both of button's wires are connected on pins(10 and 11) and other wires from buttons are connected to ground.

If someone could help me,it would be great.And if you could say 2 solutions if you know more than one.

Arduino,2 buttons 1 led.txt (452 Bytes)

OP's code:

int buttonState1=0;
int buttonState2=0;

const int buttonPin1=10;
const int buttonPin2=11;

void setup() {
pinMode(LED_BUILTIN,OUTPUT);
pinMode(buttonPin1,INPUT_PULLUP);
pinMode(buttonPin2,INPUT_PULLUP);
}


void loop() {
buttonState1 = digitalRead(buttonPin1);
buttonState2 = digitalRead(buttonPin2);

if(buttonState1 == HIGH && buttonState2 == HIGH){
digitalWrite(LED_BUILTIN,HIGH);
}

else{
digitalWrite(LED_BUILTIN,LOW);
}
}

So what's the problem? What does it do or not do that don't meet with your expectations?

OP: It looks like it should "work". I assume the "buttons" are tactile switches. Be sure to wire them by attaching any pin of the button to the Arduino pin, and the button pin that is diagonally opposite to it to ground. Then you will need to invert the state in software. Also using smaller data types in a "good thing".

Try these mods: (Note proper indenting. Use 'control T' to format your code in the IDE. It makes it easier to debug later.)

boolean buttonState1;  // boolean variable takes only 1 byte
boolean buttonState2;

const byte buttonPin1=10;  // byte constant takes only 1 byte
const byte buttonPin2=11;
const byte ledPin = 13;  // named variable allows easy changes later

void setup() {
  pinMode(ledPin, OUTPUT);
  pinMode(buttonPin1, INPUT_PULLUP);
  pinMode(buttonPin2, INPUT_PULLUP);
}

void loop() {
  buttonState1 = !digitalRead(buttonPin1);  // Pull up digitalRead is HIGH when not pushed, LOW when pushed
  buttonState2 = !digitalRead(buttonPin2);

  if (buttonState1 and buttonState2){  // since they are booleans, you don't need "== HIGH"
    digitalWrite(ledPin, HIGH);
  }

  else{
    digitalWrite(ledPin, LOW);
  }
}

It does work,but what I get is not I want.

I want to light on when 2 buttons are pressed but instead LED is always on and with each of 2 buttons I can turn it off.

It's like 2 buttons are connected in series with LED and battery in both ends.I want it like that but this is opposite from my idea.

I tried to change HIGH/LOW but then I got parallel circuit.LED was off and with press of 1st button it turns on,also with 2nd button.

I will try with bool and byte,thank you for your help.

Referring to the modifications I posted, see the two lines right after loop()? See the exclamation points in front of digitalRead()? Those are a C keyword, and the reason your buttons seem to be working backward. Look it up on this Reference page.

Hi,
Welcome to the forum.

Please read the first post in any forum entitled how to use this forum.
http://forum.arduino.cc/index.php/topic,148850.0.html then look down to item #7 about how to post your code.
It will be formatted in a scrolling window that makes it easier to read.

Because you are using your buttons to switch to gnd, when your buttons are not pressed the two inputs are pulled HIGH by the internal pullup resistors.

You pull the inputs LOW when you press your buttons.

So pressed is LOW input, unpressed is HIGH input.

Thanks.. Tom.. :slight_smile: