Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Diff: main.cpp
- Revision:
- 0:6c254e376969
- Child:
- 1:df9d6f3886f6
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/main.cpp Mon Jun 29 13:22:34 2015 +0000 @@ -0,0 +1,78 @@ +//**************************************************// +// Application to switch a light or other output // +// whenever the correct handclapping pattern is // +// detected. // +// // +// Ferry Musters & Thom Grasso - 26/06/2015 // +// Electrical Engineering - Hogeschool Utrecht // +//**************************************************// +// TODO: +// Must have: - Reset timer and measurements after (4sec?) timeout +// Must have: - Detect set pattern +// Could have: - Make user defined patterns (learn the device the pattern) + +#include "mbed.h" + +//-------------------[ Variables ]------------------- +DigitalOut led_red(LED_RED); +DigitalOut led_blue(LED_BLUE); +DigitalOut led_green(LED_GREEN); + +DigitalOut boardGnd(D4); +DigitalOut boardVcc(D5); +//InterruptIn boardClap(SW2); +InterruptIn boardClap(D6); +DigitalOut boardReset(D7); + +Serial output(USBTX, USBRX); +Timer timer; //overall timer +int clapTime[100]; //buffer to save clap times +int currentClap = 0; //counter to keep track of current clap in buffer +int printedClap = 0; //counter to keep track of printf'ed claps + + +//-------------------[ Functions ]------------------- +void clap_detected(void) +{ + clapTime[currentClap++] = timer.read_ms(); //save clap time and increment counter + + led_red = 0; //blink a led to show detected clap + boardReset = 1; //reset the clap detecter board with a reset pulse + wait(0.1); //wait to show led and also avoid handclap 'bounces' + boardReset = 0; + led_red = 1; //The LED pin is inverted so '1' means LED off +} + + + +//-------------------[ Main ]------------------- +int main() +{ + timer.start(); + boardVcc = 1; boardGnd = 0; boardReset = 0; + led_red = 1; led_green = 1; led_blue = 1; + boardClap.rise(&clap_detected); + + while (true) { + if(printedClap < currentClap) { //if the last clap isn't printed yet + output.printf("Clap %d: %d\r\n", ++printedClap, clapTime[printedClap]); //debug the clap + } + if((currentClap > 1) && (timer.read_ms() > (clapTime[currentClap-1] + 5000))) { //reset when timeout (last clap is more then 5 seconds ago) + output.printf("Timeout. %d claps were detected over a time of %dms\r\n", (currentClap - 1), (clapTime[currentClap] - clapTime[0])); + currentClap = 0; + printedClap = 0; + + led_blue = 0; //blink a led + wait(0.3); + led_blue = 1; //The LED pin is inverted so '1' means LED off + + } + + /*led_blue = 1; + led_green = 0; + wait(0.2); + led_blue = 0; + led_green = 1; + wait(0.2);*/ + } +}