Concept
A potentiometer controls the brightness of two LEDs, a red one and a green one. It turns the green one on after reaching the max brightness on the red one, turning the red one off after it. On the other side, a switch controls two LEDs of the same colour. When pressed, it turns on the LED currently being turned on by the potentiometer.
Schematic
Program
I haven’t found a way to embed it, but you can access it by clicking here.
Behaviour
It works by showing the potentiometer’s control of brightness and the switch’s turning on and off the adequate LED. It works as expected in the latter stages of the project. It fulfills the assignment quota, since two LEDs are controlled by analog means and two other LEDs are controlled by digital means.
Problems
I originally wanted 4 LEDs for each, but the problem with arranging cables made the solution impractical and aesthetically unpleasing. On top of that, the problem with having 4 LEDs on an analog sensor is that the latter two LEDs kept turning on at the same time or behaved weirdly due to issues with the mapping on the input and the output.
Once it became two LEDs, the problem persisted where LEDs would reach max brightness then turn off and reach max brightness again before the other LED turned on. This was fixed, however, by programming the “analogWrite” to consider the “knobState” and divide it by 2 instead of using the “knobState” on its own.
As soon as the green LED turned on, however, even turning the knob all the way down would leave a very slight light on the green LED. This was fixed by adding an “else” to the “if” stating that the green LED should be set to 0 via “analogWrite”.
The button’s LEDs didn’t work with the bools at first; turned out the answer was to remove the “bool” inside the “if” statement and only add the name of the bool condition.
An unexpected behaviour once the switch and the potentiometer worked as expected was that, although the switch’s LEDs only turn on when the respective LED on the potentiometer part is on, if the switch is pressed as the potentiometer transitions from the red LED to the green LED or vice versa, it leaves the previous LED on while changing control to the other one.