//For more Projects: www.arduinocircuit.com
/* In this example we will show how to select
the LED to turn on with two Up and Down buttons */
// Pin definition
int led1 = 11;
int led2 = 12;
int led3 = 13;
int buttonUp = 2;
int buttonDown = 3;
// Definition of the variables
int led = 1; // led to turn on
void setup() {
// Set pins as outputs
pinMode (led1, OUTPUT);
pinMode (led2, OUTPUT);
pinMode (led3, OUTPUT);
// Setting the pins as inputs
pinMode(buttonUp, INPUT);
pinMode (buttonDown, INPUT);
// Enabling of the internal PullUp resistors on the inputs
digitalWrite(buttonUp, HIGH);
digitalWrite(buttonDown, HIGH);
}
void loop() {
if (digitalRead (buttonUp) == 0){ // if the button is pressed
led++; // Increment the led variable
if (led>3) { // If the variable exceeds the value 3
led=1; // Reset the variable to 1
}
delay(500); // Pause for debounce
}
if (digitalRead (buttonDown) == 0){ // if the button is pressed
led--; // Decrement the led variable
if (led<1) { // If the variable is less than the value 1
led=3; // Reset the variable to 3
}
delay(500); // Pause for debounce
}
if (led==1) {
digitalWrite(led1, HIGH); // Turn on led1
digitalWrite (led2, LOW); // Turn off led2
digitalWrite (led3, LOW); // Turn off led3
}
if (led==2) {
digitalWrite(led1, LOW); // Turn off led1
digitalWrite(led2, HIGH); // Turn on led2
digitalWrite (led3, LOW); // Turn off led3
}
if (led==3) {
digitalWrite(led1, LOW); // Turn off led1
digitalWrite (led2, LOW); // Turn off led2
digitalWrite(led3, HIGH); // Turn on led3
}
}