Sketch 05 Sensor Duel
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.
// 05 Sensor Duel
// The Maker's Book of Origins, chapter 05. Upload it, then turn back to the book.
// makernest.com/origins/05
// 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(5, "05|SENSOR|DUEL");
machine.homeShows(SHOW_A | SHOW_B | SHOW_LINE);
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);
}
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
The whole shield, by the end of the book
On the bench: Two light sensors and one LED that picks the brighter side
Two sensors never read exactly the same. Comparing them means deciding how close counts as equal.
You need
- light sensor (LDR): either way round × 2
- 10 kΩ resistor (brown, black, orange) × 2
- red LED (long leg is +)
- 220 Ω resistor (red, red, brown)
- male-to-female jumper × 8
On the breadboard
Build it
- Build bench 02's circuit in the top half. That is sensor A on A0, and the LED on row 12.
- Sensor B in the bottom half: one leg in column 3, the other in column 6.
- A 10 kΩ from column 6 to column 10, bottom half.
- Jumpers: A1 V to column 3 bottom, A1 S to column 6 bottom, A1 G to column 10 bottom.
- Upload bench sketch 05 and open the Serial Monitor.
// MakerNest Origins · bench 05 · two light sensors and one LED that picks the brighter side
// Sensor A on A0, sensor B on A1. The LED on row 12 is on when A is brighter,
// off when B is brighter, and stays as it is when they are closer than GAP.
const int A_PIN = A0;
const int B_PIN = A1;
const int LED_PIN = 12;
const int GAP = 40; // two sensors never agree exactly; this is how close counts as "the same"
void setup() {
pinMode(LED_PIN, OUTPUT);
Serial.begin(9600);
}
void loop() {
int a = analogRead(A_PIN);
int b = analogRead(B_PIN);
Serial.print("A ");
Serial.print(a);
Serial.print(" B ");
Serial.println(b);
if (a > b + GAP) {
digitalWrite(LED_PIN, HIGH); // A is brighter
} else if (b > a + GAP) {
digitalWrite(LED_PIN, LOW); // B is brighter
}
// closer than GAP: leave the LED as it is
delay(300);
}
Shine a torch at A, then at B. The LED follows the brighter side. With both in the same light, look at the two numbers: they still disagree a little.
Set GAP to 0. With both sensors in the same light, the LED flickers between them.
Point one sensor at the window and one at the room. Which side wins at different times of day?
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.