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.
main.cpp
00001 #include "mbed.h" 00002 00003 DigitalOut led1(LED1); 00004 InterruptIn sw(SW2); //Use SW1 on some platforms 00005 Timer timer; 00006 00007 uint8_t recording = 0; //indicates whether recording a new pattern or not 00008 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 00009 uint8_t blinkCount = 0; //starts at 0 for first knock 00010 uint32_t startTime; //timer time in miliseconds 00011 00012 //interrupt routine that is called when button is pressed 00013 void fall_handler(void) { 00014 00015 //if not recording yet 00016 if(recording ==0) 00017 { 00018 //this is the first button press 00019 blinkCount = 0; 00020 led1 = 1; //turn off led 00021 //clear the array that stores the pattern 00022 for(uint8_t i = 0; i<20; i++) 00023 { 00024 blinkTiming[i] = 0; 00025 } 00026 //set state to recording 00027 recording = 1; 00028 //start timer 00029 timer.start(); 00030 timer.reset(); 00031 startTime = timer.read_ms(); 00032 00033 //if already recording 00034 }else if(recording == 1) 00035 { 00036 //this is not the first button press 00037 uint32_t now; 00038 //check the time verses the start time and save it 00039 now = timer.read_ms(); 00040 blinkTiming[blinkCount] = (now - startTime); 00041 //add to the number of button presses captured - same as number of blinks that will come later 00042 blinkCount++; 00043 startTime = now; //start time for next period 00044 } 00045 00046 } 00047 00048 // main() runs in its own thread in the OS 00049 int main() { 00050 00051 //set state to not recording 00052 recording = 0; 00053 //set interrupt callback for when the button is pressed 00054 sw.fall(fall_handler); 00055 00056 00057 while(1) 00058 { 00059 //if state is recording 00060 if(recording==1) 00061 { 00062 //check if the max button presses (and thus blinks) has been reached, or time period has expired 00063 if (blinkCount >= 20 || timer.read_ms() > 5000) //max presses reached or 5 sec elapsed - done recording 00064 { 00065 timer.stop(); 00066 /* re-initialize all variables */ 00067 recording = 0; 00068 blinkCount = 0; 00069 00070 } 00071 //if state not recording 00072 }else if(recording == 0) 00073 { 00074 while(recording == 0) 00075 { 00076 //blink out the pattern 00077 for(uint8_t i = 0;i < 20; i++) 00078 { 00079 //blink led 00080 led1 = 0; 00081 wait_ms(10); 00082 led1 = 1; 00083 //wait for specified delay that was captured 00084 wait_ms(blinkTiming[i]); 00085 //check if the delay is zero, if so its the last one so stop. 00086 if(blinkTiming[i] == 0){ 00087 break; 00088 } 00089 00090 } 00091 //take a break for a few seconds, then loop back and repeat the pattern again 00092 wait_ms(2000); 00093 } //end of while(recording == 0) 00094 } //end of if(recording == 0) 00095 00096 } //end of while(1) infinite loop 00097 00098 } //end of main
Generated on Sat Jul 16 2022 02:00:19 by
1.7.2