One button for several outputs

Hi, It seems to be simple and probably a common issue, but I have searched the forum as well as the playground, without finding something close enough of having one button switching several digital outputs.

Lets say, we have a button (as input on digital 2), and outputs as a green LED (on dig 3), a yellow LED (dig 4) and a green LED (dig5).

Each time you press the button, a LED turns on and the previous one turns off, switching from green, to yellow and red and over.

My problem is on the sketch. A counter does not seems to work well. The sketch for debouncer button seems closer but not enough since it is only one LEd v/s one button. The Buttons library at seems even closer but I am not sure.

Please, can somebody help me with some directions? :-[

keep the great work! ;) thanks, gps

Maybe something like this:

#include <Button.h> //
#include <LED.h> //

//number of LEDs
const byte NUMBER_OF_LEDS = 3;

//our button connected @ pin 2
Button button = Button(2);

//array of leds, @ pins 3, 4 and 5
LED leds[NUMBER_OF_LEDS] = { LED(3), LED(4), LED(5) };

//index of current led
byte index = 0;

void setup(){/nothing to setup/}

void loop(){
index = ++index%NUMBER_OF_LEDS; //constrain to [0, NUMBER_OF_LEDS-1]
for (byte i=0; i<NUMBER_OF_LEDS; i++){
i==index ? leds[index].on() : leds[index].off();


[edit]Just realized that the code probably looks cryptic if you did not write it. So, I coded one more example, using nothing but standard Arduino API calls, and for, if and else control structures.

//numbers of LEDs
const byte NUMBER_OF_LEDS = 3;

//our button connected @ pin 2
byte buttonPin = 2;

//array of leds, @ pins 3, 4 and 5
byte ledPins[NUMBER_OF_LEDS] = {3,4,5};

//index of current led
byte index = 0;

void setup(){
for (byte i=0; i<NUMBER_OF_LEDS; i++){

  • }*
    void loop(){
  • //read states*
  • if(digitalRead(buttonPin)==LOW){*
  • index++; //increase index*
  • if (index>=NUMBER_OF_LEDS){*
  • index=0; //do not allow index to be equal or above NUMBER_OF_LEDS*
  • }*
  • }*
  • //loop through all leds*
  • for (byte i=0; i<NUMBER_OF_LEDS; i++){*
  • if (i==index){*
    _ digitalWrite(ledPins*,HIGH); //set the led @ current index HIGH*_
    * } else {*
    _ digitalWrite(ledPins*,LOW); //set all other than current index LOW*
    * }

Try to write i program that remembers which led was on last and so on.

I found a way to get it!!! :smiley:

I used the library “Button” located at , instead of “Buttons”. Also I am using the DateTime library to set some limit of time since a specific time/date to choose among the outputs (color leds), after which if not used it keeps the default value.

I did this for a function in a project where I set a default value used to select among a set of possible “modes” or device behaviors (here simplified as “LED”) but using a button I want to have 15 seconds to changed it manually in a standalone device without reconnecting it to the USB port.

I am sure this can be done better, so please feel free to help me (and others) to improve it. :wink:

This is the code for the three color LEDs (or several digital outputs) switched by one button, able to be selected during 15 seconds:

#include <Button.h>
#include <DateTime.h>

unsigned long time = 1244490882; // to set some time and date in unix_t format
char LED = ‘G’; // here can be any type of digital output
Button Switch = Button(2,PULLUP); // to assign the button to pin 2
int outputID = 0; // will serve as an identity of which output is active - 0 is here the default Green Led
int GreenLed = 3; // digital pin of the first output
int YellowLed = 4; // digital pin of the second output
int RedLed = 5; // digital pin of the third output
// only three outputs here but can be more

void setup() // run once, when the sketch starts
pinMode(GreenLed, OUTPUT);
pinMode(YellowLed, OUTPUT);
pinMode(RedLed, OUTPUT);

void loop() // main block that runs over and over again
Serial.println(“15 seconds to manually select with the button among the leds…”);
do {
if (Switch.isPressed() && outputID == 0){
Serial.println(“YELLOW led has been manually selected”);
LED = ‘Y’;
digitalWrite(GreenLed, LOW);
digitalWrite(YellowLed, HIGH);
outputID = 1;
delay(200); // needed to prevent noise due to the button
else if (Switch.wasPressed() && outputID == 1){
Serial.println(“RED led has been manually selected”);
LED = ‘R’;
digitalWrite(YellowLed, LOW);
digitalWrite(RedLed, HIGH);
outputID = 2;
else if (Switch.wasPressed() && outputID == 2){
Serial.println(“GREEN Led has been manually re-selected”);
LED = ‘G’;
digitalWrite(RedLed, LOW);
digitalWrite(GreenLed, HIGH);
outputID = 0;
while ( < (time + 15)); // here the time limit is set to 15s but it can be done also with a “for…”
Serial.print("TIME OVER - The final selected color Led is: ");
delay(10000); // just some time to stop the Arduino serial monitor and read before it restarts.

Thanks AlphaBeta and friends. I was writing this while you helped me. Thanks a lot. Your option looks way more short and pro than mine...

Thanks!!! ;) gps