Write a program that uses two pushbuttons to increase or decrease the brightness
ID: 2088302 • Letter: W
Question
Write a program that uses two pushbuttons to increase or decrease the brightness of the LED on pin #11 II. Your circuit should include an "up" button and a "down" button Pressing the up button should add ~ 10% brightness to the LED a. b. c. Pressing the down button should subtract ~ 10% brightness from the LED Hint: use a variable to hold the current duty cycle (dc) value. In loopO. check the up button; if pressed, add 25 to the variable. Also check the down button; if pressed, subtract 25 from the variable. Observe what happens as you exceed max dc or min de values possible and be ready to explain it to the TA. How would you fix it if you had more time?Explanation / Answer
const int brightnessUp = 10;
const int brightnessDown = 9;
const int ledPin = 11;
const int maxBrightness = 10;
int brightness = maxBrightness;
int interval=1;
void setup() {
pinMode(ledPin, OUTPUT);
pinMode(brightnessUp, INPUT);
pinMode(brightnessDown, INPUT);
Serial.begin(9600);
}
void loop(){
if (digitalRead(brightnessUp) == HIGH && brightness < maxBrightness){
brightness = brightness + interval;
Serial.println(brightness);
}
if (digitalRead(brightnessDown) == HIGH && brightness > 0){
brightness = brightness - interval;
Serial.println(brightness);
}
delay(100);
analogWrite(ledPin, map(brightness, 0, maxBrightness, 0, 255));
//this code maps the max brightness constant to the max LED brightness
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.