/*############################################################# Program Name : ex1_PulseSensor Author : Grant Phillips Date Modified : 22/07/2016 Compiler : ARMmbed Tested On : STM32F4-Discovery Description : Example that demonstrate the use of a Pulse Sensor (http://www.pulsesensor.com) to measure a person's heart beat rate (Beats Per Minute or BPM). An LED is flashed to indicate a heart beat and the Serial Wire Viewer (SWV) is used to show the BPM value for testing purposes, but is not required for the operation of the Pulse Sensor. Please keep in mind that the sensor works on an average rate and will there- fore need a few seconds before indicating the correct BPM value. Requirements : * STM32F4-Discovery Board Circuit : * a LED connected to PD_15 * The PulseSensor is connected as follows: S (Yellow wire) - PC1 + (Grey wire) - 3V - (White wire) - GND * A wire link between PB3 and pin6 of the SWD connector (CN2) as explained in the SWV examples section ##############################################################*/ #include "mbed.h" #include "SWO.h" #include "PulseSensor.h" SWO_Channel swo; //serial wire viewer object for debug information DigitalOut PulseLED(PD_15); //LED to indicate beat detected int BPM; //variable to store Beast Per Minute value int BeatFlag=0; //flag to indicate beat detected // this function gets called every 20ms to check the new data available from the sensor void ProcessSensorData(char symbol, int data) { if(symbol == 'B'){ //if "Beat" information is received BeatFlag=1; //set a flag to notify main() BPM=data; //get the BPM value from sensor } } PulseSensor sensor(PC_1, ProcessSensorData); //Create a PulseSensor object called sensor // Sensor input pin, function to call when data is ready int main() { swo.printf("Program started...\n"); sensor.start(); //start the PulseRate sensor readings while(1) { /* check if new BPM information arrived */ if(BeatFlag==1) { //if new beat detected PulseLED=1; //turn LED ON swo.printf("BPM: %d\n", BPM); BeatFlag=0; //clear flag wait(0.015); //wait short period before turning LED off PulseLED=0; } } }