This program displays heart rate and time between heart beats on LCD, prints it to a USB serial port, print it to a bluetooth serial port and store it on a USB mass storage device. The program has two interrupt routines: 1.Every 1ms a counter is increased with one, 2. On every heart beat the counter is value copied. In the main loop the beats per minute are calculated. Ext.Modules:- Polar RMCM-01 heart rate module connected to pin8. - 2x16 LCD - a RF-BT0417CB bluetooth serial device connected to p27 and p28 - an USB mass storage device

Dependencies:   TextLCD mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 // This program displays heart rate and time between heart beats on LCD, prints it to a USB serial port,
00002 // print it to a bluetooth serial port and store it on a USB mass storage device.
00003 // The program has two interrupt routines:
00004 // 1.Every 1ms a counter is increased with one,
00005 // 2. On every heart beat the counter is value copied.
00006 // In the main loop the beats per minute are calculated.
00007 
00008 // Ext.Modules:- Polar RMCM-01 heart rate module connected to pin8.
00009 //             - 2x16 LCD
00010 //             - a RF-BT0417CB bluetooth serial device connected to p27 and p28
00011 //             - an USB mass storage device
00012 
00013 #include "mbed.h"
00014 #include "TextLCD.h"
00015 #include "MSCFileSystem.h"
00016 #define FSNAME "msc"
00017 MSCFileSystem msc(FSNAME);
00018 
00019 Timer t;                // counts the time from beginning of main loop
00020 InterruptIn beat(p8);   // beat is the name for a interrupt on pin 8
00021 TextLCD lcd(p10, p12, p15, p16, p29, p30); // rs, e, d0-d3
00022 Ticker mscnt;           // this is used to create a ms counter
00023 Serial bt(p28, p27);    // tx, rx of the bluetooth serial COM port
00024 
00025 int count,CNT,displayBPMFlag;//initialize global variables
00026 
00027 void ms_counter() {         //this interrupt routine starts every 1ms
00028     count++;                //the counter is increased with 1
00029     if (count>1999) {       //when no heart beat is detected for >2sec, then display "-" in the main loop
00030         CNT=count;          // copy counter value to CNT
00031         displayBPMFlag = 1; // set flag that the BPM can be put to LCD in the main loop
00032         count=0;            // reset counter value
00033     }
00034 }
00035 
00036 void flip() { //this interrupt routine starts on every heart beat
00037     CNT = count;        // copy counter value to CNT
00038     count = 0 ;         // reset counter value
00039     displayBPMFlag = 1; // set flag that the BPM can be put to LCD in the main loop
00040 }
00041 
00042 int main() {
00043     int BPM;                           //initialize BPM locally
00044     lcd.cls();                         //clear the lcd display
00045     lcd.locate(0,0);            //set cursor to first character and first line
00046     lcd.printf("Heart Rate");  //display the time in ms on the lcd
00047     lcd.locate(0,1);            //set cursor to first character and second line
00048     lcd.printf("Jasper Sikken");  //display the beats per minute on the lcd
00049     wait_ms(2000);
00050     mscnt.attach_us(&ms_counter,1000); //the address of the function to be attached (ms_counter) and the interval (1ms)
00051     printf("0 \r\n");                  //print 0 ms to the serial port
00052     bt.printf("0 \r\n");               //print 0 ms to the bluetooth serial port
00053     FILE *fp=fopen("/msc/HR.txt","a+");//Open file on USB Mass Storage device, 8.3 filesystem, open for write and append
00054     if (fp==NULL)error("Could not open file for write\n");// error code
00055     fprintf(fp,"0,0 \r\n");            //write "0,0" to file to indicate beginning of a measurement
00056     fclose(fp);                        //close file
00057     lcd.cls();                         //clear the lcd display
00058     t.start();                         //the time since the beginning of the main loop
00059     beat.rise(&flip);                  //the interrupt flip is started to the rising edge
00060     while (1) {                        //program loops around in here
00061         if (displayBPMFlag == 1) {     //if the flag is set that the BPM can be used in the main loop
00062             displayBPMFlag = 0;        //clear displayBPMflag
00063             if (CNT>250&CNT<2000) {    //when heart rate is within 30-240BPM
00064                 BPM = 60000/CNT;            //calculate BPM
00065                 lcd.locate(0,0);            //set cursor to first character and first line
00066                 lcd.printf("%i ms ",CNT);  //display the time in ms on the lcd
00067                 lcd.locate(0,1);            //set cursor to first character and second line
00068                 lcd.printf("%i BPM ",BPM);  //display the beats per minute on the lcd
00069                 printf("%i\r\n",CNT);       //print time between heart beats to the serial port
00070                 bt.printf("%i\r\n",CNT);    //print time between heart beats to the bluetooth serial port
00071                 FILE *fp=fopen("/msc/HR.txt","a+");//Open file on USB Mass Storage device, 8.3 filesystem, open for write and append
00072                 if ( fp == NULL )error("Could not open file for write\n");//error code
00073                 fprintf(fp, "%f,%i\r\n",t.read(),BPM);//write time-since-start-main-loop and BPM to file
00074                 fclose(fp);                 //close file
00075             } else {                //when heart rate is NOT within 30-240BPM
00076                 lcd.cls();                  //clear the lcd display
00077                 lcd.locate(0,0);            //set cursor to first character and first line
00078                 lcd.printf("- ms");         //display empty time in ms on the lcd
00079                 lcd.locate(0,1);            //set cursor to first character and second line
00080                 lcd.printf("- BPM");        //display empty beats per minute on the lcd
00081                 printf("0 \r\n");       //print no time in ms to the serial port
00082                 bt.printf("0 \r\n");    //print no between heart beats to the bluetooth serial port
00083                 FILE *fp = fopen( "/msc/HR.txt", "a+");//Open file on USB Mass Storage device, 8.3 filesystem, open for write and append
00084                 if ( fp == NULL )error("Could not open file for write\n"); // error code
00085                 fprintf(fp, "0,0 \r\n");   //write "0,0" to file
00086                 fclose(fp);             //close file
00087             }
00088 
00089         }
00090 
00091     }
00092 
00093 }