I'm completely new to arduino and coding, and I'm having a problem with my breadboard and wiring.
What I want to do is control two led lights by one button, and when one light is on the other is off, and vice versa.
I already have the coding done, which I think is right.
Many thanks in advance.
int ledPin = 9;
int ledPin2 = 10;
int button = 11;
int state;
void setup () {
pinMode(ledPin, OUTPUT);
pinMode(ledPin2, OUTPUT);
pinMode(button, INPUT);
}
void loop() {
//digitalWrite(ledPin, HIGH); //the light is on
state = digitalRead(button); //stored the state of button
if(state == HIGH){ //if button is pushed
digitalWrite(ledPin, LOW);
digitalWrite(ledPin2, HIGH);
}
else{
digitalWrite(ledPin, HIGH);
digitalWrite(ledPin2, LOW);
}
}
The code looks fine, now wire the push button between the input and ground and enable the internal pull up resistor.
This will mean changing the line in the setup that reads:-
raschemmel:
Has anybody noticed the resistor is not connected to the top led ?
THANKS! Just fixed that and now I have it connecting. Nothing is still happening when it is connected though
Is there anything else wrong with my wiring?
Yes that switch is wired wrong, it is connecting the input to 5V when it is pressed and letting the input float when it is not pushed.
Read that link, an input pin that is not connected to anything is not a stable input, it could read anything. Wire it up like I told you to.
And never post such a stupidly large picture again. Just click on it, totally useless.