Hey,
I'm currently working with Arduino for a simple project, where I need to have an array of lights, and make the lights switch to the next one when a button is pressed.
Like the following:
0 = LED that's off
1 = LED that's on
000000010
Press right button
000000001
Press right button
100000000
Press left button
000000001
Press left button
000000010
Etc.
Now I've gotten everything connected to the arduino, I'm rather sure there's nothing wrong with the buttons.
I've written the following code:
int x=3;
int del=700;
int buttonState = 0;
int buttonState2 = 0;
void setup() {
pinMode(2,OUTPUT);
pinMode(3,OUTPUT);
pinMode(4,OUTPUT);
pinMode(5,OUTPUT);
pinMode(6,OUTPUT);
pinMode(7,OUTPUT);
pinMode(8,OUTPUT);
pinMode(9,OUTPUT);
pinMode(10,OUTPUT);
pinMode(12,INPUT);
pinMode(13,INPUT);
}
void loop() {
buttonState = digitalRead(12);
if (buttonState == HIGH) {
if (x == 10) {
digitalWrite(10,LOW);
digitalWrite(2,HIGH);
x = 2;
delay(del);
}
else {
digitalWrite(x,LOW);
x = x+1;
digitalWrite(x,HIGH);
delay(del);
}}
buttonState2 = digitalRead(13);
if (buttonState2 == HIGH) {
if (x == 2) {
digitalWrite(2,LOW);
digitalWrite(10,HIGH);
x = 10;
delay(del);
}
else {
digitalWrite(x,LOW);
x = x-1;
digitalWrite(x,HIGH);
delay(del);
}}}
2-10 are the outputs for the buttons, 12 and 13 where the wires from the buttons go to.
When I only use the right button (second part of the code), it works fine.
When I use the left button too (doesn't matter if it's both right and left, or only left), it already starts going through the cycle as if I was constantly pressing the button, even though I don't press it at all.
Now I've use a volt meter thingy to make sure that there runs no current to port 12 when the button is not pressed, so the button isn't broken.
I've tried changing the output number from 12 to 11 too, but that didn't change anything either.
I think there's something wrong with code, but I can't figure it out.
Maybe any of you can see a flaw? It just compiles without an error.
It would be much appreciated if someone could help me with this.
If there's any more information you need to know, please ask me.
Thanks in advance,
Tanjim