When I run the "blink" program, the LED turns on and off like it should. However, when I run the "Knock" program, it stays lit but flickers when the piezo is knocked. Why is it staying lit? Thanks!
const int led = 1; // led connected to digital pin 1
const int piezo = A0; // the piezo is connected to analog pin 0
const int threshold = 10; // threshold value to decide when the detected sound is a knock or not
// these variables will change:
int sensorReading = 0; // variable to store the value read from the sensor pin
void setup() {
pinMode(led, OUTPUT); // declare the ledPin as as OUTPUT
Serial.begin(9600); // use the serial port
}
void loop() {
// read the sensor and store it in the variable sensorReading:
sensorReading = analogRead(piezo);
// if the sensor reading is greater than the threshold:
if (sensorReading >= threshold) {
// update the LED pin itself:
digitalWrite(led, HIGH);
// send the string "Knock!" back to the computer, followed by newline
Serial.println("Knock!");
delay(100); // delay to avoid overloading the serial port buffer
}else{
digitalWrite(led, LOW);
}
}