Using a Button to Sequentially Turn LEDs On...Confused

I'm currently taking a course for Computer Programming using the Arduino IDE and this current objective has gotten me confused on how to program. The objective is to use the Arduino with 4 LEDs and a button as a switch. When the button is pressed once, one LED turns on. When the button is pressed again, the next LED turns on, and ect. until all are lit. The 5th button press should turn them all off. Currently my setup is both the Arduino and the breadboard, with leads coming from the pins to 220 ohm resistors, to the positives on the LEDs and then grounded on the negatives. I believe using and 'if' statement with 'case' statements would be best but I cannot figure out how to use it as a switch and not just a momentary switch when the button changes state. I know this code is not complete in all that I have to do, I just need some guidance.

int ledPin1 = 12;

int ledPin2 = 11;

int ledPin3 = 10;

int ledPin4 = 9;

int buttonPress1 = 2;

int countBP = 0;

 

 

void setup()

{

  pinMode(ledPin1, OUTPUT);

  pinMode(ledPin2, OUTPUT);

  pinMode(ledPin3, OUTPUT);

  pinMode (ledPin4, OUTPUT);

  pinMode(buttonPress1, INPUT_PULLUP);

}

 

 

void loop()

{

  digitalWrite(ledPin1, LOW);

  digitalWrite(ledPin2, LOW);

  digitalWrite(ledPin3, LOW);

  digitalWrite(ledPin4, LOW);

  countBP = digitalRead(buttonPress1);

 

  while (countBP != 0)

  {

    switch (countBP)

      {

        if (countBP == 0)

      {

     case 1:

      digitalWrite(ledPin1, HIGH);

      digitalWrite(ledPin2, LOW);

      digitalWrite(ledPin3, LOW);

      digitalWrite(ledPin4, LOW);

      countBP = countBP + digitalRead(buttonPress1);

      break;

      }

        if (countBP == 1)

      {

    case 2:

      digitalWrite(ledPin1, HIGH);

      digitalWrite(ledPin2, HIGH);

      digitalWrite(ledPin3, LOW);

      digitalWrite(ledPin4, LOW);

      countBP = countBP + digitalRead(buttonPress1);

      break;
      }

        if (countBP == 2)

      {

    case 3:

      digitalWrite(ledPin1, HIGH);

      digitalWrite(ledPin2, HIGH);

      digitalWrite(ledPin3, HIGH);

      digitalWrite(ledPin4, LOW);

      countBP = countBP + digitalRead(buttonPress1);

      break;
    }

    if (countBP == 3){

      case 4:

      digitalWrite(ledPin1, HIGH);

      digitalWrite(ledPin2, HIGH);

      digitalWrite(ledPin3, HIGH);

      digitalWrite(ledPin4, HIGH);

      countBP = countBP + digitalRead(buttonPress1);
      break;
    }

    default:

      digitalWrite(ledPin1, LOW);

      digitalWrite(ledPin2, LOW);

      digitalWrite(ledPin3, LOW);

      digitalWrite(ledPin4, LOW);

      countBP = 0;

      break; 

    }

  }

}

Uploaded to the Arduino and all the LEDs light up. However, when the button is pressed and held down, but it is entirely random when they shut off and when one turns on, ect. Sometimes some only dim and not shut off or turn on completely. Then when the button is released, they all light up again.

I might need a 'previousButtonState' possibly so it counts the change in state and moves forward? Does this setup need to be 'debounced'? I've been reading a bunch but I'm not sure in how to implement it. Thank you!

Look at the state change detection example (Files, Examples, Digital). You could use the buttonPressCounter variable and the switch statement to turn on/off the LEDs.

The example uses a switch to Vcc and pull down resistor so you will need to adjust the logic.

I don't think you have understood how a switch/case works. You don't need all the ifs in there.

Have a look at the tutorials for switch/case https://www.arduino.cc/en/Tutorial/SwitchCase and https://www.arduino.cc/en/Tutorial/SwitchCase2 and see if the examples look anything like your code.

Steve

You may not need to debounce the switch, but if you do a 0.1uf cap across the switch will (hardware) debounce the switch.

Just to make sure, your switch is wired from an input to ground? I make that assumption because of the INPUT_PULLUP.

Code:

---



```
class PushButton
{
public:
PushButton(uint8_t pin) // Constructor (executes when a PushButton object is created)
: pincolor=#000000[/color] { // remember the push button pin
pinMode(pin, INPUT_PULLUP); // enable the internal pull-up resistor
};
bool isPressedcolor=#000000[/color] // read the button state check if the button has been pressed, debounce the button as well
{
bool pressed = false;
bool state = digitalReadcolor=#000000[/color];              // read the button's state
int8_t stateChange = state - previousState;  // calculate the state change since last time

if (stateChange == falling) { // If the button is pressed (went from high to low)
        if (milliscolor=#000000[/color] - previousBounceTime > debounceTime) { // check if the time since the last bounce is higher than the threshold
          pressed = true; // the button is pressed
        }
      }
      if (stateChange == rising) { // if the button is released or bounces
        previousBounceTime = milliscolor=#000000[/color]; // remember when this happened
      }

previousState = state; // remember the current state
      return pressed; // return true if the button was pressed and didn't bounce
    };
  private:
    uint8_t pin;
    bool previousState = HIGH;
    unsigned long previousBounceTime = 0;

const static unsigned long debounceTime = 25;
    const static int8_t rising = HIGH - LOW;
    const static int8_t falling = LOW - HIGH;
};

const uint8_t ledPins[] = {12, 11, 10, 9};
const uint8_t nb_leds = sizeofcolor=#000000[/color] / sizeofcolor=#000000[/color];
PushButton pushbutton = {2};

void setupcolor=#000000[/color] {
  for (const uint8_t &ledPin : ledPins)
    pinMode(ledPin, OUTPUT);
}

void loopcolor=#000000[/color] {
  static uint8_t nb_presses = 0;
  if color=#000000[/color] {
    if (nb_presses == nb_leds) {
      for (const uint8_t &ledPin : ledPins)
        digitalWrite(ledPin, LOW);
      nb_presses = 0;
    } else {
      digitalWrite(ledPins[nb_presses], HIGH);
      nb_presses++;
    }
  }
}
```

|