Little Bird Curriculum / code littlebird.com.au
vce_u1_05_threshold.ino Arduino (C/C++) 29 lines · 3 views ✦ Open in English IDE Raw Download Embed view

Switch an output when a sensor reading crosses a single threshold. Deliberately shows the chattering fault.

From the unit VCE Systems Engineering Unit 1 — Electrotechnological systems design · activity PRP 5: Threshold control — making a decision
// 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);
}

Embed this snippet

As an iframe:

Or drop it inline with a script tag (injects the highlighted block where included):

Shared from Little Bird Electronics curriculum.

Copied to clipboard