Part 2 of the same session. The fade from PWM Fade with the counter torn out and a 10 kΩ pot dropped in its place. Read A0, rescale, write D9, about 50 times a second.
What the knob actually is
A pot is a voltage divider you can turn. The track is a resistive strip with 5 V at one end and GND at the other, so a continuous voltage gradient is sitting there in the material. The wiper is a sliding contact that touches one point on it and carries that point's voltage out of the middle pin.
Which means the two "resistors" are only track above the wiper and track below it. They always add to 10 kΩ and the knob just moves the split. Wiper sitting at 2 kΩ over 8 kΩ reads 4 V.
The useful part falls straight out of that. V at the wiper is Vcc × fraction, and the track resistance cancels, so a 1 kΩ pot and a 100 kΩ pot read the same voltage at the same angle. A divider sets a ratio, not a value. It also means the reading is ratiometric, so if the supply sags to 4.8 V the fraction is unchanged. Mechanically it is an angle sensor that answers in volts.
What I learned
analogReadis the real thing, not the impersonationanalogWriteis doing. A 10 bit ADC, 0 to 1023- Analog inputs need no
pinMode. For ADC purposes the pin is an input no matter what - They are not stuck that way though. A0 to A5 work fine as ordinary digital pins. What they cannot do is PWM, because no timer is wired to them
- One count is 4.88 mV, so the ±2 counts of jitter at the centre detent is about ±10 mV of actual electrical noise. Not a vague wobble, a number
map()wants the value plus both ends of the range it is in and both ends of the range I want. Integer math, so it truncates rather than rounds- It does not clamp either, it extrapolates. Hand it something outside the input
range and it cheerfully returns something outside the output range.
constrain()is the other half of it rawanddutyare one physical fact in two number systems.rawis the sensor's language coming in,dutyis the LED's going out,map()is the translator- 1024 into 256 is 4 raw counts per duty step, so three of every four knob
positions change nothing at the LED.
raw >> 2does the same job in one instruction, and either way most of the ADC jitter dies before it arrives - Half the photons looks about 73% as bright, so a linear ramp rushes the top end
and crawls at the bottom.
((long)duty * duty) >> 8bends the output by roughly the inverse of the eye's curve so the two cancel - The counter never changes. Only the value on the way out gets bent. I kept getting that backwards
- The cast has to come before the multiply.
((long)duty * duty)is fine,(long)(duty * duty)has already overflowed by the time the cast happens.intis 16 bits here, and it breaks at duty 182 - 256 rather than 255 because a power of two compiles to a shift, and the ATmega328P has no divide instruction. Full scale lands on 254 and nobody can tell
- Serial Plotter at 9600 graphs bare numbers against time, so printing
rawanddutygives two live traces - An analog input can shape time and not just level. Same knob setting a blink
period instead of a brightness, which was the first time
session 4's millis() and this
session's
analogReadhad to share one loop if (raw > 512)is a comparator in software with a threshold I can move at runtime. Half the "digital" modules in the kit are doing that in hardware with a trimmer on the back- This is the first complete read, map, write loop, and it is open loop. Nothing checks the LED. Burn it out and the code carries on printing happily
- Which is exactly what a servo adds. Same pot as mine, geared to the output shaft so the servo can read its own angle, then a chip comparing that against the commanded angle and driving toward the difference. The comparison is the whole thing, and PID is that with a smarter rule for turning the difference into drive