In: Computer Science
I am working on an Arduino Project. I would like to build a controllable RGB LED Nightlight (with a debounce code included). With each time pushing the button I want a different color. like the following
Initial state = LED off
first press= LED turns on Red
second Press = LED turns on Green
third Press = LED turns on Blue
Fourth Press = LED turns on Purple
Fifth Press = LED turns on Teal
sixth press= LED turns on Orange
7th press = LED turns on White
8th press = LED turns off again
if you put the comments with each line of the code, it will be highly appreciated.
//The code for nightlight LED is as shown below:
const int BLED=9; //Blue LED is connected Pin 9
const int GLED=10; //Green LED is connected Pin 10
const int RLED=11; //Red LED is connected Pin 11
const int BUTTON=2; // Button connected to pin 2
boolean lastButton = LOW; //To store the state of Last Button
boolean currentButton = LOW; //To store the state of Current
Button
int ledMode = 0; //Cycle between LED states
void setup()
{ /* Here, in the setup method, we set the pins as input or output.
All the three blue,red and green LED are set as output whereas
Button is an input*/
pinMode (BLED, OUTPUT);
pinMode (GLED, OUTPUT);
pinMode (RLED, OUTPUT);
pinMode (BUTTON, INPUT);
}
/*
* Here a Debouncing Function is used *The previous button state is
passed,
* current debounced button state is noted.
*/
boolean debounce(boolean last)
{
boolean curr = digitalRead(BUTTON); //Here we read the state of
button
if (last != current) //If they aren't the same
{
delay(5); //a 5ms delay is introduced
curr= digitalRead(BUTTON); //read again
}
return curr; //return the current button value
}
/*Here we set the LED colours as required. */
void setMode(int mode)
{ //To set RED colour, we disable the other LEDs
if (mode == 1)
{ digitalWrite(RLED, HIGH);
digitalWrite(GLED, LOW);
digitalWrite(BLED, LOW);
}
//To set GREEN colour, we disable the other LEDs
else if (mode == 2)
{ digitalWrite(RLED, LOW);
digitalWrite(GLED, HIGH);
digitalWrite(BLED, LOW);
}
// To set BLUE colour, disable other LEDs
else if (mode == 3)
{ digitalWrite(RLED, LOW);
digitalWrite(GLED, LOW);
digitalWrite(BLED, HIGH);
}
//As PURPLE is a combination of RED and BLUE, we provide values to both of them
else if (mode == 4)
{ analogWrite(RLED, 127);
analogWrite(GLED, 0);
analogWrite(BLED, 127);
}
//As TEAL is amixture of BLUE and GREEN, we provide values to both of them
else if (mode == 5)
{ analogWrite(RLED, 0);
analogWrite(GLED, 127);
analogWrite(BLED, 127);
}
//As ORANGE is a mixture of GREEN and RED,we provide values to both of them
else if (mode == 6)
{ analogWrite(RLED, 127);
analogWrite(GLED, 127);
analogWrite(BLED, 0);
}
//As WHITE is a mixture of GREEN and RED and BLUE,we provide values to all of them
else if (mode == 7)
{ analogWrite(RLED, 85);
analogWrite(GLED, 85);
analogWrite(BLED, 85);
}
//Off corresponds to mode 0, we set all the pins to low
else
{ digitalWrite(RLED, LOW);
digitalWrite(GLED, LOW);
digitalWrite(BLED, LOW);
}
}
void loop() //loop function is used for continuous
execution of code
{
currentButton = debounce(lastButton); //read deboucned state
if (lastButton == LOW && currentButton == HIGH) //if the
button was pressed…
{
ledMode++;
} //increment the LED value
lastButton = currentButton; //resetting button value
// reset the counter to 0 value, if all the different options are cycled.
if (ledMode == 8)
ledMode = 0;
setMode(ledMode); //change the LED state for starting it all over again
}
// Kindly upvote the answer, if it helped you.