Skip to content

Instantly share code, notes, and snippets.

@juliangoulding
Created March 25, 2013 06:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save juliangoulding/5235180 to your computer and use it in GitHub Desktop.
Save juliangoulding/5235180 to your computer and use it in GitHub Desktop.
I adapted the blink and button sketches to get the RGB led to change with each button pressed.
// set pin numbers:
const int buttonPin1 = 6; // the number of the pushbutton pin
const int ledPin1 = 11;
const int buttonPin2 = 3; // the number of the pushbutton pin
const int ledPin2 = 12; // the number of the LED pin
const int buttonPin3 = 2; // the number of the pushbutton pin
const int ledPin3 = 13;
const int buttonPin4 = 5; // the number of the pushbutton pin
const int buttonPin5 = 7; // the number of the pushbutton pin
// the number of the pushbutton pin
// variables will change:
int buttonState1 = 0;
int buttonState2 = 0;
int buttonState3 = 0;
int buttonState4 = 0;
int buttonState5 = 0;
void setup() {
// initialize the LED pin as an output:
pinMode(ledPin1, OUTPUT);
pinMode(buttonPin1, INPUT);
pinMode(ledPin2, OUTPUT);
pinMode(buttonPin2, INPUT);
pinMode(ledPin3, OUTPUT);
pinMode(buttonPin3, INPUT);
pinMode(buttonPin4, INPUT);
pinMode(buttonPin5, INPUT);
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for Leonardo only
}
}
void loop(){
// read the state of the pushbutton value:
buttonState1 = digitalRead(buttonPin1);
buttonState2 = digitalRead(buttonPin2);
buttonState3 = digitalRead(buttonPin3);
buttonState4 = digitalRead(buttonPin4);
buttonState5 = digitalRead(buttonPin5);
// check if the pushbutton is pressed.
// if it is, the buttonState is HIGH:
if (buttonState1 == HIGH) {
// turn LED on:
digitalWrite(ledPin1, HIGH);
Serial.println("btn1");
delay(1000);
}
else {
// turn LED off:
digitalWrite(ledPin1, LOW);
}
if (buttonState2 == HIGH) {
// turn LED on:
digitalWrite(ledPin2, HIGH);
Serial.println("btn2");
delay(1000);
}
else {
// turn LED off:
digitalWrite(ledPin2, LOW);
}
if (buttonState3 == HIGH) {
// turn LED on:
digitalWrite(ledPin3, HIGH);
Serial.println("btn3");
delay(1000);
}
else {
// turn LED off:
digitalWrite(ledPin3, LOW);
}
if (buttonState4 == HIGH) {
// turn LED on:
digitalWrite(ledPin1, HIGH);
digitalWrite(ledPin2, HIGH);
Serial.println("btn4");
delay(1000);
}
else {
// turn LED off:
digitalWrite(ledPin1, LOW);
digitalWrite(ledPin2, LOW);
}
if (buttonState5 == HIGH) {
// turn LED on:
digitalWrite(ledPin3, HIGH);
digitalWrite(ledPin2, HIGH);
digitalWrite(ledPin1, HIGH);
Serial.println("btn5");
delay(1000);
}
else {
// turn LED off:
digitalWrite(ledPin3, LOW);
digitalWrite(ledPin2, LOW);
digitalWrite(ledPin1, LOW);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment