#define soundSensor A0
#define relayPin 13
int tapThreshold = 100; // Adjust the threshold based on your testing
unsigned long lastTapTime = 0;
int tapCounter = 0;
void setup() {
Serial.begin(115200);
pinMode(relayPin, OUTPUT);
}
void loop() {
int soundLevel = analogRead(soundSensor);
Serial.println(soundLevel);
if (abs(soundLevel - 512) > tapThreshold && millis() - lastTapTime > 500) {
lastTapTime = millis();
// Increment the tap counter
tapCounter++;
// Toggle the relay based on the tap counter
if (tapCounter == 2) {
digitalWrite(relayPin, HIGH); // Turn the light on
} else if (tapCounter == 4) {
digitalWrite(relayPin, LOW); // Turn the light off
tapCounter = 0; // Reset the counter after turning off the light
}
delay(500); // Add a delay to avoid rapid toggling due to continuous sound
}
}