LED Pierce and pulse program.

Dependencies:   mbed

Committer:
asagin
Date:
Sat Jan 11 12:52:56 2014 +0000
Revision:
1:171a73d84261
Parent:
0:a78b08608d76
interrupt is not understandable.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
asagin 0:a78b08608d76 1 #include "mbed.h"
asagin 0:a78b08608d76 2
asagin 0:a78b08608d76 3 /*
asagin 0:a78b08608d76 4 >> Pulse Sensor Amped 1.1 <<
asagin 0:a78b08608d76 5 This code is for Pulse Sensor Amped by Joel Murphy and Yury Gitman
asagin 0:a78b08608d76 6 www.pulsesensor.com
asagin 0:a78b08608d76 7 >>> Pulse Sensor purple wire goes to Analog Pin 0 <<<
asagin 0:a78b08608d76 8 Pulse Sensor sample aquisition and processing happens in the background via Timer 2 interrupt. 2mS sample rate.
asagin 0:a78b08608d76 9 PWM on pins 3 and 11 will not work when using this code, because we are using Timer 2!
asagin 0:a78b08608d76 10 The following variables are automatically updated:
asagin 0:a78b08608d76 11 Signal : int that holds the analog signal data straight from the sensor. updated every 2mS.
asagin 0:a78b08608d76 12 IBI : int that holds the time interval between beats. 2mS resolution.
asagin 0:a78b08608d76 13 BPM : int that holds the heart rate value, derived every beat, from averaging previous 10 IBI values.
asagin 0:a78b08608d76 14 QS : boolean that is made true whenever Pulse is found and BPM is updated. User must reset.
asagin 0:a78b08608d76 15 Pulse : boolean that is true when a heartbeat is sensed then false in time with pin13 LED going out.
asagin 0:a78b08608d76 16
asagin 0:a78b08608d76 17 This code is designed with output serial data to Processing sketch "PulseSensorAmped_Processing-xx"
asagin 0:a78b08608d76 18 The Processing sketch is a simple data visualizer.
asagin 0:a78b08608d76 19 All the work to find the heartbeat and determine the heartrate happens in the code below.
asagin 0:a78b08608d76 20 Pin 13 LED will blink with heartbeat.
asagin 0:a78b08608d76 21 If you want to use pin 13 for something else, adjust the interrupt handler
asagin 0:a78b08608d76 22 It will also fade an LED on pin fadePin with every beat. Put an LED and series resistor from fadePin to GND.
asagin 0:a78b08608d76 23 Check here for detailed code walkthrough:
asagin 0:a78b08608d76 24 http://pulsesensor.myshopify.com/pages/pulse-sensor-amped-arduino-v1dot1
asagin 0:a78b08608d76 25
asagin 0:a78b08608d76 26 Code Version 02 by Joel Murphy & Yury Gitman Fall 2012
asagin 0:a78b08608d76 27 This update changes the HRV variable name to IBI, which stands for Inter-Beat Interval, for clarity.
asagin 0:a78b08608d76 28 Switched the interrupt to Timer2. 500Hz sample rate, 2mS resolution IBI value.
asagin 0:a78b08608d76 29 Fade LED pin moved to pin 5 (use of Timer2 disables PWM on pins 3 & 11).
asagin 0:a78b08608d76 30 Tidied up inefficiencies since the last version.
asagin 0:a78b08608d76 31 */
asagin 0:a78b08608d76 32
asagin 0:a78b08608d76 33 /*
asagin 0:a78b08608d76 34 // VARIABLES
asagin 0:a78b08608d76 35 int pulsePin = 0; // Pulse Sensor purple wire connected to analog pin 0
asagin 0:a78b08608d76 36 int blinkPin = 13; // pin to blink led at each beat
asagin 0:a78b08608d76 37 int fadePin = 5; // pin to do fancy classy fading blink at each beat
asagin 0:a78b08608d76 38 int fadeRate = 0; // used to fade LED on with PWM on fadePin
asagin 0:a78b08608d76 39 */
asagin 0:a78b08608d76 40
asagin 0:a78b08608d76 41 AnalogIn pulsePin(p15); //This is signal input
asagin 0:a78b08608d76 42 SPI blinkPin(p11,p12,p13);
asagin 0:a78b08608d76 43 PwmOut fadePin(p26);
asagin 0:a78b08608d76 44 int fadeRate = 0;
asagin 0:a78b08608d76 45
asagin 0:a78b08608d76 46 // these variables are volatile because they are used during the interrupt service routine!
asagin 0:a78b08608d76 47 volatile int BPM; // used to hold the pulse rate
asagin 0:a78b08608d76 48 volatile int Signal; // holds the incoming raw data
asagin 0:a78b08608d76 49 volatile int IBI = 600; // holds the time between beats, the Inter-Beat Interval
asagin 0:a78b08608d76 50 //volatile boolean Pulse = false; // true when pulse wave is high, false when it's low
asagin 0:a78b08608d76 51 //volatile boolean QS = false; // becomes true when Arduoino finds a beat.
asagin 0:a78b08608d76 52 volatile bool Pulse = false; // true when pulse wave is high, false when it's low
asagin 0:a78b08608d76 53 volatile bool QS = false; // becomes true when Arduoino finds a beat.
asagin 0:a78b08608d76 54 /*
asagin 0:a78b08608d76 55 void setup(){
asagin 0:a78b08608d76 56 pinMode(blinkPin,OUTPUT); // pin that will blink to your heartbeat!
asagin 0:a78b08608d76 57 pinMode(fadePin,OUTPUT); // pin that will fade to your heartbeat!
asagin 0:a78b08608d76 58 Serial.begin(115200); // we agree to talk fast!
asagin 0:a78b08608d76 59 interruptSetup(); // sets up to read Pulse Sensor signal every 2mS
asagin 0:a78b08608d76 60 // UN-COMMENT THE NEXT LINE IF YOU ARE POWERING The Pulse Sensor AT LOW VOLTAGE,
asagin 0:a78b08608d76 61 // AND APPLY THAT VOLTAGE TO THE A-REF PIN
asagin 0:a78b08608d76 62 //analogReference(EXTERNAL);
asagin 0:a78b08608d76 63 }
asagin 0:a78b08608d76 64 */
asagin 0:a78b08608d76 65
asagin 0:a78b08608d76 66 void setup(){
asagin 0:a78b08608d76 67 // Serial.begin (115200); // we agree to talk fast!
asagin 0:a78b08608d76 68 // interruptSetup(); // sets up to read Pulse Sensor signal every 2mS
asagin 0:a78b08608d76 69 Serial deta(p9,p10); // we agree to talk fast!
asagin 0:a78b08608d76 70 interruptSetup(); // sets up to read Pulse Sensor signal every 2mS
asagin 0:a78b08608d76 71
asagin 0:a78b08608d76 72 // UN-COMMENT THE NEXT LINE IF YOU ARE POWERING The Pulse Sensor AT LOW VOLTAGE,
asagin 0:a78b08608d76 73 // AND APPLY THAT VOLTAGE TO THE A-REF PIN
asagin 0:a78b08608d76 74 //analogReference(EXTERNAL);
asagin 0:a78b08608d76 75 }
asagin 0:a78b08608d76 76
asagin 0:a78b08608d76 77 void loop(){
asagin 0:a78b08608d76 78 sendDataToProcessing('S', Signal); // send Processing the raw Pulse Sensor data
asagin 0:a78b08608d76 79 if (QS == true){ // Quantified Self flag is true when arduino finds a heartbeat
asagin 0:a78b08608d76 80 fadeRate = 255; // Set 'fadeRate' Variable to 255 to fade LED with pulse
asagin 0:a78b08608d76 81 sendDataToProcessing('B',BPM); // send heart rate with a 'B' prefix
asagin 0:a78b08608d76 82 sendDataToProcessing('Q',IBI); // send time between beats with a 'Q' prefix
asagin 0:a78b08608d76 83 QS = false; // reset the Quantified Self flag for next time
asagin 0:a78b08608d76 84 }
asagin 0:a78b08608d76 85
asagin 0:a78b08608d76 86 ledFadeToBeat();
asagin 0:a78b08608d76 87
asagin 0:a78b08608d76 88 delay(20); // take a break
asagin 0:a78b08608d76 89 }
asagin 0:a78b08608d76 90
asagin 0:a78b08608d76 91
asagin 0:a78b08608d76 92 void ledFadeToBeat(){
asagin 0:a78b08608d76 93 fadeRate -= 15; // set LED fade value
asagin 0:a78b08608d76 94 fadeRate = constrain(fadeRate,0,255); // keep LED fade value from going into negative numbers!
asagin 0:a78b08608d76 95 analogWrite(fadePin,fadeRate); // fade LED
asagin 0:a78b08608d76 96 }
asagin 0:a78b08608d76 97
asagin 0:a78b08608d76 98
asagin 0:a78b08608d76 99 void sendDataToProcessing(char symbol, int data ){
asagin 0:a78b08608d76 100 Serial.print(symbol); // symbol prefix tells Processing what type of data is coming
asagin 0:a78b08608d76 101 Serial.println(data); // the data to send culminating in a carriage return
asagin 0:a78b08608d76 102 }