This is the Heart Rate demo program, testing and verifying the functionality of the HR sensor.

Dependencies:   mbed

Committer:
roberthill04
Date:
Wed Feb 24 17:43:56 2016 +0000
Revision:
0:92d3ea9d3e67
Heart Rate Monitor

Who changed what in which revision?

UserRevisionLine numberNew contents of line
roberthill04 0:92d3ea9d3e67 1 #ifndef PULSE_SENSOR_H
roberthill04 0:92d3ea9d3e67 2 #define PULSE_SENSOR_H
roberthill04 0:92d3ea9d3e67 3
roberthill04 0:92d3ea9d3e67 4 #include "mbed.h"
roberthill04 0:92d3ea9d3e67 5
roberthill04 0:92d3ea9d3e67 6
roberthill04 0:92d3ea9d3e67 7 /**
roberthill04 0:92d3ea9d3e67 8 * Class for interfacing with a http://pulsesensor.myshopify.com/ 'Pulse Sensor Amped'.
roberthill04 0:92d3ea9d3e67 9 * The contents of this class are based on the "Pulse Sensor Amped 1.1" Arduino Sketch.
roberthill04 0:92d3ea9d3e67 10 *
roberthill04 0:92d3ea9d3e67 11 * When using this class for the first time, it is recommended that you use the Processing
roberthill04 0:92d3ea9d3e67 12 * GUI app available http://pulsesensor.myshopify.com/pages/code-and-guide. Using this, you
roberthill04 0:92d3ea9d3e67 13 * will easily be able to verify the operating of your sensor, and the integration of this
roberthill04 0:92d3ea9d3e67 14 * class into your mbed project.
roberthill04 0:92d3ea9d3e67 15 * WEBSITE is incorrect^^^
roberthill04 0:92d3ea9d3e67 16 **/
roberthill04 0:92d3ea9d3e67 17
roberthill04 0:92d3ea9d3e67 18 class PulseSensor
roberthill04 0:92d3ea9d3e67 19 {
roberthill04 0:92d3ea9d3e67 20 private:
roberthill04 0:92d3ea9d3e67 21 volatile int rate[10]; // used to hold last ten IBI values
roberthill04 0:92d3ea9d3e67 22 volatile unsigned long sampleCounter; // used to determine pulse timing
roberthill04 0:92d3ea9d3e67 23 volatile unsigned long lastBeatTime; // used to find the inter beat interval
roberthill04 0:92d3ea9d3e67 24 volatile int P; // used to find peak in pulse wave
roberthill04 0:92d3ea9d3e67 25 volatile int T; // used to find trough in pulse wave
roberthill04 0:92d3ea9d3e67 26 volatile int thresh; // used to find instant moment of heart beat
roberthill04 0:92d3ea9d3e67 27 volatile int amp; // used to hold amplitude of pulse waveform
roberthill04 0:92d3ea9d3e67 28 volatile bool firstBeat; // used to seed rate array so we startup with reasonable BPM
roberthill04 0:92d3ea9d3e67 29 volatile bool secondBeat; // used to seed rate array so we startup with reasonable BPM
roberthill04 0:92d3ea9d3e67 30
roberthill04 0:92d3ea9d3e67 31 // these variables are volatile because they are used during the interrupt service routine!
roberthill04 0:92d3ea9d3e67 32
roberthill04 0:92d3ea9d3e67 33 volatile int Signal; // holds the incoming raw data
roberthill04 0:92d3ea9d3e67 34 volatile int IBI; // holds the time between beats, the Inter-Beat Interval
roberthill04 0:92d3ea9d3e67 35 volatile bool Pulse; // true when pulse wave is high, false when it's low
roberthill04 0:92d3ea9d3e67 36 volatile bool QS; // becomes true when a beat is found
roberthill04 0:92d3ea9d3e67 37
roberthill04 0:92d3ea9d3e67 38
roberthill04 0:92d3ea9d3e67 39 void (*_printDataCallback)(char,int);
roberthill04 0:92d3ea9d3e67 40 static const int _sensorTickRateMs = 2;
roberthill04 0:92d3ea9d3e67 41 int _callbackRateMs;
roberthill04 0:92d3ea9d3e67 42 bool _started;
roberthill04 0:92d3ea9d3e67 43
roberthill04 0:92d3ea9d3e67 44 AnalogIn *_pAin;
roberthill04 0:92d3ea9d3e67 45 Ticker _pulseSensorTicker;
roberthill04 0:92d3ea9d3e67 46 Ticker _processDataTicker;
roberthill04 0:92d3ea9d3e67 47
roberthill04 0:92d3ea9d3e67 48 void sensor_ticker_callback(void);
roberthill04 0:92d3ea9d3e67 49 void process_data_ticker_callback(void);
roberthill04 0:92d3ea9d3e67 50
roberthill04 0:92d3ea9d3e67 51 public:
roberthill04 0:92d3ea9d3e67 52
roberthill04 0:92d3ea9d3e67 53 volatile int BPM; // used to hold the pulse rate
roberthill04 0:92d3ea9d3e67 54 /** PulseSensor Constructor - Note this does not start the reading of the sensor.
roberthill04 0:92d3ea9d3e67 55 * @param analogPin Name of the analog pin that the sensor is connected to.
roberthill04 0:92d3ea9d3e67 56 * @param printDataCallback Pointer to function which will be called to print the latest data. Output format available here: http://pulsesensor.myshopify.com/pages/code-and-guide
roberthill04 0:92d3ea9d3e67 57 * @param callbackRateMs Rate at which the printDataCallback is to be called, recommended is 20ms for graphing of pulse signal.
roberthill04 0:92d3ea9d3e67 58 */
roberthill04 0:92d3ea9d3e67 59 PulseSensor(PinName analogPin, void (*printDataCallback)(char,int), int callbackRateMs=20);
roberthill04 0:92d3ea9d3e67 60
roberthill04 0:92d3ea9d3e67 61 /** Destructor */
roberthill04 0:92d3ea9d3e67 62 ~PulseSensor();
roberthill04 0:92d3ea9d3e67 63
roberthill04 0:92d3ea9d3e67 64 /** Start reading the Pulse Sensor, and sending current readings to the print data callback.
roberthill04 0:92d3ea9d3e67 65 * @returns true if reading of the sensor is started, false if reading was aleady in progress.
roberthill04 0:92d3ea9d3e67 66 */
roberthill04 0:92d3ea9d3e67 67 bool start();
roberthill04 0:92d3ea9d3e67 68
roberthill04 0:92d3ea9d3e67 69 /** Stops the current reading of the Pulse Senson.
roberthill04 0:92d3ea9d3e67 70 * @return true if reading is stopped, false if reading was already stopped.
roberthill04 0:92d3ea9d3e67 71 */
roberthill04 0:92d3ea9d3e67 72 bool stop();
roberthill04 0:92d3ea9d3e67 73 };
roberthill04 0:92d3ea9d3e67 74
roberthill04 0:92d3ea9d3e67 75 #endif