const int BLED=9; //Blue LED on Pin 9
const int GLED=10; //Green LED on Pin 10
const int RLED=11; //Red LED on Pin 11
const int BUTTON=2; //The Button is connected to pin 2
boolean lastButton = LOW; //Last Button State
boolean currentButton = LOW; //Current Button State
const int BUTTON2=3; //The Button is connected to pin 2
boolean lastButton1 = LOW; //Last Button State
boolean currentButton1 = LOW; //Current Button State
const int BUTTON3=4; //The Button is connected to pin 2
boolean lastButton2 = LOW; //Last Button State
boolean currentButton2 = LOW; //Current Button State
const int BUTTON4=5; //The Button is connected to pin 2
boolean lastButton3 = LOW; //Last Button State
boolean currentButton3 = LOW; //Current Button State
int ledMode = 0; //Cycle between LED states
void setup()
{
pinMode (BLED, OUTPUT); //Set Blue LED as Output
pinMode (GLED, OUTPUT); //Set Green LED as Output
pinMode (RLED, OUTPUT); //Set Red LED as Output
pinMode (BUTTON, INPUT); //Set button as input
pinMode (BUTTON2, INPUT); //Set button as input
pinMode (BUTTON3, INPUT); //Set button as input
pinMode (BUTTON4, INPUT); //Set button as input
}
boolean debounce(boolean last)
{
boolean current = digitalRead(BUTTON); //Read the button state
if (last != current) //if it's different...
{
delay(5); //wait 5ms
current = digitalRead(BUTTON); //read it again
}
return current; //return the current value
}
boolean debounce1(boolean Last)
{
boolean current1 = digitalRead(BUTTON2); //Read the button state
if (Last != current1) //if it's different...
{
delay(5); //wait 5ms
current1 = digitalRead(BUTTON2); //read it again
}
return current1; //return the current value
}
boolean debounce2(boolean Last1)
{
boolean current2 = digitalRead(BUTTON3); //Read the button state
if (Last1 != current2) //if it's different...
{
delay(5); //wait 5ms
current2 = digitalRead(BUTTON3); //read it again
}
return current2; //return the current value
}
boolean debounce3(boolean Last2)
{
boolean current3 = digitalRead(BUTTON4); //Read the button state
if (Last2 != current3) //if it's different...
{
delay(5); //wait 5ms
current3 = digitalRead(BUTTON4); //read it again
}
return current3; //return the current value
}
/*
* LED Mode Selection Pass a number for the LED state and set it accordingly.
*/
void setMode(int button)
{
switch (button)
{
//teal
case 1:
analogWrite(RLED, 0);
analogWrite(GLED, 128);
analogWrite(BLED, 128);
break;
//orange
case 2:
analogWrite(RLED, 255);
analogWrite(GLED, 165);
analogWrite(BLED, 0);
break;
//white
case 3:
analogWrite(RLED, 255);
analogWrite(GLED, 255);
analogWrite(BLED, 255);
break;
//OFF (mode = 0)
default:
analogWrite(RLED, 0);
analogWrite(GLED, 0);
analogWrite(BLED, 0);
}
}
void loop()
{
currentButton = debounce(lastButton); //read debounced state
if (lastButton == LOW && currentButton == HIGH) //if it was pressed...
{
ledMode++; //increment the LED value
}
lastButton = currentButton; //reset button value
if (ledMode == 1) ledMode = 0;
setMode(ledMode); //change the LED state
currentButton1 = debounce1(lastButton1); //read debounced state
if (lastButton1 == LOW && currentButton1 == HIGH) //if it was pressed...
{
ledMode++; //increment the LED value
}
lastButton1 = currentButton1; //reset button value
//if you've cycled through the different options,
//reset the counter to 0
if (ledMode == 2) ledMode = 0;
setMode(ledMode); //change the LED state
currentButton2 = debounce2(lastButton2); //read debounced state
if (lastButton2 == LOW && currentButton2 == HIGH) //if it was pressed...
{
ledMode++; //increment the LED value
}
lastButton2 = currentButton2; //reset button value
//if you've cycled through the different options,
//reset the counter to 0
if (ledMode == 3) ledMode = 0;
setMode(ledMode); //change the LED state
currentButton3 = debounce3(lastButton3); //read debounced state
if (lastButton3 == LOW && currentButton3 == HIGH) //if it was pressed...
{
ledMode++; //increment the LED value
}
lastButton3 = currentButton3; //reset button value
//if you've cycled through the different options,
//reset the counter to 0
if (ledMode == 4) ledMode = 0;
setMode(ledMode); //change the LED state
}
No comments:
Post a Comment