multiple LED cycled by button press

I am new to this, and this is my first time trying to code something myself, so please excuse any bad form in the programming.

I am simply trying to get three LED's to cycle which one is on based off of a button press. The code that I have will only turn on the first LED, and that is it. I can't figure out why, and after browsing the forums for a bit, I give up and am asking for help. I am using an Arduino UNO for this.

My code:

/*
 * Button_Press
 *
 * On a button press, will change
 * from one pin having high output
 * to another
 *
 */

// Pins used, and what they're used for
// out1 is first, followed by 2, then 3
const int out1 = 9;
const int out2 = 10;
const int out3 = 11;
const int in1 = 2;

void setup()
{
  //setting pins up for input and output
  pinMode(out1, OUTPUT);
  pinMode(out2, OUTPUT);
  pinMode(out3, OUTPUT);
  pinMode(in1, INPUT);
  
  //turning all ouputs off
  digitalWrite(out1, LOW);
  digitalWrite(out2, LOW);
  digitalWrite(out3, LOW);
}

void loop()
{
  int pressCount = 0; // used to count how many times button was pressed

  int buttonState; // Holds the button state. LOW if pushed, HIGH if not
  
  buttonState = digitalRead(in1); // Reading input from button
  
  // when button is pushed, pressCount increases
  if (buttonState == LOW)
  {
    pressCount++;
    
    // When presscount reaches 4, resets to 0
    if (pressCount == 4)
    pressCount = 0;
  }
  
  if (pressCount == 1)
  {
    digitalWrite(out1, HIGH); // Turns on the first LED
    
    digitalWrite(out2, LOW); // Makes sure the other 2 are off
    digitalWrite(out3, LOW);
  }
  else if (pressCount == 2)
  {
    digitalWrite(out2, HIGH); // Turns on the first LED
    
    digitalWrite(out1, LOW); // Makes sure the other 2 are off
    digitalWrite(out3, LOW);
  }
  else if (pressCount == 3)
  {
    digitalWrite(out3, HIGH); // Turns on the first LED
    
    digitalWrite(out1, LOW); // Makes sure the other 2 are off
    digitalWrite(out2, LOW);
  }
}

Pictures of the breadboard setup I am using: http://i.imgur.com/3kk5IBeh.jpg?1 http://i.imgur.com/vuvbhhQh.jpg?1

Sorry about the link to pictures.

I am using 30 ohm resistors with the LED's, and a 10k ohm resistor as a pull-up with the button.

If any other information is needed to help, please let me know

Local 'automatic' variables are initialized each time the function is started:

  int pressCount = 0; // used to count how many times button was pressed

That will set pressCount to 0 each time through loop().

You can fix it by declaring the variable 'static' so it is not created each time the function runs:

  static int pressCount = 0; // used to count how many times button was pressed

Thank you for pointing that out. I added static to that int declaration, but now when I push the button, all 3 LED's light up.

/*
 * Button_Press
 *
 * On a button press, will change
 * from one pin having high output
 * to another
 *
 */

// Pins used, and what they're used for
// out1 is first, followed by 2, then 3
const int out1 = 9;
const int out2 = 10;
const int out3 = 11;
const int in1 = 2;
static int pressCount = 0; // used to count how many times button was pressed

void setup()
{
  //setting pins up for input and output
  pinMode(out1, OUTPUT);
  pinMode(out2, OUTPUT);
  pinMode(out3, OUTPUT);
  pinMode(in1, INPUT);
  
  //turning all ouputs off
  digitalWrite(out1, LOW);
  digitalWrite(out2, LOW);
  digitalWrite(out3, LOW);
  
  
}

void loop()
{
  

  int buttonState; // Holds the button state. LOW if pushed, HIGH if not
  
  buttonState = digitalRead(in1); // Reading input from button
  
  // when button is pushed, pressCount increases
  if (buttonState == LOW)
  {
    pressCount++;
    
    // When presscount reaches 4, resets to 0
    if (pressCount == 4)
    pressCount = 0;
  }
  
  if (pressCount == 1)
  {
    digitalWrite(out1, HIGH); // Turns on the first LED
    
    digitalWrite(out2, LOW); // Makes sure the other 2 are off
    digitalWrite(out3, LOW);
  }
  else if (pressCount == 2)
  {
    digitalWrite(out2, HIGH); // Turns on the first LED
    
    digitalWrite(out1, LOW); // Makes sure the other 2 are off
    digitalWrite(out3, LOW);
  }
  else if (pressCount == 3)
  {
    digitalWrite(out3, HIGH); // Turns on the first LED
    
    digitalWrite(out1, LOW); // Makes sure the other 2 are off
    digitalWrite(out2, LOW);
  }
}

I tried this as well. Same issue occurred with the LED's all lighting up

Ok. Please disregard that last reply. I had a bad connection with a resistor. When I push the button, all the LED's light up, then one at random stays lit upon release of the button

astroninja21: When I push the button, all the LED's light up, then one at random stays lit upon release of the button

It is doing what you told it to. As long as the button is pressed it lights the LEDs in sequence. They are switching faster than you can see so it looks like all three are lit.

One thing you could do is, after the lights have been changed, wait for the button to be 'not pressed':

  else if (pressCount == 3)
  {
    digitalWrite(out3, HIGH); // Turns on the first LED
    
    digitalWrite(out1, LOW); // Makes sure the other 2 are off
    digitalWrite(out2, LOW);
  }

  while (digitalRead(in1)) ;  // Wait for button to be 'not pressed'

I tried this, and the same thing is still occurring. I added a serial write right after the pressCount change, and it shows what you are talking about. pressCount is being changed rapidly while the button is being pressed, even with the snippet of code that you suggested.

/*
 * Button_Press
 *
 * On a button press, will change
 * from one pin having high output
 * to another
 *
 */

// Pins used, and what they're used for
// out1 is first, followed by 2, then 3
const int out1 = 9;
const int out2 = 10;
const int out3 = 11;
const int in1 = 2;


void setup()
{
  //setting pins up for input and output
  pinMode(out1, OUTPUT);
  pinMode(out2, OUTPUT);
  pinMode(out3, OUTPUT);
  pinMode(in1, INPUT);
  
  //turning all ouputs off
  digitalWrite(out1, LOW);
  digitalWrite(out2, LOW);
  digitalWrite(out3, LOW);
  
  Serial.begin(9600);
}

void loop()
{
  static int pressCount = 0; // used to count how many times button was pressed

  int buttonState; // Holds the button state. LOW if pushed, HIGH if not
  
  buttonState = digitalRead(in1); // Reading input from button
  
  // when button is pushed, pressCount increases
  if (buttonState == LOW)
  {
    pressCount++;
    
    // When presscount reaches 4, resets to 0
    if (pressCount == 4)
    pressCount = 0;
    
    Serial.println(pressCount);
  }
  
  if (pressCount == 1)
  {
    digitalWrite(out1, HIGH); // Turns on the first LED
    
    digitalWrite(out2, LOW); // Makes sure the other 2 are off
    digitalWrite(out3, LOW);
  }
  else if (pressCount == 2)
  {
    digitalWrite(out2, HIGH); // Turns on the first LED
    
    digitalWrite(out1, LOW); // Makes sure the other 2 are off
    digitalWrite(out3, LOW);
  }
  else if (pressCount == 3)
  {
    digitalWrite(out3, HIGH); // Turns on the first LED
    
    digitalWrite(out1, LOW); // Makes sure the other 2 are off
    digitalWrite(out2, LOW);
  }
  
  while(digitalRead(in1)); // Wait for the button to be 'not pressed'
}

Oops, I got the sense wrong. Try this:

  while(digitalRead(in1) == LOW); // Wait for the button to be 'not pressed'

I added the "== LOW" into the statement, but now the button doesn't seem to be reliable. I tried 4 different buttons, and they all randomly decide to work or not. I think this is due to the code since these are all new buttons and I tried all the buttons I have with the same results

One common problem with buttons and switches is that the contacts often bounce. When they bounce you get several button closures and opens in a short period of time. Look up software methods of 'debouncing' button inputs.

Could you also get no closure with a bounce? I am running into that issue also

astroninja21: Could you also get no closure with a bounce? I am running into that issue also

You have the internal pull-up resistors turned on so the pin should read HIGH when the button is open and LOW when the button connects the pin to Ground. Do you have anything else connected to the Arduino pin? The only connections should be one siode of the button to the Arduino pin and the other side of the button to Ground.

Sorry about the long reply time. My buttons weren't connecting to my breadboard properly. I did end up getting this project to work. I will post the code soon as soon as I find it again.

Here is the code, as promised

And, I did use a library to do a software debounce. The library can be found here ===> http://playground.arduino.cc//Code/Bounce

#include <Bounce.h>

/*
 * Button_Press
 *
 * On a button press, will change
 * from one pin having high output
 * to another
 *
 */

// Pins used, and what they're used for
// out1 is first, followed by 2, then 3
const int out1 = 8;
const int out2 = 9;
const int out3 = 10;
const int out4 = 11;
const int out5 = 12;
const int in1 = 2;

int buttonState;
int pushCounter;
int lastButtonState;

Bounce debounce = Bounce();

void setup()
{
  //setting pins up for input and output
  pinMode(out1, OUTPUT);
  pinMode(out2, OUTPUT);
  pinMode(out3, OUTPUT);
  pinMode(out4, OUTPUT);
  pinMode(out5, OUTPUT);
  pinMode(in1, INPUT);
  
  debounce.attach(in1);
  debounce.interval(5);
    
  Serial.begin(9600);
}

void loop()
{
  debounce.update();
  
  buttonState = debounce.read();
  
  if (buttonState != lastButtonState)
  {
    if (buttonState == LOW)
    {
      pushCounter++;
      if (pushCounter == 5)
      {
        pushCounter = 0;
      }
    }
  }
  lastButtonState = buttonState;
  
  if (pushCounter == 0)
  {
    digitalWrite(out1, HIGH);
    digitalWrite(out2, LOW);
    digitalWrite(out3, LOW);
    digitalWrite(out4, LOW);
    digitalWrite(out5, LOW);
  }
  else if (pushCounter == 1)
  {
    digitalWrite(out1, LOW);
    digitalWrite(out2, HIGH);
    digitalWrite(out3, LOW);
    digitalWrite(out4, LOW);
    digitalWrite(out5, LOW);
  }
  else if (pushCounter == 2)
  {
    digitalWrite(out1, LOW);
    digitalWrite(out2, LOW);
    digitalWrite(out3, HIGH);
    digitalWrite(out4, LOW);
    digitalWrite(out5, LOW);
  }
  else if (pushCounter == 3)
  {
    digitalWrite(out1, LOW);
    digitalWrite(out2, LOW);
    digitalWrite(out3, LOW);
    digitalWrite(out4, HIGH);
    digitalWrite(out5, LOW);
  }
  else if (pushCounter == 4)
  {
    digitalWrite(out1, LOW);
    digitalWrite(out2, LOW);
    digitalWrite(out3, LOW);
    digitalWrite(out4, LOW);
    digitalWrite(out5, HIGH);
  }
}