Ferry Musters / Mbed 2 deprecated Clap_Unit

Dependencies:   mbed

Committer:
Ferryy
Date:
Mon Jun 29 13:22:34 2015 +0000
Revision:
0:6c254e376969
Child:
1:df9d6f3886f6
First Commit

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Ferryy 0:6c254e376969 1 //**************************************************//
Ferryy 0:6c254e376969 2 // Application to switch a light or other output //
Ferryy 0:6c254e376969 3 // whenever the correct handclapping pattern is //
Ferryy 0:6c254e376969 4 // detected. //
Ferryy 0:6c254e376969 5 // //
Ferryy 0:6c254e376969 6 // Ferry Musters & Thom Grasso - 26/06/2015 //
Ferryy 0:6c254e376969 7 // Electrical Engineering - Hogeschool Utrecht //
Ferryy 0:6c254e376969 8 //**************************************************//
Ferryy 0:6c254e376969 9 // TODO:
Ferryy 0:6c254e376969 10 // Must have: - Reset timer and measurements after (4sec?) timeout
Ferryy 0:6c254e376969 11 // Must have: - Detect set pattern
Ferryy 0:6c254e376969 12 // Could have: - Make user defined patterns (learn the device the pattern)
Ferryy 0:6c254e376969 13
Ferryy 0:6c254e376969 14 #include "mbed.h"
Ferryy 0:6c254e376969 15
Ferryy 0:6c254e376969 16 //-------------------[ Variables ]-------------------
Ferryy 0:6c254e376969 17 DigitalOut led_red(LED_RED);
Ferryy 0:6c254e376969 18 DigitalOut led_blue(LED_BLUE);
Ferryy 0:6c254e376969 19 DigitalOut led_green(LED_GREEN);
Ferryy 0:6c254e376969 20
Ferryy 0:6c254e376969 21 DigitalOut boardGnd(D4);
Ferryy 0:6c254e376969 22 DigitalOut boardVcc(D5);
Ferryy 0:6c254e376969 23 //InterruptIn boardClap(SW2);
Ferryy 0:6c254e376969 24 InterruptIn boardClap(D6);
Ferryy 0:6c254e376969 25 DigitalOut boardReset(D7);
Ferryy 0:6c254e376969 26
Ferryy 0:6c254e376969 27 Serial output(USBTX, USBRX);
Ferryy 0:6c254e376969 28 Timer timer; //overall timer
Ferryy 0:6c254e376969 29 int clapTime[100]; //buffer to save clap times
Ferryy 0:6c254e376969 30 int currentClap = 0; //counter to keep track of current clap in buffer
Ferryy 0:6c254e376969 31 int printedClap = 0; //counter to keep track of printf'ed claps
Ferryy 0:6c254e376969 32
Ferryy 0:6c254e376969 33
Ferryy 0:6c254e376969 34 //-------------------[ Functions ]-------------------
Ferryy 0:6c254e376969 35 void clap_detected(void)
Ferryy 0:6c254e376969 36 {
Ferryy 0:6c254e376969 37 clapTime[currentClap++] = timer.read_ms(); //save clap time and increment counter
Ferryy 0:6c254e376969 38
Ferryy 0:6c254e376969 39 led_red = 0; //blink a led to show detected clap
Ferryy 0:6c254e376969 40 boardReset = 1; //reset the clap detecter board with a reset pulse
Ferryy 0:6c254e376969 41 wait(0.1); //wait to show led and also avoid handclap 'bounces'
Ferryy 0:6c254e376969 42 boardReset = 0;
Ferryy 0:6c254e376969 43 led_red = 1; //The LED pin is inverted so '1' means LED off
Ferryy 0:6c254e376969 44 }
Ferryy 0:6c254e376969 45
Ferryy 0:6c254e376969 46
Ferryy 0:6c254e376969 47
Ferryy 0:6c254e376969 48 //-------------------[ Main ]-------------------
Ferryy 0:6c254e376969 49 int main()
Ferryy 0:6c254e376969 50 {
Ferryy 0:6c254e376969 51 timer.start();
Ferryy 0:6c254e376969 52 boardVcc = 1; boardGnd = 0; boardReset = 0;
Ferryy 0:6c254e376969 53 led_red = 1; led_green = 1; led_blue = 1;
Ferryy 0:6c254e376969 54 boardClap.rise(&clap_detected);
Ferryy 0:6c254e376969 55
Ferryy 0:6c254e376969 56 while (true) {
Ferryy 0:6c254e376969 57 if(printedClap < currentClap) { //if the last clap isn't printed yet
Ferryy 0:6c254e376969 58 output.printf("Clap %d: %d\r\n", ++printedClap, clapTime[printedClap]); //debug the clap
Ferryy 0:6c254e376969 59 }
Ferryy 0:6c254e376969 60 if((currentClap > 1) && (timer.read_ms() > (clapTime[currentClap-1] + 5000))) { //reset when timeout (last clap is more then 5 seconds ago)
Ferryy 0:6c254e376969 61 output.printf("Timeout. %d claps were detected over a time of %dms\r\n", (currentClap - 1), (clapTime[currentClap] - clapTime[0]));
Ferryy 0:6c254e376969 62 currentClap = 0;
Ferryy 0:6c254e376969 63 printedClap = 0;
Ferryy 0:6c254e376969 64
Ferryy 0:6c254e376969 65 led_blue = 0; //blink a led
Ferryy 0:6c254e376969 66 wait(0.3);
Ferryy 0:6c254e376969 67 led_blue = 1; //The LED pin is inverted so '1' means LED off
Ferryy 0:6c254e376969 68
Ferryy 0:6c254e376969 69 }
Ferryy 0:6c254e376969 70
Ferryy 0:6c254e376969 71 /*led_blue = 1;
Ferryy 0:6c254e376969 72 led_green = 0;
Ferryy 0:6c254e376969 73 wait(0.2);
Ferryy 0:6c254e376969 74 led_blue = 0;
Ferryy 0:6c254e376969 75 led_green = 1;
Ferryy 0:6c254e376969 76 wait(0.2);*/
Ferryy 0:6c254e376969 77 }
Ferryy 0:6c254e376969 78 }