makernestOrigins · all chapters

Origins · Chapter 07 · Remember

The Night Shift

Find out what your room does while you sleep.

Sketch 07 The Night Shift

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
// 07 The Night Shift
// The Maker's Book of Origins, chapter 07. Upload it, then turn back to the book.
// makernest.com/origins/07
// 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)); }
}

void setup() {
  machine.begin(7, "07|NIGHT|SHIFT");
  machine.homeShows(SHOW_A | SHOW_B | SHOW_COUNT | SHOW_NOTEBOOK);
  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);
  // The notebook is the board's own memory. It writes sensor A down every WRITE EVERY, and keeps it through
  // uploads and restarts. The upload gives it the day and time. Keep it on a phone charger all week.
}

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

Wiring

Nothing new to wire in this chapter. Here is the whole shield, so you can check every wire.

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: A light sensor that writes down every time the room goes dark

Writing something down is what makes a machine useful when nobody is watching. Where it writes it decides whether it survives.

You need

On the breadboard

TOPBOTTOMLDR10kA0 VA0 SA0 G1234567891011121314151617

Build it

  1. Build the sensor half of bench 02: the light sensor in columns 3 and 6, the 10 kΩ from 6 to 10, and the three A0 jumpers.
  2. Upload bench sketch 07 and open the Serial Monitor.
  3. Cover and uncover the sensor a few times. Then type L and press Enter.
Download .ino
// MakerNest Origins · bench 07 · a light sensor that writes down every time the room goes dark
// Same circuit as bench 02, without needing the LED. Every time the light drops below DARK,
// the time since the upload is written down. Type L in the Serial Monitor and press Enter
// to read the list. Unlike your machine's notebook, this list lives in the board's working
// memory, so it is gone the moment the board loses power. Try it.

const int SENSOR_PIN = A0;
const int DARK = 300;
const int LIGHT_AGAIN = 400;     // it must get this bright again before the next dark counts
const int MAX_TIMES = 40;

unsigned long times[MAX_TIMES];
int written = 0;
bool isDark = false;

void printTime(unsigned long ms) {
  unsigned long s = ms / 1000;
  Serial.print(s / 3600);
  Serial.print(":");
  if ((s / 60) % 60 < 10) Serial.print("0");
  Serial.print((s / 60) % 60);
  Serial.print(":");
  if (s % 60 < 10) Serial.print("0");
  Serial.print(s % 60);
}

void setup() {
  Serial.begin(9600);
  Serial.println("Writing down every dark. Type L for the list.");
}

void loop() {
  int light = analogRead(SENSOR_PIN);

  if (!isDark && light < DARK) {
    isDark = true;
    if (written < MAX_TIMES) {
      times[written] = millis();
      written = written + 1;
    }
    Serial.print("Dark at ");
    printTime(millis());
    Serial.println();
  } else if (isDark && light > LIGHT_AGAIN) {
    isDark = false;
  }

  if (Serial.available() > 0) {
    char c = Serial.read();
    if (c == 'L' || c == 'l') {
      Serial.print(written);
      Serial.println(" darks so far:");
      for (int i = 0; i < written; i++) {
        printTime(times[i]);
        Serial.println();
      }
    }
  }
  delay(200);
}
What you'll see

Every dark is written down with the time since the upload.

Break it on purpose

Unplug the USB lead, plug it back in, type L. The list is empty. This one writes in working memory, which forgets when the power goes. Your machine's notebook writes in the board's saved memory, which is why your week survived.

Then try

Leave it by a window overnight on a phone charger and type L in the morning. When did it get dark?

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 WitnessChain Reaction →