# Help with button, LED combinations

Okay, so I have my arduino wired up with 6 buttons (digital pins 2-7) lighting up 6 LEDs (pins 7-13). When I press each button it successfully lights up its corresponding LED. This is for a science museum showing how different chemicals in fireworks light up different colors. So if you press button 1 with (A) chemical, it will light up blue. Button 2 with chemical (B) will light up Red and so on. I now need to add combinations, so for example if you press button 1 it will light up LED 1, press button 2 it will light up LED 2, but if you press 1 and 2 together it lights up only LED 3, purple. Does anyone know how I can program these combinations? I was thinking I could do an “if” “then” code, but I’m not sure how to go about that. I hope I explained that well enough, if not I will be happy to clarify further. My code is below.
Thanks [/left]

``````/*
Button

Turns on and off a light emitting diode(LED) connected to digital
pin 13, when pressing a pushbutton attached to pin 2.

The circuit:
* LED attached from pin 13 to ground
* pushbutton attached to pin 2 from +5V
* 10K resistor attached to pin 2 from ground

* Note: on most Arduinos there is already an LED on the board
attached to pin 13.

created 2005
by DojoDave <http://www.0j0.org>
modified 30 Aug 2011
by Tom Igoe

This example code is in the public domain.

http://www.arduino.cc/en/Tutorial/Button
*/

// constants won't change. They're used here to
// set pin numbers:
const int buttonPin1 = 2;     // the number of the pushbutton pin
const int buttonPin2 = 3;
const int buttonPin3 = 4;
const int buttonPin4 = 5;
const int buttonPin5 = 6;
const int buttonPin6 = 7;
const int ledPin1 =  8;      // the number of the LED pin
const int ledPin2 = 9;
const int ledPin3 = 10;
const int ledPin4 = 11;
const int ledPin5 = 12;
const int ledPin6 = 13;

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

void setup() {
// initialize the LED pin as an output:
pinMode(ledPin1, OUTPUT);
pinMode(ledPin2, OUTPUT);
pinMode(ledPin3, OUTPUT);
pinMode(ledPin4, OUTPUT);
pinMode(ledPin5, OUTPUT);
pinMode(ledPin6, OUTPUT);
// initialize the pushbutton pin as an input:
pinMode(buttonPin1, INPUT);
pinMode(buttonPin2, INPUT);
pinMode(buttonPin3, INPUT);
pinMode(buttonPin4, INPUT);
pinMode(buttonPin5, INPUT);
pinMode(buttonPin6, INPUT);
}

void loop(){
// read the state of the pushbutton value:

// check if the pushbutton is pressed.
// if it is, the buttonState is HIGH:
if (buttonState == HIGH) {
// turn LED on:
digitalWrite(ledPin1, HIGH);

}
else {
// turn LED off:
digitalWrite(ledPin1, LOW);
}

// check if the pushbutton is pressed.
// if it is, the buttonState is HIGH:
if (buttonState == HIGH) {
// turn LED on:
digitalWrite(ledPin2, HIGH);

}
else {
// turn LED off:
digitalWrite(ledPin2, LOW);
}

// check if the pushbutton is pressed.
// if it is, the buttonState is HIGH:
if (buttonState == HIGH) {
// turn LED on:
digitalWrite(ledPin3, HIGH);

}
else {
// turn LED off:
digitalWrite(ledPin3, LOW);
}

// check if the pushbutton is pressed.
// if it is, the buttonState is HIGH:
if (buttonState == HIGH) {
// turn LED on:
digitalWrite(ledPin4, HIGH);

}
else {
// turn LED off:
digitalWrite(ledPin4, LOW);
}

// check if the pushbutton is pressed.
// if it is, the buttonState is HIGH:
if (buttonState == HIGH) {
// turn LED on:
digitalWrite(ledPin5, HIGH);

}
else {
// turn LED off:
digitalWrite(ledPin5, LOW);
}

// check if the pushbutton is pressed.
// if it is, the buttonState is HIGH:
if (buttonState == HIGH) {
// turn LED on:
digitalWrite(ledPin6, HIGH);

}
else {
// turn LED off:
digitalWrite(ledPin6, LOW);
}
}
``````

yes, you would certainly need the `if` statement and the "combo" part would come with the Boolean Operators 'and' and 'or' check them out here; http://arduino.cc/en/Reference/HomePage (down the left column)

You might consider using functions like this:

``````void setup() {
//usual start
}
void loop() {
// if button clicks 1 and 2 buttons
123light;
// if button clicks another combo
456light;
}
void 123light () {
digitalWrite(pinX, HIGH);
digitalWrite(pinY, HIGH);
digitalWrite(pinZ, HIGH);
}
void 456light() {
digitalWrite(pinA, HIGH);
digitalWrite(pinB, HIGH);
digitalWrite(pinC, HIGH);
}
``````

Basically just call a function if two buttons are clicked.

``````void loop() {
// if button clicks 1 and 2 buttons
123light;
// if button clicks another combo
456light;
``````

You might consider calling the functions, not merely evaluating their function pointers and discarding the result.

AWOL: ``` void loop() {    // if button clicks 1 and 2 buttons    123light;    // if button clicks another combo    456light; ```

You might consider calling the functions, not merely evaluating their function pointers and discarding the result.

or in "Newblish" - DON'T FORGET THE BRACKETS !