Problem with simple LED circuit

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

Have you tried switching the buttons?

I'm not sure if something with your code is wrong, but you could have the same function with a lot less code, which also makes it easier to find bugs:

int x=3;
int del=700;
int buttonState = 0;
int buttonState2 = 0;

void setup() {
  for (int i=2; i<11; i++) {
    pinMode(i, OUTPUT);
  }
  pinMode(12, INPUT);
  pinMode(13, INPUT);
}

void loop() {
  buttonState = digitalRead(12);
  if (buttonState == HIGH) {
    digitalWrite(x, LOW);
    x++;
    if (x > 10) x = 2;
    digitalWrite(x, HIGH);
    delay(del);
  }
  
  buttonState2 = digitalRead(13);
  if (buttonState2 == HIGH) {
    digitalWrite(x, LOW);
    x--;
    if (x < 2) x = 10;
    digitalWrite(x, HIGH);
    delay(del);
  }

}

Circuit Please!

Have a look at my version: http://blog.blinkenlight.net/experiments/counting-resets/. However I do not use a standard IO pin for the button :wink:

If I understand your code right you do not activate the pullups. Thus the button pins will be floating. Thus the erratic button behavior. In order to activate the pullups you need to add

digitalWrite(12, HIGH); 
digitalWrite(13, HIGH);

It also looks as if you use the switches to pull the pins to 5V. If you follow my suggestion the switches must pull the pins to GND and the readings must be inverted.

I would also suggest to shorten the code to make it clearer what it actually does. Notice that my version has slightly poorer performance due to the way I render.

const int del=700;

void setup() {
  for (int pin=2; pin<11; pin++) {
    pinMode(pin, OUTPUT);
  }
  pinMode(12, INPUT);
  digitalWrite(12, HIGH); 

  pinMode(13, INPUT);
  digitalWrite(13, HIGH); 
}

void render_output_and_delay(uint8_t x) {
  for (uint8_t led=2; led<11; ++led) {
      digitalWrite(led, led==x);
  }
  delay(del);
}

void loop() {
  static int x=3;
  
  if (!digitalRead(12)) { 
    x = (x<10? x+1: 2);
    render_output_and_delay(x);
  }
  
  if (!digitalRead(13)) { 
    x = (x>2? x-1: 10);
    render_output_and_delay(x);
  }
}