makernestOrigins · all chapters

Origins · Chapter 08 · Sequence

Chain Reaction

Things happen in an order you set.

Sketch 08 Chain Reaction

Copy it, open File, New Sketch, select everything in the new window and paste over it. Check Tools, Board says Arduino Uno and Tools, Port shows your board, then press Upload.

Download .ino
// 08 Chain Reaction
// The Maker's Book of Origins, chapter 08. Upload it, then turn back to the book.
// makernest.com/origins/08
// MakerNest Origins is a brand of Innointel Global Private Limited.

#include <MakerNestOrigins.h>

// ---- the cold rule, with its gap (chapter 04)
bool coldNow() {
  static bool alarm = false;
  if (machine.settings.coldLine == NOT_SET) return false;      // no line chosen yet: no opinion
  int start = machine.settings.coldLine;
  int stop = (machine.settings.stopAt == NOT_SET) ? start : machine.settings.stopAt;
  float t = machine.watchedTemperature();                      // sensor A, or B if you chose B (chapter 05)
  if (!alarm && t < start) alarm = true;                       // below START: it starts
  if (alarm && t >= stop) alarm = false;                       // back up to STOP AT: it stops
  return alarm;
}

// ---- two places, one gap (chapter 05)
bool apartNow() {
  if (machine.settings.apartBy == 0) return false;             // no gap chosen yet
  float gap = machine.settings.apartBy / 2.0;                  // stored in half degrees
  return fabs(machine.temperatureA() - machine.temperatureB()) > gap;
}

// ---- what it does about it (chapters 02 and 03)
void showIt(bool cold, bool apart) {
  uint8_t w = machine.settings.whenCold;
  if (apart) {                                                 // apart: orange, and the buzzer
    machine.ring(ORANGE);
    machine.buzzerBeep(true);
    return;
  }
  if (cold && (w == WHEN_RING || w == WHEN_BOTH)) machine.ring(RED); else machine.ringNormal();
  if (w == WHEN_ALWAYS) machine.buzzer(true);                  // always on: it has stopped asking
  else { machine.buzzer(false); machine.buzzerBeep(cold && (w == WHEN_BUZZER || w == WHEN_BOTH)); }
}

bool wasCold = false;

void setup() {
  machine.begin(8, "08|CHAIN|REACTION");
  machine.homeShows(SHOW_A | SHOW_B | SHOW_COUNT | SHOW_NOTEBOOK);
  machine.useScreen(SCR_SEQUENCE);
  machine.useScreen(SCR_RUN_NOW);
  machine.useScreen(SCR_START_WITH);
  machine.useScreen(SCR_WRITE_EVERY);
  machine.useScreen(SCR_LOG);
  machine.useScreen(SCR_SETTLE);
  machine.useScreen(SCR_KEEP_COUNT);
  machine.useScreen(SCR_APART_BY);
  machine.useScreen(SCR_WATCHES);
  machine.useScreen(SCR_COLD_LINE);
  machine.useScreen(SCR_STOP_AT);
  machine.useScreen(SCR_NORMAL_COLOUR);
  machine.useScreen(SCR_WHEN_COLD);
  machine.useScreen(SCR_TEST);
}

void loop() {
  machine.update();
  bool cold = coldNow();
  bool apart = apartNow();
  showIt(cold, apart);
  // START WITH your cold decision: the sequence runs by itself the moment it turns cold.
  if (machine.settings.startWith == 1 && cold && !wasCold) machine.runSequence();
  wasCold = cold;
  // The sequence never checks whether a step worked. It only knows what comes next.
}

Your settings and your notebook stay saved on the board when you upload.

Wiring

Red is power, black is the way back, and every signal has its own colour. On the shield, black goes to G, red to V, the signal to S. Every time.

New this chapter

SWITCH BOARDUNDER THE BASEGNDVCCSIGROW 9GVSTHE SHIELD2345678910111213A4A5USEDTODAY

The whole shield, by the end of the book

GVSROW 2SENSOR AROW 3DIALROW 4DIALROW 5DIALROW 6BUZZERROW 7SENSOR BROW 8DOOR SWITCHROW 9SWITCH BOARDROW 11LIGHT RINGA4DISPLAYA5DISPLAYPOWER 3V3DISPLAY

On the bench: Traffic lights: three LEDs and your own timings

A sequence is a list of things in an order, each with how long it lasts. Change the numbers, change the crossing.

You need

On the breadboard

TOPBOTTOM+RED220+YEL220+GRN22012 S12 G13 S13 G10 S10 G1234567891011121314151617

Build it

  1. Red LED: long leg in column 2, short leg in column 3. A 220 Ω from column 3 to column 5.
  2. Yellow LED: long leg in column 7, short leg in column 8. A 220 Ω from column 8 to column 10.
  3. Green LED: long leg in column 12, short leg in column 13. A 220 Ω from column 13 to column 15.
  4. All in the top half. Jumpers: row 12 S to column 2, row 12 G to column 5.
  5. Row 13 S to column 7, row 13 G to column 10.
  6. Row 10 S to column 12, row 10 G to column 15.
  7. Upload bench sketch 08.
Download .ino
// MakerNest Origins · bench 08 · traffic lights with your own timings
// Red on row 12, yellow on row 13, green on row 10. Change the four numbers to make
// your own crossing. All times are in milliseconds: 1000 is one second.

const int RED_PIN = 12;
const int YELLOW_PIN = 13;
const int GREEN_PIN = 10;

const unsigned long RED_TIME = 5000;
const unsigned long RED_AND_YELLOW_TIME = 1500;
const unsigned long GREEN_TIME = 5000;
const unsigned long YELLOW_TIME = 2000;

void lights(bool red, bool yellow, bool green) {
  digitalWrite(RED_PIN, red ? HIGH : LOW);
  digitalWrite(YELLOW_PIN, yellow ? HIGH : LOW);
  digitalWrite(GREEN_PIN, green ? HIGH : LOW);
}

void setup() {
  pinMode(RED_PIN, OUTPUT);
  pinMode(YELLOW_PIN, OUTPUT);
  pinMode(GREEN_PIN, OUTPUT);
}

void loop() {
  lights(true, false, false);   // stop
  delay(RED_TIME);
  lights(true, true, false);    // get ready
  delay(RED_AND_YELLOW_TIME);
  lights(false, false, true);   // go
  delay(GREEN_TIME);
  lights(false, true, false);   // stop if you can
  delay(YELLOW_TIME);
}
What you'll see

Red, red and yellow, green, yellow, and round again.

Break it on purpose

Make YELLOW_TIME 0. Green jumps straight to red. Ask somebody at home if that would be safe at a real crossing.

Then try

Time a real crossing near you and copy its numbers.

How the bench works

Bench sketches replace your chapter sketch while you use them. Your settings and your notebook stay saved on the board. When you finish, upload the chapter's sketch again and your machine is back exactly as it was.

While a bench sketch runs, the display and the ring freeze on whatever they last showed. That is normal.

If the light ring is still lit from your chapter sketch, switch everything off and on again before you start. It goes dark and stops using power the bench needs.

The breadboard has 17 numbered columns and two halves, top and bottom. The five holes in one column of one half are joined underneath. Nothing joins across the gap in the middle.

Every jumper from the shield has a female end: push it onto a shield pin. The male end goes into the breadboard. G, V and S are the same three pins you have used all book: ground, power, signal.

Never put a wire straight from V to G. That is a short circuit.

Ask Milo

Stuck, curious, or something looks wrong? Tell me what you see.

You can paste your code or an error message here too.

← The Night ShiftThe Doorkeeper →