arduino / arduino/docs-content

Bug in "Detect a Knock" code

Open
#2,159 1 comment 0 reactions 0 assignees View on GitHub
bug community maker
Dominant language
Python
Stars
375
Forks
555
Avg merge
4d 3h
Merged PRs (30d)
28

Description

The sketch for the ["Detect a Knock" tutorial](https://github.com/arduino/docs-content/blob/1a2334bad8ea2b5ebfaa8357f886014fb13cf322/content/built-in-examples/06.sensors/Knock/Knock.md) has a bug in it that limits the sensitivity of the knock sensor.

The code can be found at:
Home / Programming / Built-in Examples / Detect a Knock

The bug is on line 53 of the code snippet. The line `delay(100);` should be moved up a line so that it is inside the "if" block and immediately after the line `Serial.println("Knock!");`. In its current location, this `delay()` causes the code to wait a tenth of a second between looking for knocks. As a result many knocks vibrations (which are very brief) go undetected. As the comment on line 53 suggests, the purpose of the `delay(100)` line is to avoid overwhelming the serial port buffer, and thus the delay is only necessary when printing to the serial port.

When I relied on this code snippet to test a knock sensor in a prototype that I built years ago, it lead me to believe that this form of knock sensor was not very sensitive. I then made many modifications to my hardware to increase the sensitivity. In the end, none of those modifications were needed, since the real problem was the bug in this code snippet and not the hardware of the knock sensor itself.

Contributor guide

Open the contributing guide

Research direction

Open content/built-in-examples/06.sensors/Knock/Knock.md and inspect the Detect a Knock sketch around line 53. Move the delay(100) line into the if block immediately after the serial output, then verify the tutorial code matches the requested behavior and the delay no longer runs between knock checks.

Written by the indexing model from the issue text.

Assessment

Tech stack
arduino
Domain
documentation, embedded-iot
Issue type
Bug
Difficulty
1/5
Estimated time
Under an hour
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
55/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.