Band 3 · Combination Lock
Practice A crossing that waits to be asked
Band 1's tower plus Band 3's button, and the one design decision that separates a toy from something you would put on a road.
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 crossing that waits for somebody to ask
Your signal tower from Band 1 runs the same sequence for ever whether anybody is there or not. Every real crossing in the world has a button on the post.
Build one. Cars get green by default. Somebody presses. The lights change to let them cross, then go back to the cars.
A Practice page does not tell you how
A Practice page gives you the job, the check, and the one thing worth thinking about. The wiring and the code are yours. That is the whole point of it: you have been reading working code for four bands, and at some point you have to write some.
You already know every piece of this. Nothing here is new.
| You need | You already did it in |
|---|---|
| Three lights on three pins, and the idea that each step is one state | Band 1, Parts 6 and 7 |
A button wired by the diagonal rule, and read with INPUT_PULLUP so that pressed is 0 | Band 3, Parts 3 and 4 |
| Noticing the moment a button goes down, not that it is down | Band 3, Part 9 |
| A variable that remembers something between one round and the next | Band 3, Part 8 |
The one decision that makes this hard
10 minThe obvious version is: press the button, lights change. Write that down and then think about what it would do on a real road.
A crossing that changes the instant you press it kills people
A car doing fifty is thirty metres from the line when the light goes green to red with no yellow. A real crossing never obeys the button immediately. It remembers that somebody asked, and it changes at the next moment that is safe: after the green has run its minimum time, and always through the yellow.
So the press does not cause the change. The press sets a flag, and the sequence looks at the flag when it reaches a point where changing is safe. That is the idea worth carrying out of this page, and it is worth far more than the traffic light: it is how you handle any input that arrives at a moment you were not ready for.
Answer these three on paper before you write any code. There is no single right answer to any of them, and a real crossing designer argues about all three.
- Somebody presses while the cars are already stopped. What should happen? Nothing? A second crossing queued up? Something else?
- Somebody presses, then presses four more times because nothing happened yet. Does that make it come faster?
- How long is the walk phase, and how did you decide? Time yourself crossing a road of about that width.
Build it, and then prove it
30 minSo 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.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.- Leave it alone for two full minutes. The cars keep their green and nothing changes on its own.
- Press once during green. It changes within a few seconds, through the yellow, never straight to red.
- Press five times quickly. It does exactly what it did for one press.
- Press while the walk phase is already running. Whatever you decided in question 1, that is what it does, every time.
Band 1's sequence cannot be used as it stands, and finding out why is half this page
Band 1's tower is four blocks with delay(5000) and delay(1500) in them. While a delay is running, the board is frozen. It is not looking at anything. A button pressed during those five seconds is never seen at all, and it does not queue up: the moment simply passes.
So the sequence you already have cannot listen. Two things follow.
One: the cars' green is not part of the cycle. It is where the device waits. Check 1 below says nothing may change on its own, so the four Band 1 blocks are your walk sequence, run once when somebody asks. They are not the idle state.
Two: any waiting you do has to keep watching. Instead of one long delay, use a for loop of short ones and look at the button on every pass. The for loop is Band 2 Part 4.
// wait about this many tenths of a second,
// but keep looking at the button the whole time
void waitWatching(int tenths) {
for (int i = 0; i < tenths; i = i + 1) {
lookAtButton();
delay(100);
}
}
This is a workaround and it is worth knowing that. Band 5 Part 7 gives you the real answer, millis, and when you meet it you will already know exactly what problem it solves, because you will have felt it here.
If you are properly stuck, a skeleton
Not the answer. The shape, with the decisions left out.
bool somebodyAsked = false;
int lastButton = HIGH;
void lookAtButton() {
// read it, and set somebodyAsked to true
// on the MOMENT it goes down. Band 3 Part 9.
}
void loop() {
// 1. IDLE. Cars green, everything else off.
// Sit here watching the button until somebodyAsked.
// 2. Minimum green, so a press does not stop
// the cars instantly. waitWatching(...)
// 3. The walk sequence: your four Band 1 blocks,
// through the yellow, never straight to red.
// 4. somebodyAsked = false, and back to idle.
}
Notice where somebodyAsked = false sits. Put it at the top instead and every press is thrown away before it is used; put it nowhere and the crossing runs for ever. Both are worth doing on purpose once, so that you have seen them.
Two ways further
- Bronze
- Add a light for the person crossing, not the cars: on during the walk phase, flashing for the last three seconds. Now you have two audiences and they must never both be told to go.
- Silver
- Add a minimum time between crossings, so that pressing again the instant the cars get their green back does not stop them immediately. Say in your log what you chose and what a driver would think of it.
Keep
- Your three answers from Think first, written before you built
- Your code, which is yours, with a comment saying where the flag is cleared and why there
- One sentence: what would go wrong if the button changed the lights immediately
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.