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 http://www.arduino.cc/playground/Code/Buttons seems even closer but I am not sure.
Please, can somebody help me with some directions? :-[
//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(){
if(button.isPressed()){
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.
//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(){
pinMode(buttonPin,INPUT);
for (byte i=0; i<NUMBER_OF_LEDS; i++){
pinMode(ledPins*,OUTPUT);*
}* } 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*
* } } } [/quote][/edit]*_
I used the library “Button” located at http://www.arduino.cc/playground/Code/Button , 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.
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
{
Serial.begin(19200);
pinMode(GreenLed, OUTPUT);
pinMode(YellowLed, OUTPUT);
pinMode(RedLed, OUTPUT);
DateTime.sync(time);
}
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;
delay(200);
}
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;
delay(200);
}
}
while (DateTime.now() < (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: ");
Serial.println(LED);
delay(10000); // just some time to stop the Arduino serial monitor and read before it restarts.
}