Skip to content

Commit 7f75d46

Browse files
authored
Only delay after serial output in "Knock"
The "Knock" sketch polls the voltage output from a piezo disc to detect the vibrations associated with a knock. The duration of vibrations from a single knock is likely to significantly exceed the unchecked polling interval. This would result in a single knock producing multiple detections, and thus multiple prints to Serial. In order to avoid this, a "debouncing" delay was added to the sketch. Previously the delay was positioned in the outer scope of the `loop` function, which caused it to always affect the polling interval. This caused the sketch to miss the detection of knocks that produced vibrations that only occurred during that unnecessary delay. The problem is fixed by moving the delay inside the knock detection conditional block, so that debouncing is only done when actually needed.
1 parent f1abc30 commit 7f75d46

File tree

1 file changed

+1
-1
lines changed

1 file changed

+1
-1
lines changed

examples/06.Sensors/Knock/Knock.ino

+1-1
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,6 @@ void loop() {
4949
digitalWrite(ledPin, ledState);
5050
// send the string "Knock!" back to the computer, followed by newline
5151
Serial.println("Knock!");
52+
delay(100); // delay to avoid overloading the serial port buffer
5253
}
53-
delay(100); // delay to avoid overloading the serial port buffer
5454
}

0 commit comments

Comments
 (0)