Question

In: Computer Science

I am working on an Arduino Project. I would like to build a controllable RGB LED...

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.

Solutions

Expert Solution

//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.


Related Solutions

I am working on an accounting project related to a supermarket and would like to seek...
I am working on an accounting project related to a supermarket and would like to seek help with regard to its costing system. I was asked to describe the company’s costing system. I have identified supermarket as a value-added intermediary between suppliers and customers, but how do I classify or describe it's costing system since it does not manufacture its own product. Would there be a manufacturing cost incurred or I can only trace non-manufacturing costs to its products? I...
CODE WITH ARDUINO: With an RGB Led, Use the iterative loop (for loop) to make red,...
CODE WITH ARDUINO: With an RGB Led, Use the iterative loop (for loop) to make red, blue, and green. Use ‘analogWrite’ command inside the 'for loop.' Use the value ranges from 0-255 for this command. So, to activate any of the pins, the value should be 255. To turn off 0. pattern: First for loop Red light – delay – Second for loop for blue light – delay - Third for loop for green light - delay. (The resulting lights...
I am working on these questions but I would like someone to review them before I...
I am working on these questions but I would like someone to review them before I submit and add any addition information I may have missed or worse gotten wrong! I provided the questions and then my answers to them. 1) Explain what is blacklisting and whitelisting? 2) iptables: Compare -j DROP vs -j REJECT. Which option would you use to implement a firewall rule that blocks incoming packets and why? 3) State the iptables command you would use to...
i am working on a project.. i would love to implement ENERGY METERING WITH IT SO...
i am working on a project.. i would love to implement ENERGY METERING WITH IT SO AS TO BE ABLE TO CALCULATE THE BILLS AT THE END OF A CERTAIN PERIOD.. how could this be done...BULBS,FANS,SOCKETS are the appliances involved... how can i get the energy used by all this appliances. i need very DETAILED TECHNICAL EXPLANATION.
Hello guys i would to build a buck converter and use arduino PWM to control the...
Hello guys i would to build a buck converter and use arduino PWM to control the output voltage 12V to 8.4V any ideas ? thanks
I'm working in Java and am working on a project where I need to find an...
I'm working in Java and am working on a project where I need to find an average. The catch is that for some of the values there is no data because they will be entered at a later date. I have variables assigned so that for each entry if there is an input I'll have it say _____available = 1, otherwise the variable will equal 0. I'll use an example to make this more clear. Let's say I am trying...
I am working on a project that requires a measurement of acceleration. I have a ADXL345...
I am working on a project that requires a measurement of acceleration. I have a ADXL345 accelerometer, an aduino and a MAX7219 dot matrix display. I need help setting up a code for my aduino to display the acceleration from the ADXL345 accelerometer onto the MAX7219 display. Please help!
I would like for the project to be on Amazon The Project to be addressed by...
I would like for the project to be on Amazon The Project to be addressed by the Paper: You have just graduated from Keiser University’s MBA program and have secured a position as a fund manager for a well known investment banking house. You have been given $25 million to manage/invest in a single stock. The fund is a pension/retirement fund so its perspective is long term with moderate risk of loss of capital and a required return of 9%...
I am trying to solve this question: "You are working on a school project at the...
I am trying to solve this question: "You are working on a school project at the library when your friend Jane taps you on the shoulder. She cannot seem to connect to a certain website that she needs for her class. Fortunately, you know enough about Windows and networking to help troubleshoot the problem. You open a Windows command prompt and ......" The dots at the end of the story indicates that I need to continue the story. However, I...
I am in a statistics class. When I graduate, I would like to work in an...
I am in a statistics class. When I graduate, I would like to work in an area where I can help restructure businesses that are failing and make them successful. My professor has asked me where in my career that I think I will use the process of data collection. He has asked me to provide 2 examples. This is my first statistics class, so I don't really know of how to give him any examples. Can you please give...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT