Menu
CoddyTech

Knob Threshold

Part of the Inputs section of Coddy's Arduino journey. Lesson 12 of 22.

A threshold is a line drawn across a range: above it the sketch does one thing, below it another.

const int threshold = 600;

if (reading > threshold) {
  digitalWrite(13, HIGH);
} else {
  digitalWrite(13, LOW);
}

Keeping the line in a const gives it a name and one place to change it.

The else matters: without it, the LED turns on once the reading goes above the line and never turns off again.

challenge icon

Challenge

Easy

The knob is on A0 and the LED on pin 13. Light the LED while the reading is above 600:

  • keep 600 in a const int threshold
  • read A0, and light the LED when the reading is above the threshold, otherwise turn it off

Press Run, turn the knob past the line both ways, then Check.

Try it yourself

const int ledPin = 13;
// const int threshold = ...

void setup() {
  pinMode(ledPin, OUTPUT);
}

void loop() {
  int reading = analogRead(A0);
  // light the LED above the threshold, turn it off below
}
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Inputs

Practice on your own: Arduino playground