Button that goes through multiple LEDs

I am trying to have a button that will turn a LED on with one click then clicked again change to another, then click again to another than and other click to turn off. I am struggling hard here and i'm a total beginner. :(

Show us what you have; use code tags when posting code. ``` [code] ``` your code here ``` [/code] ``` will result in

your code here

Let's assume that you have four LEDs. All are initially off. Keep a counter to count the button presses. One each button press, increment the counter.

If counter equals 1, switch all leds off, switch LED1 on. If counter equals 2, switch all leds off, switch LED2 on. If counter equals 3, switch all leds off, switch LED3 on. If counter equals 4, switch all leds off, switch LED4 on. If counter equals 5, reset counter to 0

Advisable; wire button between pin and ground and use pinMode(yourButtonPin, INPUT_PULLUP). Test for pin going low. This is the easiest way to prevent floating pins if the button is not pressed.

Also take into account bouncing; one press might result in multiple activations.

Use the serial port (and e.g. serial monitor) for debugging.

It could be done this way

#include <Bounce2.h>  //https://github.com/thomasfredericks/Bounce2

const byte ledPins[3] = { 3, 4, 5 };
const byte buttonPin = 2;
const byte numberOfLeds = sizeof(ledPins) / sizeof(ledPins[0]);

Bounce button;

byte whichLedIsOn;  // 0 means none lit

void setup() {
  // button connects to GND
  button.attach(buttonPin, INPUT_PULLUP);
  for (byte idx = 0; idx < numberOfLeds; idx++) {
    pinMode(ledPins[idx], OUTPUT);

void loop() {
  if (button.fell()) {
    // go to next led (could be first), reset if end reached
    if (++whichLedIsOn > numberOfLeds) {
      whichLedIsOn = 0;
    // switch all leds to the 'I am selected' state
    for (byte idx = 0; idx < numberOfLeds; idx++) {
      digitalWrite(ledPins[idx], idx == (whichLedIsOn - 1));

an error came up:

compilation terminated.

exit status 1 Error compiling for board Arduino/Genuino Uno.

You probably did not install the Bounce2 library.


The code is compiled and tested on a Nano.

The output window contains far more information than what you showed. Click in the output window, scroll to the top, select and copy ALL text and paste it here (using code tags as shown earlier).

I compiled the above code and this is the full error

C:\Users\sterretje\Documents\Arduino\sketch_may02a\sketch_may02a.ino:1:68: fatal error: Bounce2.h: No such file or directory

 #include <Bounce2.h>  //https://github.com/thomasfredericks/Bounce2
compilation terminated.

exit status 1
Error compiling.

As Whandall suspected, you probably don’t have the library installed.