|
| 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 | +} |
0 commit comments