Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*****************************************
- * Arduino Clapper *
- * by Learnelectronics *
- * inspired by Sketch by andprof *
- * 4/20/23 Use this however *
- * you wish. Open Source. *
- *****************************************/
- int Sensor = A0; //microphone sensor attached to A0
- int clap = 0; //claps detected starts at 0
- long detection_range_start = 0; //detection ranbge starts at 0
- long detection_range = 0; //see above
- boolean status_lights = false; //clapper inital state is off
- void setup() { //setup loop runs once time
- pinMode(Sensor, INPUT); //sensor pin (A0) is set as input
- pinMode(2,OUTPUT); //LED pin set as output
- Serial.begin(115200); //serial comms for debugging
- }
- void loop() { //main loops runs forever
- int status_sensor = digitalRead(Sensor); //read the sensor and store value in status_sensor
- if (status_sensor == 0) //if it is 0
- {
- if (clap == 0) //and a clap is detected
- {
- detection_range_start = detection_range = millis(); //start range is detection range is millis all the same
- clap++; //increment clap count
- }
- else if (clap > 0 && millis()-detection_range >= 50) //if time has pssed before another detected clap
- {
- detection_range = millis(); //reset detection range
- clap++; //increment clap count
- Serial.println(clap); //for debugging
- }
- }
- if (millis()-detection_range_start >= 400) //if there are 2 claps in under 400 milliseconds do the thing
- {
- if (clap == 2)
- {
- if (!status_lights)
- {
- status_lights = true;
- digitalWrite(2, HIGH); //do the thing
- }
- else if (status_lights)
- {
- status_lights = false;
- digitalWrite(2, LOW); //undo the thing
- }
- }
- clap = 0; //reset clap count
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement