Using a Button to switch LEDs

So, I am heading to make a very simple game, and to do that, I have to get the buttons working.

The problem is this: When I push the button, I want something to happen only 1 time, not everytime the program runs while the button is beeing pressed. (example: press button, an other LED lits).

Here is a simple example I'm working with:

const int buttonPin = 2;
const int LED1 =  12;
const int LED2 = 13;

short int button = 0;
short int chase = 0;
boolean ready = true;

void setup()
{
  pinMode(LED1, OUTPUT);
  pinMode(LED2, OUTPUT);  
  pinMode(buttonPin, INPUT);     
}

void loop()
{
  button = digitalRead(buttonPin);
  
  if(ready == true)
  {
    if(button == HIGH)
    {
      chase++;
      ready = false;
    }
  }
  
  if(button == LOW)
  {
    ready = true;
  }
  
  if(chase > 1)
  {
    chase = 0;
  }
  
  if(chase == 0)
  {
    digitalWrite(LED1, HIGH);
    digitalWrite(LED2, LOW);
  }
  
  if (chase == 1)
  {
    digitalWrite(LED1, LOW);
    digitalWrite(LED2, HIGH);
  }
}

This is how I tried to solve it. But however, it doesn't work as I want it to do. It's kinda random if the LEDs' switching or not.

So every time the button is pressed, you want "chase" to go up 1?

How about some button debouncing:

void loop()
{
  if (digitalRead(buttonPin) == HIGH) {
    chase = 1 - chase;
    digitalWrite(LED1, chase);
    digitalWrite(LED2, 1-chase);
    delay(50);
    while (digitalRead(buttonPin) == HIGH) {}
    delay(50);
  }
}

Jeremy1998: Yes, since there is two chases (0 and 1), I am going up 1, but when "chase" is 2, it drops down to 0 again.

And yes, I could make a boolean instead, but this solution fits better, since after a while I will work with more chases.

drhex: Thanks for your help! I will try out this buttondebounching. Never heard of. Maybe I'll learn something new :)

I just want to write a new post here, as an update.

I tried the Button Debounching, and it worked! Thanks to drhex for the given code.

But to the update, I put in an extra button and an extra LED. The solution I was given did not fit with this new set-up. At least that I could think of.
However, I made myself a new and working code.

const int buttonLeft = 2;
const int buttonRight = 1;
const int LED1 =  12;
const int LED2 = 13;
const int LED3 = 11;

short int chase = 0;

void setup()
{
  pinMode(LED1, OUTPUT);
  pinMode(LED2, OUTPUT);  
  pinMode(LED3, OUTPUT);
  pinMode(buttonLeft, INPUT);
  pinMode(buttonRight, INPUT); 
}

void loop()
{
  if(chase > 2) { chase = 2; }
  if(chase < 0) { chase = 0; }
  
  if(chase == 0) { digitalWrite(LED1, HIGH); } else { digitalWrite(LED1, LOW); }
  if(chase == 1) { digitalWrite(LED2, HIGH); } else { digitalWrite(LED2, LOW); }
  if(chase == 2) { digitalWrite(LED3, HIGH); } else { digitalWrite(LED3, LOW); }
  
  if (digitalRead(buttonRight) == HIGH)
  {
    chase++;
    
    delay(50);
    while (digitalRead(buttonRight) == HIGH) {}
    delay(50);
  }
  
  if (digitalRead(buttonLeft) == HIGH)
  {
    chase--;
    
    delay(50);
    while (digitalRead(buttonLeft) == HIGH) {}
    delay(50);
  }
}

This works perfectly fine with what I have set up at the moment.
So now, I can add more LEDs (like a 3x3 screen with 4 buttons: up, down, left and right, and with a start/reset button to utilize the 14 pins).

But my new problem is this: If I am going to have something that moves or blinks at the screen the same time I am moving around, with the “while (digitalRead(buttonRight) == HIGH) {}” delay, the blinking/movement will not be active while pressing down any buttons.

Is there a solution for this?

EDIT: Multithread is the thing, right? Does Arduino handle this?

  if(chase > 2) { chase = 2; }
  if(chase < 0) { chase = 0; }

That’s what constrain is for:

chase = constrain(chase,0,2);
 if(chase == 0) { digitalWrite(LED1, HIGH); } else { digitalWrite(LED1, LOW);

Check out the ternary operator:

digitalWrite(LED1, chase==0? HIGH:LOW);

or even simpler (since HIGH==1 and LOW==0, which are also the results of a comparison in C) :wink:

digitalWrite(LED1, chase==0);

Multithread is the thing, right? Does Arduino handle this?

Search for threads on interrupts.

There is a Button library that you would use. It handles debouncing of buttons, unique button presses, etc. http://www.arduino.cc/playground/Code/Button

You can look at the source code for the library, to see how all the features are implemented.