hi im using an a tap sensor and im currently programing it so that from a tap the LED blinks once.But when i put the following code,it could be seen that the LED starts to blink and stop when i tap it.i know the reason for it and i know a solution but the problem is how to implement it.my solution was to use the flag function so that when there is one tap light gets on only once.
const int ledPin = 13;
const int knockSensor = A0;
const int threshold = 100;
int sensorReading = 0;
int ledState = LOW;
void setup() {
pinMode(ledPin, OUTPUT);
Serial.begin(9600);
}
void loop() {
sensorReading = analogRead(knockSensor);
if (sensorReading >= threshold) {
ledState = !ledState;
digitalWrite(ledPin, ledState);
Serial.println("Knock!");
}
delay(100);
}
I may have answered to quickly as the code you posted and the problem you describe seems to be based on the fact that the sensor reading does not return to lower than threshold with in the 100ms delay.
You could add a millis timer and block the if (sensorReading >= threshold) { for a period of time
I would add a serial print to see what sensorReading is reading while testing as the threshold may be set to low.
This code will only detect a knock if you happen to be knocking juuust at the moment between the 100ms delays. If a knock is (say) 1/100th of a second long, then it will only detect one in every ten knocks you make.