Arduino Ladder · Bozoma Innovation Hub

Band 3  ·  Combination Lock

Practice  The toggle lamp, from nothing

One button, one lamp, press to toggle. You did it in Band 3 with the code in front of you. Do it again with nothing in front of you.

Time
25 min
You need
1 LED, 1 resistor, 1 button, 5 jumper wires (plus a second button if you do the Silver, and a ~ pin for the Bronze)
At once
Everybody at once
Before this
Finish Band 3 up to Part 9. This page gives you no sketch. That is deliberate.

Sketches for this project

Every file opens with a plain-English header saying what it does and how to wire it, hole by hole.

The job

One button, one lamp, press to toggle

The smallest useful device there is. Press once, light on. Press again, light off. It should work a hundred times out of a hundred.

You did this in Band 3 with the band's code in front of you. Do it again with nothing in front of you, and find out how much of it you actually own.

Why the smallest job is a good one to repeat

There is exactly one hard idea in it and no wiring to hide behind. If your toggle is unreliable you cannot blame the circuit, because the circuit is a button and a light.

The hard idea is that a button that is held down is down for thousands of rounds of loop, and a toggle that reacts to down instead of the moment it goes down will flip thousands of times and land wherever it likes.

Build

Build it, then measure whether it works

25 min
WiringBand 3 Part 3 for the button, hole by hole, in the middle of the serial-monitor part. Band 1 Part 5 for the light. Two parts, two pins, one resistor. Go and read those rather than trusting a summary here.
One thing the two bands do not agree on, and it will cost you an eveningBand 3 puts its button in columns 1 and 3. Band 1's light uses column 3 for its resistor. Same column, so a button leg would join straight onto the light's pin and the button would read the light instead of your finger. Nothing would look wrong.

So move the button along: columns 25 and 27, wired exactly the way Band 3 Part 3 does it at 1 and 3. Legs in f25, f27, e25, e27. Wire j25 to pin 2, and a27 to the − rail.

Count the wires on that rail before you power up: the button's, the light's, and one from the rail to a GND pin. That last one is Band 1's, it is the one people forget when they rebuild, and without it the rail is dead metal.

And use the top − rail for everything, the one nearest row j, because that is the one Band 1 uses. Band 3 used the bottom one. On most boards the two are not joined, so the button's ground wire is a long one running the length of the board. Run it rather than reaching for the near rail.

Write it from scratch. Do not open Band 3's sketch until you have something that runs.

The check is a count, not a feeling

“It seems to work” is how a bouncy button gets shipped. So count.

  1. Press it twenty times, deliberately, pausing between each. Count out loud.
  2. The light should have changed state twenty times, so it ends up back where it started.

Then make the test tell you more than that. Counting only whether it ends up right catches an odd number of extra presses and misses an even number, which is roughly half of real bounce. Add a running count with Serial.println and read the last number. It should say 20. Serial is Band 3 Part 3.

You should seeThe light off at the end, if it started off. If it ends up on, one of your twenty presses counted twice, and that is bounce. You did not feel it happen and the count is the only reason you know.
If you get twenty out of twenty first timeTake your debounce out and do it again. If it still works, your button is a good one, and you have learned something useful: a fault you cannot reproduce is not a fault you have fixed. Put the debounce back anyway, because the next button will not be this one.
The two things it needs, if you are stuck

One. A variable holding what the button read last time round, so you can tell the difference between “it is down” and “it has just gone down”.

Two. A short wait after you notice a change, so the mechanical clatter inside the switch has finished before you look again. Twenty to fifty thousandths of a second is plenty.

Band 3 Parts 8 and 9 are where both of those come from. Read them again rather than copying the sketch.

Change it

Two ways further

Bronze
Make the lamp fade on and off over half a second instead of snapping, using Band 2's analogWrite. Move the light to a ~ pin first. Then notice that the button stops responding during the fade, and say why in one sentence. That sentence is Band 5 arriving.
Silver
Add a second button that does not toggle but always turns it off, however the lamp got on. Two inputs, one state, and now you have to think about what happens when both are pressed together.
Done

Keep

  • Your count out of twenty, before and after adding the debounce
  • Your code, written without looking at the band's
  • One sentence on the difference between “the button is down” and “the button has just gone down”

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.