Threshold control — making a decision
Now the system decides for itself.
- Choose a threshold from your own recorded readings, not from an example.
- Write the decision: if the reading is above the threshold do one thing, otherwise do another.
- Test it by changing conditions until it switches.
- Find the fault. Hold the sensor right at the threshold. The output will flicker on and off rapidly. This is chattering, and every real control system has to deal with it.
- Record it happening before you fix it.
- Justify your threshold in writing, referring to the readings you took.
What you'll learn
Make a system act on its own reading, and justify the threshold.
What you'll make
My threshold comes from my recorded data and I can defend the number.
What you need
Student kit, a sensor from PRP 3 or 4, LED.
Build it up — 13 steps
Sample code
// 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);
}
IPO chart
Inputs
- A sensor reading from PRP 3 or PRP 4
Processing
- An if/else comparison against a threshold the student chooses
Outputs
- An LED that changes state when the threshold is crossed
Stuck? Common things to check Tap to open
That the threshold is arbitrary. It is the central design decision in the whole system.
Take it slower
Give the working sensor code and have the student write only the if/else decision.
Push further
Add a second condition so the output acts only when both are met, and explain why that suits your project better than either alone.
Done when…
A working threshold decision, evidence of chattering, and a written justification.
Reflect
What conditions would make your threshold the wrong choice?