|
| 1 | +/* |
| 2 | + Code to detect pulses from the PulseSensor, |
| 3 | + using an interrupt service routine. |
| 4 | +
|
| 5 | + Here is a link to the tutorial\ |
| 6 | + https://pulsesensor.com/pages/getting-advanced |
| 7 | +
|
| 8 | + Copyright World Famous Electronics LLC - see LICENSE |
| 9 | + Contributors: |
| 10 | + Joel Murphy, https://pulsesensor.com |
| 11 | + Yury Gitman, https://pulsesensor.com |
| 12 | + Bradford Needham, @bneedhamia, https://bluepapertech.com |
| 13 | +
|
| 14 | + Licensed under the MIT License, a copy of which |
| 15 | + should have been included with this software. |
| 16 | +
|
| 17 | + This software is not intended for medical use. |
| 18 | +*/ |
| 19 | + |
| 20 | +/* |
| 21 | + Include the DueTimer library. If you don't have it, use library manager to get it. |
| 22 | + You can also find it at https://github.com/ivanseidel/DueTimer |
| 23 | + If you use the Servo library, probably want to include that before this inlude. Just sayin'... |
| 24 | + This will grab the next available timer and call it sampleTimer for use throughout the code |
| 25 | +*/ |
| 26 | +#include <DueTimer.h> |
| 27 | +DueTimer sampleTimer = Timer.getAvailable(); |
| 28 | + |
| 29 | +/* |
| 30 | + Every Sketch that uses the PulseSensor Playground must |
| 31 | + define USE_ARDUINO_INTERRUPTS before including PulseSensorPlayground.h. |
| 32 | + Here, #define USE_ARDUINO_INTERRUPTS true tells the library to use |
| 33 | + interrupts to automatically read and process PulseSensor data. |
| 34 | +
|
| 35 | + See PulseSensor_BPM_Alternative.ino for an example of not using interrupts. |
| 36 | +*/ |
| 37 | +#define USE_ARDUINO_INTERRUPTS true |
| 38 | +#include <PulseSensorPlayground.h> |
| 39 | + |
| 40 | +/* |
| 41 | + * Declare the interrupt service routine |
| 42 | + * This will be used in setup as the interrupt callback in attachInterrupt |
| 43 | + */ |
| 44 | +void sampleTimer_ISR(){ |
| 45 | + PulseSensorPlayground::OurThis->onSampleTime(); |
| 46 | +} |
| 47 | + |
| 48 | +/* |
| 49 | + The format of our output. |
| 50 | +
|
| 51 | + Set this to PROCESSING_VISUALIZER if you're going to run |
| 52 | + the Processing Visualizer Sketch. |
| 53 | + See https://github.com/WorldFamousElectronics/PulseSensor_Amped_Processing_Visualizer |
| 54 | +
|
| 55 | + Set this to SERIAL_PLOTTER if you're going to run |
| 56 | + the Arduino IDE's Serial Plotter. |
| 57 | +*/ |
| 58 | +const int OUTPUT_TYPE = SERIAL_PLOTTER; |
| 59 | + |
| 60 | +/* |
| 61 | + Pinout: |
| 62 | + PULSE_INPUT = Analog Input. Connected to the pulse sensor |
| 63 | + purple (signal) wire. |
| 64 | + PULSE_BLINK = digital Output. Connected to an LED (and 1K series resistor) |
| 65 | + that will flash on each detected pulse. |
| 66 | + PULSE_FADE = digital Output. PWM pin onnected to an LED (and 1K series resistor) |
| 67 | + that will smoothly fade with each pulse. |
| 68 | + NOTE: PULSE_FADE must be a pin that supports PWM. Do not use |
| 69 | + pin 9 or 10, because those pins' PWM interferes with the sample timer. |
| 70 | + THRESHOLD should be set higher than the PulseSensor signal idles |
| 71 | + at when there is nothing touching it. The expected idle value |
| 72 | + should be 512, which is 1/2 of the ADC range. To check the idle value |
| 73 | + open a serial monitor and make note of the PulseSensor signal values |
| 74 | + with nothing touching the sensor. THRESHOLD should be a value higher |
| 75 | + than the range of idle noise by 25 to 50 or so. When the library |
| 76 | + is finding heartbeats, the value is adjusted based on the pulse signal |
| 77 | + waveform. THRESHOLD sets the default when there is no pulse present. |
| 78 | + Adjust as neccesary. |
| 79 | +*/ |
| 80 | +const int PULSE_INPUT = A0; |
| 81 | +const int PULSE_BLINK = LED_BUILTIN; |
| 82 | +const int PULSE_FADE = 5; |
| 83 | +const int THRESHOLD = 550; // Adjust this number to avoid noise when idle |
| 84 | + |
| 85 | +/* |
| 86 | + All the PulseSensor Playground functions. |
| 87 | +*/ |
| 88 | +PulseSensorPlayground pulseSensor; |
| 89 | + |
| 90 | +void setup() { |
| 91 | + /* |
| 92 | + Use 115200 baud because that's what the Processing Sketch expects to read, |
| 93 | + and because that speed provides about 11 bytes per millisecond. |
| 94 | +
|
| 95 | + If we used a slower baud rate, we'd likely write bytes faster than |
| 96 | + they can be transmitted, which would mess up the timing |
| 97 | + of readSensor() calls, which would make the pulse measurement |
| 98 | + not work properly. |
| 99 | + */ |
| 100 | + Serial.begin(115200); |
| 101 | + |
| 102 | + // Configure the PulseSensor manager. |
| 103 | + |
| 104 | + pulseSensor.analogInput(PULSE_INPUT); |
| 105 | + pulseSensor.blinkOnPulse(PULSE_BLINK); |
| 106 | + pulseSensor.fadeOnPulse(PULSE_FADE); |
| 107 | + |
| 108 | + pulseSensor.setSerial(Serial); |
| 109 | + pulseSensor.setOutputType(OUTPUT_TYPE); |
| 110 | + pulseSensor.setThreshold(THRESHOLD); |
| 111 | + |
| 112 | + // Now that everything is ready, start reading the PulseSensor signal. |
| 113 | + if (!pulseSensor.begin()) { |
| 114 | + /* |
| 115 | + PulseSensor initialization failed, |
| 116 | + likely because our particular Arduino platform interrupts |
| 117 | + aren't supported yet. |
| 118 | +
|
| 119 | + If your Sketch hangs here, try PulseSensor_BPM_Alternative.ino, |
| 120 | + which doesn't use interrupts. |
| 121 | + */ |
| 122 | + for(;;) { |
| 123 | + // Flash the led to show things didn't work. |
| 124 | + digitalWrite(PULSE_BLINK, LOW); |
| 125 | + delay(50); Serial.println('!'); |
| 126 | + digitalWrite(PULSE_BLINK, HIGH); |
| 127 | + delay(50); |
| 128 | + } |
| 129 | + } |
| 130 | + |
| 131 | + /* This starts the sample timer interrupt. Do this last in the setup() routine. |
| 132 | + * We are using Timer6 to avoid potential conflict with the servo library |
| 133 | + * This timer selection could be better... |
| 134 | + * Use pause() and resume() to start and stop sampling on the fly |
| 135 | + * Check Resources folder in the library for more tools |
| 136 | + */ |
| 137 | + sampleTimer.attachInterrupt(sampleTimer_ISR); |
| 138 | + sampleTimer.start(2000); // Calls every period microseconds |
| 139 | + |
| 140 | +} |
| 141 | + |
| 142 | +void loop() { |
| 143 | + /* |
| 144 | + Wait a bit. |
| 145 | + We don't output every sample, because our baud rate |
| 146 | + won't support that much I/O. |
| 147 | + */ |
| 148 | + delay(20); |
| 149 | + |
| 150 | + // write the latest sample to Serial. |
| 151 | + pulseSensor.outputSample(); |
| 152 | + |
| 153 | + /* |
| 154 | + If a beat has happened since we last checked, |
| 155 | + write the per-beat information to Serial. |
| 156 | + */ |
| 157 | + if (pulseSensor.sawStartOfBeat()) { |
| 158 | + pulseSensor.outputBeat(); |
| 159 | + } |
| 160 | +} |
0 commit comments