Question: Step 4 : Write ( and debug ) the software First, write software to make the brightness of LED 1 be proportional to the potentiometer
Step : Write and debug the software
First, write software to make the brightness of LED be proportional to the potentiometer value. This breaks down into three steps.
A Use analogRead to read the input into a variable.
B You need to scale it Think about it V reads and should correspond to an analogWrite value of It becomes clearer when you round them both up the ratio of input to output is : or : So if you divide the input by you get the correct output value.
C Use analogWrite to write the correct output value to LED
I recommend implementing everything to this point to make sure it works. When you turn the potentiometer, the LED gets brighter and dimmer.
Second, write software to make LED get brighter and dimmer depending on whether Button is pressed or released. Pressed make it brighter, Released make it dimmer
This step is much harder than it sounds, for several reasons.
For one thing, you need to get the button to affect LED while at the same time reading the potentiometer and controlling LED For another thing, you can't just keep incrementing or decrementing you have to stop when you hit the minimum or maximum.
To handle the first problem, we are going to do a small bit of what is called cooperative multitasking." Inside the loop function, which is called repeatedly, you should check the potentiometer and set LED and then check the button and just nudge the LED value. By checking and nudging, you can alternate very quickly between updating LED and updating LED with the illusion that they are being done at the same time. It is cooperative the two steps run quickly and it is multitasking both tasks are being done in parallel.
To manage the brightness of LED create a variable named something like "LEDbrightness". Hey why not be literal? This automatically creates a third problem if you declare LEDbrightness inside the loop function, it goes away every time loop exits. The loop function acts like it has no shortterm memory. This is because if you declare it inside loop, it is local, and it ceases to exist when loop exits. So the first "trick" is to declare LEDbrightness outside the loop function make it global on purpose.
Next, inside loop, write code to nudge the value. It should look something like this...
if digitalReadBUTTONINHIGH
LEDbrightess;
else
LEDbrightness;
analogWriteLEDLEDbrightness;
delay;
Notice how I am using "BUTTONIN and "LED instead of actual pin numbers meaning, I #defined it further up in my program. I stuck in a delay because, without it it gets bright too fast and you can't see it
At this point, I recommend implementing this and making sure it sortof works. You should notice at least two problems: the brightness will appear to cycle if you keep pressing the button. Also, go back and check the assignment should pressing the button make it get brighter or dimmer?
To keep it from cycling through brightness, you need to add range checking. For example, if you increment the brightness above analogWrite doesn't work correctly.
Range checking might look like this:
if LEDbrightness LEDbrightness ;
Of course, you also need to rangecheck when you decrement. And you need to make sure it gets brighter when you press the button.
Once you get the software straight, you should be able to turn the potentiometer while pressing and releasing the button. Each LED should get brighter and dimmer simultaneously.
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
