makernestOrigins · all chapters

Origins · Chapter 04 · Stop

The Chatterbox

Teach it when to stop.

Sketch 04 The Chatterbox

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
// 04 The Chatterbox
// The Maker's Book of Origins, chapter 04. Upload it, then turn back to the book.
// makernest.com/origins/04
// MakerNest Origins is a brand of Innointel Global Private Limited.

#include <MakerNestOrigins.h>

bool alarm = false;

void setup() {
  machine.begin(4, "04|CHATTER|BOX");
  machine.homeShows(SHOW_A | SHOW_LINE);     // the number, and START and STOP under it
  machine.useScreen(SCR_COLD_LINE);          // START
  machine.useScreen(SCR_STOP_AT);            // STOP AT
  machine.useScreen(SCR_NORMAL_COLOUR);
  machine.useScreen(SCR_WHEN_COLD);
  machine.useScreen(SCR_TEST);
}

void loop() {
  machine.update();
  if (machine.settings.coldLine == NOT_SET) { machine.ringNormal(); return; }
  int start = machine.settings.coldLine;
  int stop = (machine.settings.stopAt == NOT_SET) ? start : machine.settings.stopAt;
  float t = machine.temperatureA();

  // Two instructions, not one.
  if (!alarm && t < start) alarm = true;     // start
  if (alarm && t >= stop) alarm = false;     // stop
  // With STOP AT the same as START, every tiny wobble across the line flips it. Give it a gap.

  uint8_t w = machine.settings.whenCold;
  if (alarm && (w == WHEN_RING || w == WHEN_BOTH)) machine.ring(RED); else machine.ringNormal();
  if (w == WHEN_ALWAYS) machine.buzzer(true);
  else { machine.buzzer(false); machine.buzzerBeep(alarm && (w == WHEN_BUZZER || w == WHEN_BOTH)); }
}

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 that waits before it changes its mind

One line makes a light that flickers when the number sits on it. Two lines and a wait make a light that only changes when the change is real. Exactly the Chatterbox, on a breadboard.

You need

On the breadboard

TOPBOTTOMLDR10k+LED220A0 VA0 SA0 G12 S12 G1234567891011121314151617

Build it

  1. Build bench 02's circuit, or keep it if it is still on the breadboard.
  2. Upload bench sketch 04 and open the Serial Monitor.
Download .ino
// MakerNest Origins · bench 04 · a light that waits before it changes its mind
// Same circuit as bench 02. Two lines instead of one, and a wait.
// The LED comes on below ON_BELOW and only goes off again above OFF_ABOVE.
// Either change has to stay true for WAIT milliseconds before it happens.

const int SENSOR_PIN = A0;
const int LED_PIN = 12;
const int ON_BELOW = 300;
const int OFF_ABOVE = 400;   // the gap between the two lines stops it chattering
const unsigned long WAIT = 2000;

bool ledOn = false;
unsigned long wantedSince = 0;   // when the light first asked for a change

void setup() {
  pinMode(LED_PIN, OUTPUT);
  Serial.begin(9600);
}

void loop() {
  int light = analogRead(SENSOR_PIN);
  Serial.println(light);

  bool wantChange = (!ledOn && light < ON_BELOW) || (ledOn && light > OFF_ABOVE);
  if (!wantChange) {
    wantedSince = 0;                       // the light changed its mind: start again
  } else if (wantedSince == 0) {
    wantedSince = millis();                // the light has asked: start the clock
  } else if (millis() - wantedSince >= WAIT) {
    ledOn = !ledOn;                        // it asked for long enough: change
    digitalWrite(LED_PIN, ledOn ? HIGH : LOW);
    wantedSince = 0;
  }
  delay(100);
}
What you'll see

Cover the sensor quickly and uncover it. Nothing happens. Keep it covered for two seconds and the LED comes on. It needs more light than before to go off again.

Break it on purpose

Make OFF_ABOVE the same as ON_BELOW and WAIT 0. Hold your hand where the number sits on the line. It chatters.

Then try

Find the smallest gap and the shortest wait that still stop it chattering in your room.

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 NoisemakerSensor Duel →