Library for interfacing with a 'Pulse Sensor Amped', from http://pulsesensor.myshopify.com

Dependents:   finalProject PulseSense HealthCare_Graduation 4180project ... more

Simple mbed application for monitoring a pulse sensor

#include "mbed.h"
#include "PulseSensor.h"

Serial pc(USBTX, USBRX);
    

/** Print the data in a format that can be parsed by the 
 *  Processing application available here: http://pulsesensor.myshopify.com/pages/code-and-guide
 */
void sendDataToProcessing(char symbol, int data)
{
    pc.printf("%c%d\r\n", symbol, data);
}



int main() {
    
    PulseSensor sensor(p15, sendDataToProcessing);

    pc.baud(115200);
    
    sensor.start();

    while(1) {
    }
}

/media/uploads/donalm/mbed_pulsesensor_scaled.png

Committer:
donalm
Date:
Sun Feb 09 15:37:19 2014 +0000
Revision:
0:e80a245c4d0d
Initial version.

Who changed what in which revision?

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