Problem with code or circuit.

Short description:

When I press the right button, it skips over the middle LED - most of the time - sometimes even when I press the left button. Soft press at the right: skip (90%), hard press at left: normal (70%).
Scroll down for picture and code. (Skip the "Long description if you don’t want to waste time.)

Long description:

I have come to a new problem in my programming, or maybe it’s the circuit that screws things up.
Everything works as intended, but not perfect when it comes to the buttons. What seems to happen is that when I press one of the buttons, it may skip over the middle LED - but mostly when I press the button hard… When I press softly, it may work as it should. What’s making me confused, is that the left button rarely skips the middle LED, I have to press it hard to make it work, but when it comes to the right button, it skips most of the time.

Click for image
“Hints”: Did I use wrong type of resistors for the buttons?
Since it sends “high - low - high” (it gets two presses, at least), I can only imagine some circuit fail somewhere.

Main program:

// Variables
const int LED1 = 13;    // Left LED
const int LED2 = 12;    // Middle LED
const int LED3 = 11;    // Right LED
const int bLeft = 10;   // Left button
const int bRight = 9;   // Right Button

short int playerX = 1;  // This is for the movement of the LED (set to start in the middle)

int milli = 0;          // Milliseconds counter
int sec = 0;            // Second counter
int curTime = millis(); // Check the current millisecond
int prevTime = 0;       // Check if curTime changes, then increase "mill"

#include <Nfunctions.h> // Our own function header

void setup()
  pinMode(LED1, OUTPUT);      // Left LED
  pinMode(LED2, OUTPUT);      // Middle LED
  pinMode(LED3, OUTPUT);      // Right LED
  pinMode(bLeft, INPUT);      // Left button
  pinMode(bRight, INPUT);     // Right button
  checkLED();                 // Lit up the middle LED at the start-up
  Serial.begin(9600);         // This is used to print "sec" to the Serial Monitor

void loop()



// Check which LED should be lit
void checkLED()
  playerX = constrain(playerX,0,2);
  digitalWrite(LED1, playerX == 0); digitalWrite(LED2, playerX == 1); digitalWrite(LED3, playerX == 2);

//Time check
void checkTime()
  curTime = millis();
  if(milli >= 975) { milli = 0; sec++; Serial.println(sec); }
  if(prevTime != curTime) { prevTime = curTime; milli++; }

// The controls
void checkButton (int bDir)
  if (digitalRead(bDir) == HIGH)
    if(bDir == bRight) { playerX++; } else { playerX--; }
    checkLED(); // Update player's LED
    // Don't check for further presdown until button is released
    while (digitalRead(bDir) == HIGH) { checkTime(); }

// The movement
void movement()


I over-commented just so you could follow easily.
I also want to inform you that the counting is vital for further programming - when I set up the “screen” with moving LEDs, without using “delay()”.

You need to "debounce" the button presses. It is common that buttons don't go nicely from on to off, but toggle a few times back and forth. You get around it by not looking at a button for the 10 or so milliseconds following a transition.

"Hints": Did I use wrong type of resistors for the buttons?

Yes you are using 100R pull down resistors. Not only does that waste a lot of current but it requires much more current to pass through the button. Try changing them to 10K and see if it improves. If it does not improve keep them at that value and look to your software.

I am embarased. I had that delay before and after the button check. I just put it away temorary because it slowed down the counting. Forgot to put it back there, and didn't thought that it should cause this. Thanks a lot!

And yes, I just replaced the 1K resistors to 10K resistors. Hope it will work nicely from now on.

Again, I am really happy to get replies so fast, and it shurely solved my problem!