Skip to content

Instantly share code, notes, and snippets.

@dwaq

dwaq/clapper.ino

Created Aug 7, 2019
Embed
What would you like to do?
Arduino code for the Clap-Activated Applause Machine https://hackaday.io/project/166959-clap-activated-applause-machine
//***************************************************************************************
// Clap-Activated Applause Machine
//
// By: Dillon Nichols
// @Dillon1337
// https://hackaday.io/project/166959-clap-activated-applause-machine
//
// This work is licensed under a Creative Commons Attribution 3.0 Unported License.
//***************************************************************************************
// pin definitions
const int motor = 9;
const int button = 7;
const int mic = 3;
const int led = A3;
// delay after an interrupt is triggered
const int debounceDelay = 50;
// counter for time between claps
int ms_ellapsed = 0;
// disable the microphone interrupts while clapping
bool interruptEnabled = false;
// when the first clap is detected
bool firstDetected = false;
// when the second clap is detected
bool startClapping = false;
// a reasonable time between claps (as compared to a human)
#define CLAP_DELAY 286
// SUPER SECRET ALGORITHIM
// (just clap a bunch of times)
void clapAlgorithim(void) {
clapWithDelay(CLAP_DELAY);
clapWithDelay(CLAP_DELAY);
clapWithDelay(CLAP_DELAY);
clapWithDelay(CLAP_DELAY);
clapWithDelay(CLAP_DELAY);
clapWithDelay(CLAP_DELAY);
clapWithDelay(CLAP_DELAY);
// extra code to fake turning itself off
/*delay(1000);
clapWithDelay(150);
clapWithDelay(150);
*/
}
// interrupt triggers this function
void soundDetected(void) {
// only run if interrupt is enabled
if(interruptEnabled) {
// on first clap detection
if(firstDetected == false) {
firstDetected = true;
// start the counter at the debounce delay value
// because we waited that long earlier without counting
ms_ellapsed = debounceDelay;
//Serial.println("1st sound detected");
}
// second clap detected
else {
//Serial.print("2nd sound detected at ");
//Serial.println(ms_ellapsed);
// 2 claps with 0.2 to 1 second in between triggers the machine
if(ms_ellapsed > 200 && ms_ellapsed < 1000) {
startClapping = true;
}
// reset variables
ms_ellapsed = 0;
firstDetected = false;
}
// disable the interrupt for a bit (debouncing)
interruptEnabled = false;
}
}
void setup() {
// make the mic an input and trigger a function if the pin rises
pinMode(mic, INPUT);
attachInterrupt(digitalPinToInterrupt(mic), soundDetected, RISING);
interruptEnabled = true;
// enable all Serial lines for debugging
//Serial.begin(115200);
// configure other pins
pinMode(led, OUTPUT);
pinMode(motor, OUTPUT);
pinMode(button, INPUT);
}
// clap by spinning the motor one revolution and then waiting
void clapWithDelay(int waitTime) {
// spin the motor until button is pressed
while(digitalRead(button) == LOW)
{
digitalWrite(motor, HIGH);
}
// then turn off motor
digitalWrite(motor, LOW);
// wait some time
delay(waitTime);
}
void loop() {
// loop once per millisecond
delay(1);
// count up the timer between claps
if(firstDetected){
ms_ellapsed++;
}
// after 2.5 seconds between claps, reset the timer (timeout)
if (ms_ellapsed > 2500) {
ms_ellapsed = 0;
firstDetected = false;
//Serial.println("Timeout.. resetting..");
}
// mic input debouncing
// wait some time after disabling the interrupt to enable it again
if (interruptEnabled == false) {
//Serial.println("Interrupt disabled.. waiting..");
delay(debounceDelay);
interruptEnabled = true;
}
// when it is time to clap..
if(startClapping) {
// turn on the applause light
digitalWrite(led, HIGH);
// start clapping
clapAlgorithim();
// turn off the light
digitalWrite(led, LOW);
startClapping = false;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.