Created
March 23, 2022 14:23
-
-
Save cmoz/d6e5e0dc249a2ce05bdddd8391bb0b1c to your computer and use it in GitHub Desktop.
Basic sound sensor with clap detection
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#define sensorPin 7 | |
// Variable to store the time when last event happened | |
unsigned long lastEvent = 0; | |
void setup() { | |
pinMode(sensorPin, INPUT); // Set sensor pin as an INPUT | |
Serial.begin(9600); | |
} | |
void loop() { | |
// Read Sound sensor | |
int sensorData = digitalRead(sensorPin); | |
// If pin goes LOW, sound is detected | |
if (sensorData == LOW) { | |
// If 25ms have passed since last LOW state, it means that | |
// the clap is detected and not due to any spurious sounds | |
if (millis() - lastEvent > 25) { | |
Serial.println("Clap detected!"); | |
} | |
// Remember when last event happened | |
lastEvent = millis(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment