Initially, this blog post was going to be pretty dull. I was going to describe some of the things I've been learning: the difference between energy and current, how pulse-width modulation works, the nuances behind how a multimeter functions, and many more juicy tidbits that are SO fun to write about, and even MORE fun to read about.
jk that definitely would've been super boring - I made a timelapse of me making something instead!
This is the first idea that I've come up with on my own. I'm definitely not the first person to do this, and I'm sure it's nothing to write home about if you've passed the "light up an LED" stage of learning, but I'm proud of my little original creation (even if it's in the same way that a toddler is proud of the crappy sandcastle he just made).
In summary: Twist the little knob --> light fades back and forth ⭐
I think this concept could easily be expanded to create..
Even though I originally built this because I wanted to learn how to use potentiometers, it's effortless (literally < 2 mins of effort) to make the lights fade back and forth through other types of input as well (e.g. programmatic):
It was really fun to make a timelapse, even if I'm painfully aware of the rough patches (a new years resolution of mine is to just ship things and not worry so much about sweating the small stuff). A few lessons learned for if I want to do something like this again:
Here is the insanely messy whiteboarding I did to figure out how I wanted to jump through the hoops involved in converting from the voltage input value (a 10-bit number) to the output values (an 8-bit number) on a per-LED basis:
lol ignore my manic unrelated notes scattered throughout + the scribbles from my kids in the bottom-right corner
The code:
#define LED_1 11
#define LED_2 10
#define LED_3 9
#define LED_4 6
#define POTENTIOMETER_INPUT_PIN A0
const int MAX_POTENTIOMETER_VALUE = pow(2, 10) - 1;
const int MAX_ANALOG_WRITE_VALUE = pow(2, 8) - 1;
void setup() {
pinMode(LED_1, OUTPUT);
pinMode(LED_2, OUTPUT);
pinMode(LED_3, OUTPUT);
pinMode(LED_4, OUTPUT);
pinMode(POTENTIOMETER_INPUT_PIN, INPUT);
}
void loop() {
float input = analogRead(POTENTIOMETER_INPUT_PIN);
float inputStandardized = input / MAX_POTENTIOMETER_VALUE;
handleLEDBrightness(inputStandardized, LED_1, 0, 0.4);
handleLEDBrightness(inputStandardized, LED_2, .4, 0.2);
handleLEDBrightness(inputStandardized, LED_3, .6, 0.2);
handleLEDBrightness(inputStandardized, LED_4, 1, 0.4);
}
void handleLEDBrightness(float inputValue, int outputPin, float peakBrightnessValue, float deviationRange) {
float peakDelta = abs(peakBrightnessValue - inputValue);
float outputValueRaw = 1 - peakDelta / deviationRange;
float outputValue = max(outputValueRaw, 0) * MAX_ANALOG_WRITE_VALUE;
analogWrite(outputPin, outputValue);
}