Band 4 · Night Guard
Practice The dimmable lamp
A knob and a light, with three traps in it you can find with a pen before you find them with the board. One of them is Band 6 arriving two bands early.
Sketches for this project
Every file opens with a plain-English header saying what it does and how to wire it, hole by hole.
A lamp you can turn down
A knob and a light. Turn the knob and the light follows, smoothly, over the whole travel. Right down means off. Right up means full.
That sounds like the easiest thing on this page and it has three traps in it, all of which you can find with a pen before you find them with the board.
You have built a version of this already. b4_02_pot_dim_led.ino, in Band 4 Part 6, is a knob-driven lamp. Do it from scratch this time without opening that file, and the three traps below are exactly the ones that sketch quietly solved for you while you were reading it.
| You need | You already did it in |
|---|---|
analogWrite and why the pin needs a ~ | Band 2, Parts 2 and 5 |
analogRead, and the 0 to 1023 it gives you | Band 4, Parts 3 and 5 |
map, for turning one range into another | Band 4, Part 6 |
Three traps, on paper
10 min- The two ranges are not the same size.
analogReadhands you 0 to 1023.analogWritewants 0 to 255. Feed one straight into the other and only the first quarter of the knob behaves.
Predict what the other three quarters do, and write your prediction down before you build it. Does the lamp just stay bright once it passes 255? Something stranger? Then build it and turn the knob slowly from stop to stop, counting how many times the light goes from full bright to dark. Most people's prediction is wrong, and the real answer tells you something true about how the board stores a number. - Your knob probably does not reach both ends. Most do not quite make 0, and most do not quite make 1023. If yours stops at 12, your lamp never goes fully off. You will have to measure your own knob, exactly as Band 4 made you measure your own room.
- Your eye is not a straight line. Going from 0 to 60 looks like an enormous change. Going from 195 to 255 looks like almost nothing. So a knob that is mathematically even will not feel even, and no amount of careful code fixes that, because the unevenness is in you.
Build it, measure it, then fix what you find
25 minBoth bring rail wires with them, and so does the wire nobody counts: one from the − rail to a GND pin, and one from the + rail to 5V for the knob's power leg. Band 4 Part 5 names them. Rebuild without them and the knob reads nonsense rather than nothing, which is harder to spot.
Not Band 1 Part 5 for the light, even though that is the one you remember. Band 1 puts its LED in
f5 and f6, and Band 4 puts the knob's legs in f5, f6 and f7. Same holes. Band 4 Part 6 already moved the light out of the way, to f20 and f22, and it already put it on pin 9, which has the ~ this build needs. It also uses the same top − rail as the knob.First, print your knob before you drive anything with it. Turn it slowly all the way from one stop to the other, watching the serial monitor.
Now drive the lamp, using your two measured numbers rather than 0 and 1023.
Hold the knob completely still and watch the light for thirty seconds.
Then look at the lamp. You may see a faint shimmer in the dim third, and you may see nothing at all: two counts out of 1023 is under half a step out of 255, which is often invisible. Either way the wobble is there and it is being written to the light on every round.
Two ways further
- Bronze
- Make the bottom half of the knob's travel cover the bottom quarter of the brightness, so the dim end gets more room. Your eye will thank you. Then say in one sentence why that is a better lamp and a worse instrument.
- Silver
- Swap the knob for the light sensor, so the lamp gets brighter as the room gets darker. Careful: that is not just reversing the numbers, because a lamp that lights the room it is measuring will chase itself. Point it away, and write down what happened when you did not.
Keep
- Your knob's two real numbers, lowest and highest
- Your prediction for trap 1, written before you built it
- What you saw when you held the knob still, in your own words
Ship it
This one is yours. Nobody handed you a sketch for it, so the code in your video is code you wrote. Say that in the post, because it is the difference between somebody who can follow a tutorial and somebody who can build a thing, and it is the difference an employer is looking for.
The four shots as usual, and make the fourth one count: the check this page asked you to run, or the version that did not work first. Put a line of your own code on screen for three seconds. Almost nobody does, and it is the most convincing three seconds in the video.
Show your work has the template. Tag it #BozomaBuilds.