Skip to content

Commit c779339

Browse files
authored
Merge pull request #38 from biomurph/master
Added Features
2 parents bd1257d + cb4e811 commit c779339

File tree

9 files changed

+220
-10
lines changed

9 files changed

+220
-10
lines changed

README.md

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,22 +97,33 @@ More Info On Libraries in General 👉 [https://www.arduino.cc/en/Guide/Libra
9797

9898
### Processing Visualizer:
9999

100-
Get detailed visulization of the heart's pulse and behavior. Send the PulseSensor data into Processing!
100+
Get detailed visualization of the heart's pulse and behavior. Send the PulseSensor data into Processing!
101101

102102
- [**Project Page**](https://pulsesensor.com/pages/getting-advanced)
103103

104104
<img src="https://cdn.shopify.com/s/files/1/0100/6632/files/ScreenShot_1024x1024.png?v=1491857113" width="400">
105105

106106
---
107107

108+
### Pulse Transit Time:
109+
110+
Use two Pulse Sensors on different parts of your body to measure Pulse Transit Time!
111+
112+
- [**Project Page**](https://pulsesensor.com/pages/pulse-transit-time)
113+
114+
<img src="https://cdn.shopify.com/s/files/1/0100/6632/files/PulseSensor_PTT-17042_grande.jpg?v=1517336059" width="400">
115+
116+
---
117+
108118
## Connecting the Harware 😎
109119
1. Prepare the sensor, with the Kit parts.
110120

111121
<img src="https://cdn.shopify.com/s/files/1/0100/6632/products/PulseSensorKit-Labeled-Contents_1_2048x2048.jpg?v=1348506345" width="400">
112122

113123
2. See the recommended wiring for your specific project
114124

115-
<img src="https://github.com/WorldFamousElectronics/PulseSensorStarterProject/blob/master/connections.png" width="400">
125+
<img src="https://github.com/WorldFamousElectronics/PulseSensorStarterProject/raw/master/connections.png" width="400">
126+
116127
<img src="https://github.com/WorldFamousElectronics/PulseSensorStarterProject/raw/master/Arduino-LEDonPin13-PulseSensor-Pic.jpg" width="400">
117128

118129
---
Lines changed: 181 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,181 @@
1+
/*
2+
Arduino Sketch to detect pulses from two PulseSensors
3+
and measures the time between! This can be used to derive
4+
Pulse Transit Time (PTT)
5+
6+
Here is a link to the PTT tutorial
7+
https://pulsesensor.com/pages/pulse-transit-time
8+
9+
Copyright World Famous Electronics LLC - see LICENSE
10+
Contributors:
11+
Joel Murphy, https://pulsesensor.com
12+
Yury Gitman, https://pulsesensor.com
13+
Bradford Needham, @bneedhamia, https://bluepapertech.com
14+
15+
Licensed under the MIT License, a copy of which
16+
should have been included with this software.
17+
18+
This software is not intended for medical use.
19+
*/
20+
21+
/*
22+
Every Sketch that uses the PulseSensor Playground must
23+
define USE_ARDUINO_INTERRUPTS before including PulseSensorPlayground.h.
24+
Here, #define USE_ARDUINO_INTERRUPTS false tells the library to
25+
not use interrupts to read data from the PulseSensor.
26+
27+
If you want to use interrupts, simply change the line below
28+
to read:
29+
#define USE_ARDUINO_INTERRUPTS true
30+
31+
Set US_PS_INTERRUPTS to false if either
32+
1) Your Arduino platform's interrupts aren't yet supported
33+
by PulseSensor Playground, or
34+
2) You don't wish to use interrupts because of the side effects.
35+
36+
NOTE: if US_PS_INTERRUPTS is false, your Sketch must
37+
call pulse.sawNewSample() at least once every 2 milliseconds
38+
to accurately read the PulseSensor signal.
39+
*/
40+
#define USE_ARDUINO_INTERRUPTS true
41+
#include <PulseSensorPlayground.h>
42+
43+
44+
/*
45+
The format of our output.
46+
47+
Set this to PROCESSING_VISUALIZER if you're going to run
48+
the multi-sensor Processing Visualizer Sketch.
49+
See https://github.com/WorldFamousElectronics/PulseSensorAmped_2_Sensors
50+
51+
Set this to SERIAL_PLOTTER if you're going to run
52+
the Arduino IDE's Serial Plotter.
53+
*/
54+
const int OUTPUT_TYPE = PROCESSING_VISUALIZER;
55+
56+
/*
57+
Number of PulseSensor devices we're reading from.
58+
*/
59+
const int PULSE_SENSOR_COUNT = 2;
60+
61+
/*
62+
PIN_POWERx = the output pin that the red (power) pin of
63+
the first PulseSensor will be connected to. PulseSensor only
64+
draws about 4mA, so almost any micro can power it from a GPIO.
65+
If you don't want to use pins to power the PulseSensors, you can remove
66+
the code dealing with PIN_POWER0 and PIN_POWER1.
67+
PIN_INPUTx = Analog Input. Connected to the pulse sensor
68+
purple (signal) wire.
69+
PIN_BLINKx = digital Output. Connected to an LED (must have at least
70+
470 ohm resistor) that will flash on each detected pulse.
71+
PIN_FADEx = digital Output. PWM pin onnected to an LED (must have
72+
at least 470 ohm resistor) that will smoothly fade with each pulse.
73+
74+
NOTE: PIN_FADEx must be pins that support PWM.
75+
If USE_INTERRUPTS is true, Do not use pin 9 or 10 for PIN_FADEx
76+
because those pins' PWM interferes with the sample timer.
77+
*/
78+
const int PIN_INPUT0 = A0;
79+
const int PIN_BLINK0 = 13; // Pin 13 is the on-board LED
80+
const int PIN_FADE0 = 5;
81+
82+
const int PIN_INPUT1 = A1;
83+
const int PIN_BLINK1 = 12;
84+
const int PIN_FADE1 = 11;
85+
86+
const int THRESHOLD = 550; // Adjust this number to avoid noise when idle
87+
88+
/*
89+
All the PulseSensor Playground functions.
90+
We tell it how many PulseSensors we're using.
91+
*/
92+
PulseSensorPlayground pulseSensor(PULSE_SENSOR_COUNT);
93+
94+
/*
95+
Variables used to determine PTT.
96+
NOTE: This code assumes the Pulse Sensor on analog pin 0 is closer to he heart.
97+
*/
98+
unsigned long lastBeatSampleNumber[PULSE_SENSOR_COUNT];
99+
int PTT;
100+
101+
void setup() {
102+
/*
103+
Use 250000 baud because that's what the Processing Sketch expects to read,
104+
and because that speed provides about 25 bytes per millisecond,
105+
or 50 characters per PulseSensor sample period of 2 milliseconds.
106+
107+
If we used a slower baud rate, we'd likely write bytes faster than
108+
they can be transmitted, which would mess up the timing
109+
of readSensor() calls, which would make the pulse measurement
110+
not work properly.
111+
*/
112+
Serial.begin(250000);
113+
114+
/*
115+
Configure the PulseSensor manager,
116+
telling it which PulseSensor (0 or 1)
117+
we're configuring.
118+
*/
119+
120+
pulseSensor.analogInput(PIN_INPUT0, 0);
121+
pulseSensor.blinkOnPulse(PIN_BLINK0, 0);
122+
pulseSensor.fadeOnPulse(PIN_FADE0, 0);
123+
124+
pulseSensor.analogInput(PIN_INPUT1, 1);
125+
pulseSensor.blinkOnPulse(PIN_BLINK1, 1);
126+
pulseSensor.fadeOnPulse(PIN_FADE1, 1);
127+
128+
pulseSensor.setSerial(Serial);
129+
pulseSensor.setOutputType(OUTPUT_TYPE);
130+
pulseSensor.setThreshold(THRESHOLD);
131+
132+
133+
// Now that everything is ready, start reading the PulseSensor signal.
134+
if (!pulseSensor.begin()) {
135+
/*
136+
PulseSensor initialization failed,
137+
likely because our Arduino platform interrupts
138+
aren't supported yet.
139+
140+
If your Sketch hangs here, try changing USE_ARDUINO_INTERRUPTS to false.
141+
*/
142+
for (;;) {
143+
// Flash the led to show things didn't work.
144+
digitalWrite(PIN_BLINK0, LOW);
145+
delay(50);
146+
digitalWrite(PIN_BLINK0, HIGH);
147+
delay(50);
148+
}
149+
}
150+
}
151+
152+
void loop() {
153+
154+
/*
155+
Wait a bit.
156+
We don't output every sample, because our baud rate
157+
won't support that much I/O.
158+
*/
159+
delay(20);
160+
161+
// write the latest sample to Serial.
162+
pulseSensor.outputSample();
163+
164+
/*
165+
If a beat has happened on a given PulseSensor
166+
since we last checked, write the per-beat information
167+
about that PulseSensor to Serial.
168+
*/
169+
for (int i = 0; i < PULSE_SENSOR_COUNT; ++i) {
170+
if (pulseSensor.sawStartOfBeat(i)) {
171+
pulseSensor.outputBeat(i);
172+
173+
lastBeatSampleNumber[i] = pulseSensor.getLastBeatTime(i);
174+
if(i == 1){
175+
PTT = lastBeatSampleNumber[1] - lastBeatSampleNumber[0];
176+
pulseSensor.outputToSerial('|',PTT);
177+
}
178+
}
179+
}
180+
181+
}

keywords.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,10 @@ setSerial KEYWORD2
2727
setOutputType KEYWORD2
2828
sawStartOfBeat KEYWORD2
2929
setThreshold KEYWORD2
30+
getLastBeatTime KEYWORD2
31+
outputToSerial KEYWORD2
32+
getPulseAmplitude KEYWORD2
33+
getLastBeatTime KEYWORD2
3034

3135
#######################################
3236
# Instances (KEYWORD2)

library.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name=PulseSensor Playground
2-
version=1.3.3
2+
version=1.4.3
33
author=Joel Murphy, Yury Gitman, Brad Needham
44
maintainer=Joel Murphy, Yury Gitman
55
sentence=Support at PulseSensor.com

resources/PulseSensor Playground Tools.md

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,11 +66,11 @@ Returns the sample number when the latest beat was found. The sample number is b
6666

6767
---
6868
### sawStartOfBeat()
69-
Returns true if a new heartbeat pulse has been detected. Type = boolean.
69+
Returns `true` if a new heartbeat pulse has been detected. Type = boolean.
7070

7171
---
7272
### isInsideBeat()
73-
Returns true for the time when a measured heartbeat wave is above the value set in setThreshold() above. Type = boolean.
73+
Returns `true` for the time when a measured heartbeat wave is above the value set in setThreshold() above. Type = boolean.
7474

7575
---
7676
### outputSample()
@@ -80,6 +80,10 @@ Output the latest sample over the Serial port. If your Sketch wants to plot samp
8080
### outputBeat()
8181
Output the latest BPM and IBI values over the Serial port.
8282

83+
---
84+
### outputToSerial(char, int)
85+
Output Data with a character prefix. Used exclusively with the Pulse Sensor Processing Visualizer.
86+
8387
---
8488
## Using Pulse Sensor Interrupts
8589

src/PulseSensorPlayground.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,10 @@ void PulseSensorPlayground::outputBeat(int sensorIndex) {
204204
SerialOutput.outputBeat(Sensors, SensorCount, sensorIndex);
205205
}
206206

207+
void PulseSensorPlayground::outputToSerial(char s, int d) {
208+
SerialOutput.outputToSerial(s,d);
209+
}
210+
207211
int PulseSensorPlayground::getPulseAmplitude(int sensorIndex) {
208212
if (sensorIndex != constrain(sensorIndex, 0, SensorCount)) {
209213
return; // out of range.

src/PulseSensorPlayground.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -350,6 +350,12 @@ class PulseSensorPlayground {
350350
*/
351351
void outputSample();
352352

353+
/*
354+
Serial print data with prefix.
355+
Used exclusively with the Pulse Sensor Processing sketch.
356+
*/
357+
void outputToSerial(char symbol, int data);
358+
353359
/*
354360
Returns the current amplitude of the pulse waveform.
355361
*/

src/utility/PulseSensor.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ class PulseSensor {
6969
// (internal to the library) Updtate the thresh variables.
7070
void setThreshold(int threshold);
7171

72+
7273
private:
7374
// Configuration
7475
int InputPin; // Analog input pin for PulseSensor.

src/utility/PulseSensorSerialOutput.h

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -71,9 +71,10 @@ class PulseSensorSerialOutput {
7171
*/
7272
void outputBeat(PulseSensor sensors[], int numberOfSensors, int sensorIndex);
7373

74-
//
75-
// // testing feedback
76-
// void printThreshSetting();
74+
/*
75+
Write the given data prefixed by the given symbol.
76+
*/
77+
void outputToSerial(char symbol, int data);
7778

7879
private:
7980
// If non-null, the output stream to print to. If null, don't print.
@@ -82,7 +83,5 @@ class PulseSensorSerialOutput {
8283
// The destination of data: PROCESSING_VISUALIZER or SERIAL_PLOTTER
8384
int OutputType;
8485

85-
// Write the given data prefixed by the given symbol.
86-
void outputToSerial(char symbol, int data);
8786
};
8887
#endif // PULSE_SENSOR_SERIAL_OUTPUT_H

0 commit comments

Comments
 (0)