Problem 1: HW 5: RGB LED Expanded (associated Lab 3 RGB LED) Using the code in the RGB lab as a starting point, make the following modifications. Post the code and a video of it working in the homework submission. Homework: Modify your program to add the colors of Teal, Orange and White for a total of 7 colors and one off-state. Implement a switch statement to control the color mode instead of multiple if statements Add a second button that will toggle the blink mode delay between 0, 100 ms, 800 ms, and 6400 ms. Zero means no blinking.
this is the code:
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 Switch=2; //The Button is connected to pin 2
boolean lastSwitch = LOW; //Last Button State
boolean currentSwitch = LOW; //Current Button State
int ledMode = 0; //Cycle between LED states
boolean ledOn = false;
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 (Switch, INPUT); //Set button as input (not required)}
}
/** LED Mode Selection Pass a number for the LED state and set it accordingly.*/
void setMode(int mode)
{
//RED
if (mode == 1)
{
digitalWrite(RLED, HIGH);
digitalWrite(GLED, LOW);
digitalWrite(BLED, LOW);
}
//GREEN
else if (mode == 2)
{
digitalWrite(RLED, LOW);
digitalWrite(GLED, HIGH);
digitalWrite(BLED, LOW);
}
//BLUE
else if (mode == 3)
{
digitalWrite(RLED, LOW);
digitalWrite(GLED, LOW);
digitalWrite(BLED, HIGH);
}
//PURPLE (RED+BLUE)
else if (mode == 4)
{
analogWrite(RLED, 127);
analogWrite(GLED, 0);
analogWrite(BLED, 127);
}
//OFF (mode = 0)
else
{
digitalWrite(RLED, LOW);
digitalWrite(GLED, LOW);
digitalWrite(BLED, LOW);
}
}
boolean debounce (boolean last)
{
boolean current = digitalRead(Switch);
if (lastSwitch != current)
{
delay(5);
current = digitalRead(Switch);
}
return current;
}
void loop()
{
currentSwitch = debounce(lastSwitch); //read button state
if (lastSwitch == LOW && currentSwitch == HIGH) //if it was pressed...
{
ledMode++; //increment the LED value
}
lastSwitch = currentSwitch; //reset button value
//if you've cycled through the different options,
//reset the counter to 0
if (ledMode == 5) ledMode = 0;
setMode(ledMode); //change the LED state}
}


Trending now
This is a popular solution!
Step by step
Solved in 2 steps









