Problem with my arduino selector switch

I'm trying to make a selector switch, which will turn on individual LEDs according to the button i pressed. The code works but i'm having troubles as i press "butonPin1" "ledPin1 and ledPin2" both turns on. Can someone please help me out with the code?

I'm using 330R for the leds, and 10K for the pull down resistors of the tact switches

const int buttonPin1 = 2; const int ledPin1 = 13; const int buttonPin2 = 4; const int ledPin2 = 12; const int buttonPin3 = 7; const int ledPin3 = 8;

int buttonState1; int buttonState2; int buttonState3;

void setup() {

pinMode(ledPin1, OUTPUT); pinMode(buttonPin1, INPUT); pinMode(ledPin2, OUTPUT); pinMode(buttonPin2, INPUT); pinMode(ledPin3, OUTPUT); pinMode(buttonPin3, INPUT); }

void loop() {

buttonState1 = digitalRead(buttonPin1);

if (buttonState1 == 1) {

digitalWrite(ledPin3, 0); digitalWrite(ledPin2, 0); digitalWrite(ledPin1, 1); } else { }

buttonState2 = digitalRead(buttonPin2);

if (buttonState2 == 1) {

digitalWrite(ledPin3, 0); digitalWrite(ledPin1, 0); digitalWrite(ledPin2, 1); } else {

}

buttonState3 = digitalRead(buttonPin3);

if (buttonState3 == 1) {

digitalWrite(ledPin1, 0); digitalWrite(ledPin2, 0); digitalWrite(ledPin3, 1); } else { } }

Are you sure you have it wired correctly? The code looks OK in that regard. Do you have pulldown resistors connected to the button input pins, to keep them low unless the buttons are pressed?

You don't need the empty 'else' for each 'if', either.

And it's better to post your code in [code]code tags[/code], rather than inline. It makes it easier to read, or to copy and paste into an IDE for checking/verifying. Also, when posted inline, it can sometimes become corrupted by italics or smilies.

Edit: Usually it's better to connect the buttons between the input pin and ground, then usepinMode(buttonPin, INPUT_PULLUP);to enable the internal pullup resistors, to avoid using external resistors. Then the logic is inverted. ie When a button is pressed, the pin goes low.

How are your switches wired? Try useing INPUT_PULLUP instead of INPUT

|500x436

LarryD: How are your switches wired? Try useing INPUT_PULLUP instead of INPUT

|500x436

I was just editing to say the same as you posted. Sorry 'bout that. :)

(deleted)

@OldSteve Isn't it night time there?

LarryD: @OldSteve Isn't it night time there?

Sure is. Right on 1am. I'm just having a last coffee before bed. :D

Edit: And sorry christopher_h, I missed where you said you're using pulldown resistors when I said this:-

Do you have pulldown resistors connected to the button input pins, to keep them low unless the buttons are pressed?

0xCOFFEE and bed ?

(deleted)

travis_farmer: I don't see any code or hardware for button debouncing.

No debouncing needed. Its not as if the presses are being counted, not even to toggle a value. Of course, the OP may move the goalposts in his next post, and suddenly they will need debouncing!

LarryD: 0xCOFFEE and bed ?

Yeah, I can't help myself. I live on coffee.

spycatcher2k: 0xCOFFEE MMMMM. . . .. my favorite HEX number :)

Mine too. :)