https://www.adafruit.com/product/1077
Embed:
(wiki syntax)
Show/hide line numbers
main.cpp
00001 #include "mbed.h" 00002 00003 #define INTERRUPT_MODE 00004 //#define DIGITAL_IN_MODE 00005 00006 DigitalOut led(D10); 00007 00008 #ifdef DIGITAL_IN_MODE 00009 //DigitalOut myled(LED1); 00010 DigitalIn mypin(D9); 00011 #endif 00012 00013 #ifdef INTERRUPT_MODE 00014 InterruptIn hr(D6); 00015 Ticker flipper; 00016 #endif 00017 00018 int hr_cnt = 0; 00019 00020 void flip() 00021 { 00022 printf("Beat - int\n"); 00023 led = !led; 00024 hr_cnt++; 00025 } 00026 00027 void calculate(){ 00028 hr_cnt = hr_cnt * 6; 00029 printf("Heartrate %d \n", hr_cnt); 00030 hr_cnt = 0; 00031 } 00032 00033 int read, old_read; 00034 00035 int main() 00036 { 00037 #ifdef INTERRUPT_MODE 00038 hr.rise(&flip); 00039 flipper.attach(callback(calculate),10); // setup flipper to call flip after 2 seconds 00040 #endif 00041 00042 00043 #ifdef DIGITAL_IN_MODE 00044 if(mypin.is_connected()) { 00045 printf("mypin is connected and initialized! \n\r"); 00046 } 00047 00048 // Optional: set mode as PullUp/PullDown/PullNone/OpenDrain 00049 mypin.mode(PullDown); 00050 00051 read = mypin.read(); 00052 00053 //printf("beat %d \n", read); 00054 00055 printf("Waiting for heart beat...\n\n"); 00056 00057 while (!mypin.read()) {}; 00058 printf("Heart beat detected!\n"); 00059 #endif 00060 00061 while(1) { 00062 00063 00064 #ifdef DIGITAL_IN_MODE 00065 00066 read = mypin.read(); 00067 if (read && (old_read != read)) { 00068 printf("Beat\n"); 00069 } 00070 00071 old_read = read; 00072 #endif 00073 //printf("beat %d \n", read); 00074 //printf("hello \n\n"); 00075 00076 //led.write(0); 00077 //wait(1.0); 00078 //led.write(1); 00079 //wait(1.0); 00080 // 00081 // myled = 1; // LED is ON 00082 // wait(0.2); // 200 ms 00083 // myled = 0; // LED is OFF 00084 // wait(1.0); // 1 sec 00085 } 00086 }
Generated on Mon Jul 18 2022 04:16:58 by
1.7.2