Tri-LED and For Loop Question

Hey, I am a bit of an arduino noob so bare with me :) I am working on a university project which involves creating a music sequencer with Max/MSP.

The problem I am having at the moment is trying to control a tri-color LED using a single button.

For yellow Pin 12 needs to be on, for red Pin 13 needs to be on, for orange, both need to be on.

I have grabbed the button example from the arduino sketchbook and it is simple to just turn an LED on or off, or switch between yellow and red , but I am having trouble being able to switch the pins.

Here is the code so far, I am not sure how to use for loops properly, but I assume that is what is needed. I am also not sure how to turn on 2 pins at once. Here is the code:

//for = (led[1]; led[2]; led[3], led[2]; led[0];)

int led[] = {0, 12, 13}; // select led pin int inputPin = 2; // choose the input pin (for a pushbutton) int val = 0; // variable for reading the pin status

void setup() { pinMode(led[1], OUTPUT); // declare LED as output pinMode(inputPin, INPUT); // declare pushbutton as input }

void loop(){ val = digitalRead(inputPin); // read input value if (val == 0) { // check if the button is on digitalWrite(led[1], LOW); // turn LED OFF } else { digitalWrite(led[1], HIGH); // turn LED ON } }

Any help would be greatly apprieciated :)

As that was my first post I couldn't post a link, but here is our current WP blogroll:

(http://euphonyproject.wordpress.com/)

Untested code:

#define LEDPINS 2
#define RED 0
#define YELLOW 1
#define ORANGE 2

byte ledPins[LEDPINS] = {12, 13}; // select led pin
byte inputPin = 2; // choose the input pin (for a pushbutton)

byte colorState = RED;

void setup() {
for(byte i=0; i<LEDPINS; i++){
pinMode(ledPins*, OUTPUT); // declare LED as output*
}
pinMode(inputPin, INPUT); // declare pushbutton as input
digitalWrite(inputPin,HIGH); //wire switch directly between ground and inputPin
}
void loop(){
if (digitalRead(inputPin) == LOW) { // check if the button is on

  • colorState++;*
  • colorState>ORANGE ? colorState = RED : colorState;*
  • delay(20); //debounce?*
    }
    switch(colorState)
    {
  • case RED: digitalWrite(ledPins[1],HIGH); digitalWrite(ledPins[0],LOW); break;*
  • case YELLOW: digitalWrite(ledPins[0],HIGH); digitalWrite(ledPins[1],LOW); break;*
  • case ORANGE: digitalWrite(ledPins[0],HIGH); digitalWrite(ledPins[1],HIGH); break;*
    }
    }
    [/quote]

Hey. Thanks this nearly works. What happens is this:

Button Press > Red Button Press > Flash Between Red, Yellow and Orange Button Press > Yellow Button Press > Flash Between Red, Yellow and Orange Button Press > Orange Button Press > Flash Between Red, Yellow and Orange

What I need to happen is:

Button Press > Red Button Press > Yellow Button Press > Orange Button Press > LEDs OFF

I do not understand the code to be honest. I added another state, off. It seems to go like:

Blinking
Yellow
Blinking
Yellow
Blinking
Off
Blinking
Orange
Blinking
Red

Seems to be random arrgg.
Here is the code I changed:

#define LEDPINS 3
#define RED 0
#define YELLOW 1
#define ORANGE 2
#define OFF 3

byte ledPins[LEDPINS] = {12, 13}; // select led pin
byte inputPin = 2; // choose the input pin (for a pushbutton)

byte colorState = RED;

void setup() {
for(byte i=0; i<LEDPINS; i++){
pinMode(ledPins*, OUTPUT); // declare LED as output*
}
pinMode(inputPin, INPUT); // declare pushbutton as input
digitalWrite(inputPin, HIGH); //wire switch directly between ground and inputPin
}
void loop(){
if (digitalRead(inputPin) == 0) { // check if the button is on

  • colorState++;*
  • colorState>OFF ? colorState = RED : colorState;*
  • delay(20); //debounce?*
    }
    switch(colorState)
    {
  • case RED: digitalWrite(ledPins[0],LOW); digitalWrite(ledPins[1],HIGH); break;*
  • case YELLOW: digitalWrite(ledPins[0],HIGH); digitalWrite(ledPins[1],LOW); break;*
  • case ORANGE: digitalWrite(ledPins[0],HIGH); digitalWrite(ledPins[1],HIGH); break;*
  • case OFF: digitalWrite(ledPins[0],LOW); digitalWrite(ledPins[1],LOW); break;*
    }
    }[/quote]
    I also have no idea to get rid of the blining state in the middle of each press!

What if you change

delay(20); //debounce?

to

delay(200); //debounce?

I see what is happening now, its to do with the type of switch.

The buttons are latching so, you press it once, INPUT = HIGH, press it again, INPUT = LOW

Is there any way to get around this with the code? P.S thank you very much for the help :)

I'm trying to write up a (kind of) similar code and I plugged in your sketch and ran it on my board with a pushbutton and a Superflux RGB LED and about gave myself a seizure!