Arduino Ladder · Bozoma Innovation Hub

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.

Time
40 min
You need
3 LEDs, 3 resistors, 1 button, 9 jumper wires (plus a fourth LED and resistor if you do the Bronze)
At once
Everybody at once
Before this
Finish Band 1 and Band 3 up to Part 9. No sketch is given. The wiring is in the two bands you have already done.

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

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 needYou already did it in
Three lights on three pins, and the idea that each step is one stateBand 1, Parts 6 and 7
A button wired by the diagonal rule, and read with INPUT_PULLUP so that pressed is 0Band 3, Parts 3 and 4
Noticing the moment a button goes down, not that it is downBand 3, Part 9
A variable that remembers something between one round and the nextBand 3, Part 8
Think first

The one decision that makes this hard

10 min

The 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.

  1. Somebody presses while the cars are already stopped. What should happen? Nothing? A second crossing queued up? Something else?
  2. Somebody presses, then presses four more times because nothing happened yet. Does that make it come faster?
  3. How long is the walk phase, and how did you decide? Time yourself crossing a road of about that width.
Build

Build it, and then prove it

30 min
WiringBand 1 Part 6 has the three lights, hole by hole. Band 3 Part 3 has the button, in the middle of the serial-monitor part. Keep every light's own resistor.
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.

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.
The check, and do all four
  1. Leave it alone for two full minutes. The cars keep their green and nothing changes on its own.
  2. Press once during green. It changes within a few seconds, through the yellow, never straight to red.
  3. Press five times quickly. It does exactly what it did for one press.
  4. 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.

Change it

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.
Done

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.