I'm trying to have a servo move when my Piezo sensor goes off due to a knock (a door lock).
I have a 27mm Piezoelectric Disk Element and a LED with an Arduino Uno.
The Piezo is connected to A0 and GND with a 1 megohm resistor connecting the two. The LED is attached to 13 and GND.
I have Serial monitor running as well as the LED to tell me when there's been a knock - however, after I 'knock' once, it continues sensing a knock for a long period of time - ranging from a 4 or so seconds to much longer, if I let it.
Why is this happening? What's wrong with this - my code, my wiring, my sensor? I've tried looking this problem up, but I wasn't able to find anything.
(It may be very simple as I'm very new to this, so I apologize!)
Code for reference:
int ledPin = 13;
int knockSensor = 0;
byte val = 0;
int statePin = LOW;
int THRESHOLD = 100;
void setup() {
pinMode(ledPin, OUTPUT);
Serial.begin(9600);
}
void loop() {
val = analogRead(knockSensor);
if (val >= THRESHOLD) {
statePin = !statePin;
digitalWrite(ledPin, statePin);
Serial.println("Knock!");
}
delay(250); // we have to make a delay to avoid overloading the serial port
}