/*
Knock Sensor
This sketch reads a piezo element to detect a knocking sound.
It reads an analog pin and compares the result to a set threshold.
If the result is greater than the threshold, it writes "knock" to the serial
port, and toggles the LED on pin 13.
The circuit:
- positive connection of the piezo attached to analog in 0
- negative connection of the piezo attached to ground
- 1 megohm resistor attached from analog in 0 to ground
created 25 Mar 2007
by David Cuartielles <http://www.0j0.org>
modified 30 Aug 2011
by Tom Igoe
This example code is in the public domain.
https://www.arduino.cc/en/Tutorial/BuiltInExamples/Knock
*/// these constants won't change:
constintledPin=13;// LED connected to digital pin 13
constintknockSensor=A0;// the piezo is connected to analog pin 0
constintthreshold=100;// threshold value to decide when the detected sound is a knock or not
// these variables will change:
intsensorReading=0;// variable to store the value read from the sensor pin
intledState=LOW;// variable used to store the last LED status, to toggle the light
voidloop(){// read the sensor and store it in the variable sensorReading:
sensorReading=analogRead(knockSensor);// if the sensor reading is greater than the threshold:
if(sensorReading>=threshold){// toggle the status of the ledPin:
ledState=!ledState;// update the LED pin itself:
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
}