Multiple button-triggered LEDs

Hey,

I'm very sorry if this is a stupid or already asked question. Before someone drops a lmgtfy link, I DID search for it but was not able to find it.

For our project, we have multiple buttons (9 to be exact) that trigger their own cluster of LEDs (but testing the script now we only have 1 LED per button, so 9 LEDs). There are no double triggers, so when button 1 is pressed, only LED 1 will go on. Also, when released, the LEDs fade out.

We finally were able to script the first part correctly, button 1 triggering LED 1, and we tried expanding it to more LEDs, but when you press button 1, LED 1 goes on, and when you press button 2 when LED 1 is STILL on, LED 2 won't turn on until LED 1 has faded out, but we need the LEDs to be turned on at the same time!

const int buttonPin = 2;     // the number of the pushbutton pin
const int ledPin =  11;      // the number of the LED pin

// variables will change:
int buttonState = 0;         // variable for reading the pushbutton status

int brightness = 0;    // how bright the LED is
const int buttonPin2 = 4;     // the number of the pushbutton pin
const int ledPin2 =  10;      // the number of the LED pin

// variables will change:
int buttonState2 = 0;         // variable for reading the pushbutton status

int brightness2 = 0;    // how bright the LED is
int fadeAmount = 5;

void setup() {
  // initialize the LED pin as an output:
  pinMode(ledPin, OUTPUT);      
  // initialize the pushbutton pin as an input:
  pinMode(buttonPin, INPUT);   
  // initialize the LED pin as an output:
  pinMode(ledPin2, OUTPUT);      
  // initialize the pushbutton pin as an input:
  pinMode(buttonPin2, INPUT);     
}

void loop(){
  // read the state of the pushbutton value:
  buttonState = digitalRead(buttonPin);

  // check if the pushbutton is pressed.
  // if it is, the buttonState is HIGH:
  if (buttonState == HIGH) {     
    // turn LED on:    
    brightness = 255;
    analogWrite(11, 255);
    digitalWrite(ledPin, HIGH);
  } 
  else{
    while (brightness >= 0 && buttonState != HIGH) {
      analogWrite(11, brightness);
      brightness = brightness - fadeAmount;
      delay(70);
      buttonState = digitalRead(buttonPin);
   
    } 
  }
  
  
    buttonState2 = digitalRead(buttonPin2);

  // check if the pushbutton is pressed.
  // if it is, the buttonState is HIGH:
  if (buttonState2 == HIGH) {     
    // turn LED on:    
    brightness2 = 255;
    analogWrite(10, 255);
    digitalWrite(ledPin2, HIGH);
  } 
  else{
    while (brightness2 >= 0 && buttonState2 != HIGH) {
      analogWrite(10, brightness2);
      brightness2 = brightness2 - fadeAmount;
      delay(70);
      buttonState2 = digitalRead(buttonPin2);
   
    } 
  }
}

Here's the code we used. Input 11 and 2 = LED 1 and button 1, input 10 and 4 = LED 2 and button 2

We're completely new to Arduino and some things we don't yet understand... please help us...

[quote author=Schonhage link=topic=82084.msg618253#msg618253 date=1323350787]
press button 1, LED 1 goes on, and when you press button 2 when LED 1 is STILL on, LED 2 won't turn on until LED 1 has faded out[/quote]

The reason is this line:
[code]while (brightness2 >= 0 && buttonState2 != HIGH) {

You are staying inside the while() loop to fade out the LED until brightness (or brightness2) becomes 0. Then the microcontroller moves on to the next line (or block) of code.

Since you are new, I can understand why you did the following. However, this doesn't make much practical sense:

  if (buttonState == HIGH) {     
    // turn LED on and other stuff...
  } 
  else{
    while (brightness >= 0 && buttonState != HIGH) {

[/code] Your while statement would only get executed IF buttonState is LOW. So "&& buttonState != HIGH" is always true because that condition only gets checked AFTER the if-statement checking the state of buttonState.

Which also means, the "else-clause" will get executed each time the loop() repeats and the button's state is low. Instead of doing a "while" loop, just make an if-statement in that else-clause.

  if (buttonState == HIGH)  {     
        // turn LED on and other stuff...
   } else {
       if (brightness >= 0) {    // this will execute on each iteration of loop(), but allow the rest of loop to be processed.
            analogWrite(11, brightness);
            brightness = brightness - fadeAmount;
            delay(70);   // this could bite you down the road.
            buttonState = digitalRead(buttonPin);
       }
    }

There are other optimizations you could be doing in your code, like using arrays for example. But this will hopefully put you on a path to get closer to what you want.