Created
August 7, 2019 22:58
-
-
Save dwaq/35bf46b9d76081fec116b856d3b7ecda to your computer and use it in GitHub Desktop.
Arduino code for the Clap-Activated Applause Machine https://hackaday.io/project/166959-clap-activated-applause-machine
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
//*************************************************************************************** | |
// 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