Question: Step 4 : Write ( and debug ) the software First, write software to make the brightness of LED 1 be proportional to the potentiometer

Step 4: Write (and debug) the software
First, write software to make the brightness of LED1 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 --+5V reads 1023 and should correspond to an analogWrite value of 255. It becomes clearer when you round them both up -- the ratio of input to output is 1024:256 or 4:1. So if you divide the input by 4 you get the correct output value.
C) Use analogWrite to write the correct output value to LED1.
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 LED2 get brighter and dimmer depending on whether Button 1 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 LED2 while at the same time reading the potentiometer and controlling LED1. 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 "co-operative multitasking." Inside the loop() function, which is called repeatedly, you should check the potentiometer and set LED1, and then check the button and just nudge the LED2 value. By checking and nudging, you can alternate very quickly between updating LED1 and updating LED2, 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 LED2, create a variable named something like "LED2brightness". (Hey, why not be literal?) This automatically creates a third problem -- if you declare LED2brightness inside the loop() function, it goes away every time loop() exits. The loop function acts like it has no short-term 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 LED2brightness 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 (digitalRead(BUTTON_IN)==HIGH){
LED2brightess++;
} else {
LED2brightness--;
}
analogWrite(LED2,LED2brightness);
delay(20);
Notice how I am using "BUTTON_IN" and "LED2" 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 sort-of 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 255, analogWrite doesn't work correctly.
Range checking might look like this:
if (LED2brightness >255){LED2brightness =255;}
Of course, you also need to range-check 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 blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!