// PRP 6 — hysteresis. // // One threshold chatters because the sensor's own noise crosses it // repeatedly. Two thresholds with a gap wider than that noise fixes it. // // Make the gap wider than the noise you measured in PRP 4, and no wider. const int SENSOR_PIN = A0; const int OUTPUT_PIN = 13; const int TURN_ON_BELOW = 480; // <-- your numbers const int TURN_OFF_ABOVE = 520; bool outputOn = false; void setup() { pinMode(OUTPUT_PIN, OUTPUT); Serial.begin(9600); } void loop() { int reading = analogRead(SENSOR_PIN); if (!outputOn && reading < TURN_ON_BELOW) { outputOn = true; } else if (outputOn && reading > TURN_OFF_ABOVE) { outputOn = false; } // Between the two thresholds nothing changes. That is the whole trick. digitalWrite(OUTPUT_PIN, outputOn ? HIGH : LOW); Serial.println(reading); delay(100); }