2 buttons: left and right

Hello.
I bought 2 buttons like this: (different color, same number of legs)
;

On my breadboard i managed to give them 5V each by wiring them in parallel. Everything good untill now!

I have 3 leds on my breadboard and i want to control them using the buttons (left and right), to be more precisely:

Start with center led turned on and stay ON untill a button is pressed. Press left/right button, turn off the previous led (center one) and turn on the one on the left/on the right untill another button is pressed. But it doesn`t work: when i press right or left, sometimes all leds blink, or i need to press multiple times a button, sometimes it skips a led, it has many bugs…

This is my code:

int left=2;
int right=4;
int led[3]={11,12,13};
int LF;
int RG;  
int j=1;

void setup()
{
  pinMode(left, INPUT);
  pinMode(right, INPUT);
  for(int i=0;i<3;i++)
    pinMode(led[i], OUTPUT);
  digitalWrite(led[j],HIGH); //start with center led turned ON
}

void loop()
{
  LF = digitalRead(left); //read left button
  RG = digitalRead(right); //read right button
  if (LF == HIGH) // if left button is pressed
  {
    digitalWrite(led[j],LOW);
    j=j+1;
    if(j>2) //end of the array
      j=0;
    if(j<0) //the other end of the array
      j=2;
    digitalWrite(led[j], HIGH);
  }
  if (RG == HIGH) //if right button is pressed
  {
    digitalWrite(led[j],LOW);
    j=j-1;
    if(j>2) //end of the array
      j=0;
    if(j<0) //the other end of the array
      j=2;
    digitalWrite(led[j], HIGH);
  }
}

Hope someone can repair my code… TY!

Before we look at the code exactly how are these buttons wired up.
Have you got a diagram?
It sounds like you have a floating input.
Read this:-
http://www.thebox.myzen.co.uk/Tutorial/Inputs.html

I don`t know if there is a program where you can draw the circuit, so i used Paint:

Red Circles = leds
Black Squares = buttons
Blue Rectangles = resistors

Red Cables = power for buttons (5 v)
Black Cables = Ground for leds and buttons
Rest of Cables = wiring to arduino

Haven't checked the logic of your code, but you are not debouncing your pushbutton switches.

explanation --> http://www.arduino.cc/en/tutorial/switch

Tried to add the debounce, same result: all flicker, same led flickers twice, and other bugs...

You have no resistors on the LEDs that will destroy your Arduino in time.

It is hard to see if those buttons are right with the way you have shown it.
What value are those resistors?

Pen and paper are good to draw schematics, then post a photo of the result.

Tried to add the debounce,

So post that.

Let me buy some 220Ohms resistors (tomorrow), wire them, and i will give you some pictures...

I bought some 220 ohm resistors and 4 x 20K ohms (wired in parallel 2 - to obtain 10K ohms)... And my setup looks like this:


Did i wired corectly the 2x20K ohms in parallel to obtain 10K for the button?

Wire Colors:
Yellow = 5V (Orange and Red should proive 5V each (parallel) for the 2 buttons)
Brown = GND
Orange + Green = left button pin 2 and right button pin 4
Purple + Green + Red = pins 11 12 13 for leds

But the code doesnt work... When i press a button, all leds turn on. After that sometimes the correct led stays on, sometimes other led stays on. I mean... the code doesnt do what i want to do :0

Look at what happenes when i upload the code and push buttons:

It looks like you have not wired those LEDs in series with the resistors, they all look to be on the same line so the resistors are shorting out.

Thanks for the video.

What you are forgetting is that the the loop function repeats many hundreds of times a second, so when you have a button held down it just zips through lighting all the LEDs in turn. That is why it looks like they are all on. One way round this is to wait until a button is released before looking for anything else. One way to do this is here:-

int left=2;
int right=4;
int led[3]={11,12,13};
int LF;
int RG;  
int j=1;

void setup()
{
  pinMode(left, INPUT);
  pinMode(right, INPUT);
  for(int i=0;i<3;i++)
    pinMode(led[i], OUTPUT);
  digitalWrite(led[j],HIGH); //start with center led turned ON
}

void loop()
{
  LF = digitalRead(left); //read left button
  RG = digitalRead(right); //read right button
  if (LF == HIGH) // if left button is pressed
  {
    digitalWrite(led[j],LOW);
    j=j+1;
    if(j>2) //end of the array
      j=0;
    if(j<0) //the other end of the array
      j=2;
    digitalWrite(led[j], HIGH);
    while(digitalRead(left) == HIGH) { } // wait until you release the button
    delay(30); // debounce delay
  }
  if (RG == HIGH) //if right button is pressed
  {
    digitalWrite(led[j],LOW);
    j=j-1;
    if(j>2) //end of the array
      j=0;
    if(j<0) //the other end of the array
      j=2;
    digitalWrite(led[j], HIGH);
    while(digitalRead(right) == HIGH) { } // wait until you release the button
    delay(30); // debounce delay
  }
}

The other way to do this is to look not for a high but for an edge. That is when the button is high but the last time round the loop it was low. This looks for a change in the button state not simply just a level.

You do that like this:-

int left=2;
int right=4;
int led[3]={11,12,13};
int LF;
int RG;  
int j=1;

void setup()
{
  pinMode(left, INPUT);
  pinMode(right, INPUT);
  for(int i=0;i<3;i++)
    pinMode(led[i], OUTPUT);
  digitalWrite(led[j],HIGH); //start with center led turned ON
}

void loop()
{
  static int lastLeft,lastRight;
  LF = digitalRead(left); //read left button
  RG = digitalRead(right); //read right button
  if ((LF == HIGH) && (LF != lastLeft)) // if left button has just been pressed
  {
    digitalWrite(led[j],LOW);
    j=j+1;
    if(j>2) //end of the array
      j=0;
    if(j<0) //the other end of the array
      j=2;
    digitalWrite(led[j], HIGH);
    delay(30);
  }
  if ((RG == HIGH) && (RG != lastRight) ) //if right button has just been pressed
  {
    digitalWrite(led[j],LOW);
    j=j-1;
    if(j>2) //end of the array
      j=0;
    if(j<0) //the other end of the array
      j=2;
    digitalWrite(led[j], HIGH);
    delay(30);
  }
  lastLeft = LF;  // save current values so you can detect a edge rather than a just a push
  lastRight = RG;
}

It works!! But left goes right and right goes left. I will edit that...

Can you explain me how your code works... i mean how does those lastleft and lastright help me?

If you look in the arduino IDE under File -> examples -> 02.Digital -> StateChangeDetection it gives you another example.

What it does is to detect the first time that the button is pressed and ignores when it is held down.

You see when the code is like you had it you incremented the LED you wanted to turn on every time round the loop. This meant this happened thousands of times a second. That last code only triggers your LEDs when the last time round the loop the button was unpressed and this time round the loop it is pressed. Therefore you only detect when the button changes not all the time it is held down.