Threshold control — making a decision
Students will: Make a system act on its own reading, and justify the threshold.
// PRP 5 — a single threshold decision.
//
// THRESHOLD is a design decision. Replace this value with one you
// chose from your own recorded readings, and be able to defend it.
//
// Expect this sketch to chatter when the reading sits near the
// threshold. That fault is the point of the activity — PRP 6 fixes it.
const int SENSOR_PIN = A0;
const int OUTPUT_PIN = 13;
const int THRESHOLD = 500; // <-- your number, from your data
void setup() {
pinMode(OUTPUT_PIN, OUTPUT);
Serial.begin(9600);
}
void loop() {
int reading = analogRead(SENSOR_PIN);
Serial.println(reading);
if (reading < THRESHOLD) {
digitalWrite(OUTPUT_PIN, HIGH);
} else {
digitalWrite(OUTPUT_PIN, LOW);
}
delay(100);
}
Build it up — 13 steps
Common errors & fixes
That the threshold is arbitrary. It is the central design decision in the whole system.