This is a simple application that stores the timing of button presses (within a 5 second period), then replays the sequence back out by flashing an LED in the same timing intervals. This was based on a secret knock program I did a long time ago.

Committer:
maclobdell
Date:
Tue Jun 27 20:40:36 2017 +0000
Revision:
0:11f1739fb2ad
Initial version.  It works!

Who changed what in which revision?

UserRevisionLine numberNew contents of line
maclobdell 0:11f1739fb2ad 1 #include "mbed.h"
maclobdell 0:11f1739fb2ad 2
maclobdell 0:11f1739fb2ad 3 DigitalOut led1(LED1);
maclobdell 0:11f1739fb2ad 4 InterruptIn sw(SW2); //Use SW1 on some platforms
maclobdell 0:11f1739fb2ad 5 Timer timer;
maclobdell 0:11f1739fb2ad 6
maclobdell 0:11f1739fb2ad 7 uint8_t recording = 0; //indicates whether recording a new pattern or not
maclobdell 0:11f1739fb2ad 8 uint32_t blinkTiming[20] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}; //to store pattern - series of milisecond time delays
maclobdell 0:11f1739fb2ad 9 uint8_t blinkCount = 0; //starts at 0 for first knock
maclobdell 0:11f1739fb2ad 10 uint32_t startTime; //timer time in miliseconds
maclobdell 0:11f1739fb2ad 11
maclobdell 0:11f1739fb2ad 12 //interrupt routine that is called when button is pressed
maclobdell 0:11f1739fb2ad 13 void fall_handler(void) {
maclobdell 0:11f1739fb2ad 14
maclobdell 0:11f1739fb2ad 15 //if not recording yet
maclobdell 0:11f1739fb2ad 16 if(recording ==0)
maclobdell 0:11f1739fb2ad 17 {
maclobdell 0:11f1739fb2ad 18 //this is the first button press
maclobdell 0:11f1739fb2ad 19 blinkCount = 0;
maclobdell 0:11f1739fb2ad 20 led1 = 1; //turn off led
maclobdell 0:11f1739fb2ad 21 //clear the array that stores the pattern
maclobdell 0:11f1739fb2ad 22 for(uint8_t i = 0; i<20; i++)
maclobdell 0:11f1739fb2ad 23 {
maclobdell 0:11f1739fb2ad 24 blinkTiming[i] = 0;
maclobdell 0:11f1739fb2ad 25 }
maclobdell 0:11f1739fb2ad 26 //set state to recording
maclobdell 0:11f1739fb2ad 27 recording = 1;
maclobdell 0:11f1739fb2ad 28 //start timer
maclobdell 0:11f1739fb2ad 29 timer.start();
maclobdell 0:11f1739fb2ad 30 timer.reset();
maclobdell 0:11f1739fb2ad 31 startTime = timer.read_ms();
maclobdell 0:11f1739fb2ad 32
maclobdell 0:11f1739fb2ad 33 //if already recording
maclobdell 0:11f1739fb2ad 34 }else if(recording == 1)
maclobdell 0:11f1739fb2ad 35 {
maclobdell 0:11f1739fb2ad 36 //this is not the first button press
maclobdell 0:11f1739fb2ad 37 uint32_t now;
maclobdell 0:11f1739fb2ad 38 //check the time verses the start time and save it
maclobdell 0:11f1739fb2ad 39 now = timer.read_ms();
maclobdell 0:11f1739fb2ad 40 blinkTiming[blinkCount] = (now - startTime);
maclobdell 0:11f1739fb2ad 41 //add to the number of button presses captured - same as number of blinks that will come later
maclobdell 0:11f1739fb2ad 42 blinkCount++;
maclobdell 0:11f1739fb2ad 43 startTime = now; //start time for next period
maclobdell 0:11f1739fb2ad 44 }
maclobdell 0:11f1739fb2ad 45
maclobdell 0:11f1739fb2ad 46 }
maclobdell 0:11f1739fb2ad 47
maclobdell 0:11f1739fb2ad 48 // main() runs in its own thread in the OS
maclobdell 0:11f1739fb2ad 49 int main() {
maclobdell 0:11f1739fb2ad 50
maclobdell 0:11f1739fb2ad 51 //set state to not recording
maclobdell 0:11f1739fb2ad 52 recording = 0;
maclobdell 0:11f1739fb2ad 53 //set interrupt callback for when the button is pressed
maclobdell 0:11f1739fb2ad 54 sw.fall(fall_handler);
maclobdell 0:11f1739fb2ad 55
maclobdell 0:11f1739fb2ad 56
maclobdell 0:11f1739fb2ad 57 while(1)
maclobdell 0:11f1739fb2ad 58 {
maclobdell 0:11f1739fb2ad 59 //if state is recording
maclobdell 0:11f1739fb2ad 60 if(recording==1)
maclobdell 0:11f1739fb2ad 61 {
maclobdell 0:11f1739fb2ad 62 //check if the max button presses (and thus blinks) has been reached, or time period has expired
maclobdell 0:11f1739fb2ad 63 if (blinkCount >= 20 || timer.read_ms() > 5000) //max presses reached or 5 sec elapsed - done recording
maclobdell 0:11f1739fb2ad 64 {
maclobdell 0:11f1739fb2ad 65 timer.stop();
maclobdell 0:11f1739fb2ad 66 /* re-initialize all variables */
maclobdell 0:11f1739fb2ad 67 recording = 0;
maclobdell 0:11f1739fb2ad 68 blinkCount = 0;
maclobdell 0:11f1739fb2ad 69
maclobdell 0:11f1739fb2ad 70 }
maclobdell 0:11f1739fb2ad 71 //if state not recording
maclobdell 0:11f1739fb2ad 72 }else if(recording == 0)
maclobdell 0:11f1739fb2ad 73 {
maclobdell 0:11f1739fb2ad 74 while(recording == 0)
maclobdell 0:11f1739fb2ad 75 {
maclobdell 0:11f1739fb2ad 76 //blink out the pattern
maclobdell 0:11f1739fb2ad 77 for(uint8_t i = 0;i < 20; i++)
maclobdell 0:11f1739fb2ad 78 {
maclobdell 0:11f1739fb2ad 79 //blink led
maclobdell 0:11f1739fb2ad 80 led1 = 0;
maclobdell 0:11f1739fb2ad 81 wait_ms(10);
maclobdell 0:11f1739fb2ad 82 led1 = 1;
maclobdell 0:11f1739fb2ad 83 //wait for specified delay that was captured
maclobdell 0:11f1739fb2ad 84 wait_ms(blinkTiming[i]);
maclobdell 0:11f1739fb2ad 85 //check if the delay is zero, if so its the last one so stop.
maclobdell 0:11f1739fb2ad 86 if(blinkTiming[i] == 0){
maclobdell 0:11f1739fb2ad 87 break;
maclobdell 0:11f1739fb2ad 88 }
maclobdell 0:11f1739fb2ad 89
maclobdell 0:11f1739fb2ad 90 }
maclobdell 0:11f1739fb2ad 91 //take a break for a few seconds, then loop back and repeat the pattern again
maclobdell 0:11f1739fb2ad 92 wait_ms(2000);
maclobdell 0:11f1739fb2ad 93 } //end of while(recording == 0)
maclobdell 0:11f1739fb2ad 94 } //end of if(recording == 0)
maclobdell 0:11f1739fb2ad 95
maclobdell 0:11f1739fb2ad 96 } //end of while(1) infinite loop
maclobdell 0:11f1739fb2ad 97
maclobdell 0:11f1739fb2ad 98 } //end of main