Not sure if my arduino is broken or not

Hello,

I am planning on building an audio-switch for the sake of my own learning. The thing i want to do if when pushing button one, source one should be selected and so on. For now I am using LEDs to simulate output, so when pushing button one LED1 turns on, and so on... Thing is i think i got my code right, men the 3 LEDs just keeps blinking randomly and i do not know why.

LED 1: Connected to 10 & GND
LED 2: Connected to 9 & GND
LED 3: Connected to 8 & GND

Button 1: Connected to 7 and GND
Button 2: Connected to 6 and GND
Button 3: Connected to 5 and GND

My code:

const int button1 = 7; 
const int button2 = 6; 
const int button3 = 5;
const int led1 = 10; 
const int led2 = 9; 
const int led3 = 8; 
int btn1status = 0;
int btn2status = 0;
int btn3status = 0;


void setup() {
  // put your setup code here, to run once:
pinMode (button1, INPUT);
pinMode (button2, INPUT);
pinMode (button3, INPUT);
pinMode (led1, OUTPUT);
pinMode (led2, OUTPUT);
pinMode (led3, OUTPUT);
digitalWrite (led1, LOW);
digitalWrite (led2, LOW);
digitalWrite (led3, LOW);

}

void loop() {
  // put your main code here, to run repeatedly:
  btn1status = digitalRead(button1);
  btn2status = digitalRead(button2);
  btn3status = digitalRead(button3);
if (btn1status == HIGH) {
  digitalWrite(led1, HIGH);
  digitalWrite(led2, LOW);
  digitalWrite(led3, LOW);
}
  if (btn2status == HIGH) {
  digitalWrite(led1, LOW);
  digitalWrite(led2, HIGH);
  digitalWrite(led3, LOW);
  }
  if (btn3status == HIGH) {
  digitalWrite(led1, LOW);
  digitalWrite(led2, LOW);
  digitalWrite(led3, HIGH);
  }
  else {
  digitalWrite(led1, LOW);
  digitalWrite(led2, LOW);
  digitalWrite(led3, LOW);
  }
}

I would be very grateful if someone could help me with this =)

(deleted)

So this is what your code does:
If button1 is pressed, turn led1 on, then:
If button2 is pressed, turn led1 off, turn led2 on, then:
If button3 is pressed, turn led1 and led2 off, turn led3 on, then:
If button3 is not pressed, turn everything off.

This code will always set all 3 outputs to low, except button3 is pressed, then led3 is HIGH.
So no, your code is not right.

Also, how do you intend to switch the audio signal? You need additional hardware to do that.

lg, couka

The inputs are floating when the button isn't pressed - It's like you had nothing connected to the pin. With nothing at all on a high-impedance input, the wire is "floating", and it will pick up ambient electromagnetic noise and the voltage will move randomly between 0v and 5v (the internal clamping diodes keep it within that range). If you just read an analog pin connected to nothing and print the values to serial, you'll find that waving your hands near the pin will change the reading.

You need to use a pull-up or pull-down resistor on the input pin to ensure it has a known state when not connected to anything. IE, if pressing the button connects to Vcc, put a pull-down resistor to ground on the pin (so it will read 1 if pressed, and 0 otherwise). If pressing the button connects to Gnd, put a pull-up resistor to Vcc on the pin (so it reads 0 if pressed and 1 otherwise). 10k is a good value.

The easiest solution though, it to have the buttons connect to ground when pushed (so 0/LOW is pressed, 1/HIGH is not-pressed), and use the internal pullup resistor by doing pinMode(pin,INPUT_PULLUP).

Even if not using the internal pullups, best practice is to have the buttons connect to ground, not Vcc, so you don't have to run the supply wire to all your buttons.

Is my code totally wrong to my purpuse? :confused:

spycatcher2k:
change INPUT to INPUT_PULLUP
and HIGH to LOW

That made it more stable, but now LED1 is only on while i am pushing button 1. I want LED1 to be lit until i press button 2, then LED1 should turn of and LED2 turn on.

couka:
So this is what your code does:
If button1 is pressed, turn led1 on, then:
If button2 is pressed, turn led1 off, turn led2 on, then:
If button3 is pressed, turn led1 and led2 off, turn led3 on, then:
If button3 is not pressed, turn everything off.

This code will always set all 3 outputs to low, except button3 is pressed, then led3 is HIGH.
So no, your code is not right.

Also, how do you intend to switch the audio signal? You need additional hardware to do that.

lg, couka

Thank you for your replay =) I am aware that I need additional hardware, but figured i would get the basic code running first =) Trying to get a basic understanding =) Should i be using "else if" instead?

DrAzzy:
The inputs are floating when the button isn't pressed - It's like you had nothing connected to the pin. With nothing at all on a high-impedance input, the wire is "floating", and it will pick up ambient electromagnetic noise and the voltage will move randomly between 0v and 5v (the internal clamping diodes keep it within that range). If you just read an analog pin connected to nothing and print the values to serial, you'll find that waving your hands near the pin will change the reading.

You need to use a pull-up or pull-down resistor on the input pin to ensure it has a known state when not connected to anything. IE, if pressing the button connects to Vcc, put a pull-down resistor to ground on the pin (so it will read 1 if pressed, and 0 otherwise). If pressing the button connects to Gnd, put a pull-up resistor to Vcc on the pin (so it reads 0 if pressed and 1 otherwise). 10k is a good value.

The easiest solution though, it to have the buttons connect to ground when pushed (so 0/LOW is pressed, 1/HIGH is not-pressed), and use the internal pullup resistor by doing pinMode(pin,INPUT_PULLUP).

Even if not using the internal pullups, best practice is to have the buttons connect to ground, not Vcc, so you don't have to run the supply wire to all your buttons.

Thank you for your comment, it explained a bit on why it was acting so irradical =) Using INPUT_PULLUP solved THAT issue =)

Now i get it =) When releasing a button none of the statements are true and therefor releasing a button turns LED of.

By removing the following the code works as i want it to =)

  else {
  digitalWrite(led1, LOW);
  digitalWrite(led2, LOW);
  digitalWrite(led3, LOW);
  }

LED 1: Connected to 10 & GND
LED 2: Connected to 9 & GND
LED 3: Connected to 8 & GND

Where's the 3 current limiting resistors?

dlloyd:
Where's the 3 current limiting resistors?

Acually not using any due to lack of them :confused: Is this damaging my Arduino?

Yes ... damaging to the outputs and the LEDs. The LEDs will clamp the output voltage to around 2V. This will draw excessive current from each output - could be greater than 40mA. Need to limit the current to 20mA or less. For red LEDs and 5V power, use I=V/R where V=(5V-2V)=3V.

[color=teal][b]Resistor  LED current[/b][/color]
180Ω      16.7mA
220Ω      13.6mA
270Ω      11.1mA
330Ω       9.1mA
360Ω       8.3mA
390Ω       7.7mA
430Ω       7.0mA
470Ω       6.4mA
510Ω       5.9mA