Is there any way to have the Arduino recognize when it is within, say plus or minus 10 of a number? For example, if a rotary encoder is within X either way of a certain number, it lights an LED?
This should light up the pin 13 LED if encoderVal and otherVal happen to be 10 or less away from each other:
void setup() {
pinMode(13, OUTPUT);
int encoderVal = random(-20, 20);
int otherVal = random(-20, 20);
if(abs(encoderVal - otherVal) <= 10) {
digitalWrite(13, HIGH);
} else {
digitalWrite(13, LOW);
}
}
void loop() {}
Untested.
Also untested but shorter:
digitalWrite(13, abs(encoderVal - otherVal) <= 10);
Brilliant, thanks everyone!