Hello all, first of all I'd like to say english is not my main language and that I am fairly new to this world so forgive my ignorance, I've been kind of self-teaching things and I may lack of some very important electronic basics.
I have been having some problems trying to make four push-buttons to light four separated LED, one for each button, I know it sounds very easy but I can't get it to work. the first button works fine but then other LED starts to light up like for no reason and I don't seem to be controlling them. I send you the code here and I will post an image of my wiring as soon as possible.
The code is basicly copying the button example three times, the problem may be in the wiring so I will post the image as soon as possible like I said.
const int buttonPin1 = 2;
const int buttonPin2 = 3;
const int buttonPin3 = 4;
const int ledPin1 = 5;
const int ledPin2 = 6;
const int ledPin3 = 7;
int buttonState1 = 0;
int buttonState2 = 0;
int buttonState3 = 0;
void setup() {
pinMode(ledPin1, OUTPUT);
pinMode(ledPin2, OUTPUT);
pinMode(ledPin3, OUTPUT);
pinMode(buttonPin1, INPUT);
pinMode(buttonPin2, INPUT);
pinMode(buttonPin3, INPUT);
}
void loop(){
buttonState1 = digitalRead(buttonPin1);
buttonState2 = digitalRead(buttonPin2);
buttonState3 = digitalRead(buttonPin3);
if (buttonState1 == HIGH) {
digitalWrite(ledPin1, HIGH);
}
else {
digitalWrite(ledPin1, LOW);
}
if (buttonState2 == HIGH) {
digitalWrite(ledPin2, HIGH);
}
else {
digitalWrite(ledPin2, LOW);
}
if (buttonState3 == HIGH) {
digitalWrite(ledPin3, HIGH);
}
else {
digitalWrite(ledPin3, LOW);
}
}
Thank you all for helping me out and sorry for asking this kind of so easy questions.
Sadly I dont know how to post an image here But I can describe how it is wired, It is like the arduino button example three times plus three LEDs wired to ground and to the LED pins by a resistor.
Your code looks ok.
One thing you might want to add at the end of Setup is:
digitalWrite(ledPin1, LOW);
digitalWrite(ledPin2, LOW);
digitalWrite(ledPin3, LOW);
Just to start out ensuring all LEDs will be off, but probably not necessary.
Hi, First of all thanks for responding to my post.
I think the problem is on the wiring too but I can't upload an image of how it is wired so if you could point at me how to do it I'll be thankful (: anyways I described how it is wired in the previous post maybe is a little hard to imagine but it's just like that example three times.
Finally I know it is asking too much but if anyone could take five minutes to make a Fritzing scheme on how I should wire it so It works with my code or with his, I'll be very very thankful.
be cool and wire up in this manner.
at this condition your initial input is low. When you press the switch, the resistor prevents you from short circuit and Behave as HIGH at digital input.
You should also add button debounce, as that may be causing the issues. http://arduino.cc/it/Tutorial/Debounce.
Also, in the diagram you have GND and + 5V going to the same rail on the breadboard, otherwiose it looks ok.