Challenge 1: Can you control two lights with the same brightness?
The bright and fading lights can probably be controlled via the analog pins instead of the digital ones. Based on what we've learned before, we know that the brightness is controlled from 0 to 255, but the potentiometer is going to give us a reading from 0 to 1023.
This means, we'll need to adjust the value from the potentiometer. This is going to be a bit annoying because of integer arithmetic. For example, if we read the number 100 from the potentiometer, to scale it on a range from 0 to 255, we would normally just say "100 divided by 1024 times 255." Unfortunately, since 100 is stored as an integer, doing 100 / 1024 gives us zero, because integer division takes the floor function (rounds down) to the nearest integer. So zero times 255 would have to still be zero.
To fix this, we need to trick the computer into doing "normal arithmetic" and then convert that answer back to an integer to store it. Using the value 1024.0 tricks the computer into doing normal arithmetic, and then we use static_cast<int>() to convert the answer from a double back to an integer.
We can now make the brightness of both LEDs increase or decrease together.
However, this still didn't work!
Through some Googling, we found that we need to be using the pins marked with a ~ on the board, since that means they are PWM (Pulse Width Modulation) outputs, which allow you to vary the levels of power by turning the signal on and off very quickly. Once we make that change, then everything works great!
Challenge 2: With just one potentiometer can you control two lights so that as one light gets dimmer, the other gets brighter?

No comments:
Post a Comment