From 7f75d462c3eeb05bbcb58eca18a8a1ce26f21ea2 Mon Sep 17 00:00:00 2001 From: per1234 Date: Wed, 11 Sep 2024 04:33:57 -0700 Subject: [PATCH] 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. --- examples/06.Sensors/Knock/Knock.ino | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/06.Sensors/Knock/Knock.ino b/examples/06.Sensors/Knock/Knock.ino index fa174a1..46047d9 100644 --- a/examples/06.Sensors/Knock/Knock.ino +++ b/examples/06.Sensors/Knock/Knock.ino @@ -49,6 +49,6 @@ void loop() { digitalWrite(ledPin, ledState); // send the string "Knock!" back to the computer, followed by newline Serial.println("Knock!"); + delay(100); // delay to avoid overloading the serial port buffer } - delay(100); // delay to avoid overloading the serial port buffer }