1 pushbutton, 1 green LED, 1 red LED and some resistors, etc Requirments of how
ID: 2268767 • Letter: 1
Question
1 pushbutton, 1 green LED, 1 red LED and some resistors, etc Requirments of how the system should work 1. The green LED is connected to pin 10 and the red RED is connected to pin 9. 2. The pushbutton is connected to pin 2. 3. When the pushbutton is pressed, the green LED is on but the red LED is off. 4. When the pushbutton is unpressed, the green LED is off but the red LED is on. 5. Display the status of LEDs on the serial monitor. If the green LED is on and the red LED is off, show" Green on, Red off" If the green LED is off and the red LED is on, show " Green off, Red on"Explanation / Answer
/*
Adafruit Arduino - Lesson 3. RGB LED
*/
int redPin = 11;
int greenPin = 10;
int bluePin = 9;
//uncomment this line if using a Common Anode LED
//#define COMMON_ANODE
void setup()
{
pinMode(redPin, OUTPUT);
pinMode(greenPin, OUTPUT);
pinMode(bluePin, OUTPUT);
}
void loop()
{
setColor(255, 0, 0); // red
delay(1000);
setColor(0, 255, 0); // green
delay(1000);
setColor(0, 0, 255); // blue
delay(1000);
setColor(255, 255, 0); // yellow
delay(1000);
setColor(80, 0, 80); // purple
delay(1000);
setColor(0, 255, 255); // aqua
delay(1000);
}
void setColor(int red, int green, int blue)
{
#ifdef COMMON_ANODE
red = 255 - red;
green = 255 - green;
blue = 255 - blue;
#endif
analogWrite(redPin, red);
analogWrite(greenPin, green);
analogWrite(bluePin, blue);
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.