|
| 1 | +/* |
| 2 | + Code to detect pulses from the PulseSensor, |
| 3 | + using an interrupt service routine. |
| 4 | +
|
| 5 | +>>>> This example targest the Arduino UNO R4. |
| 6 | +>>>> It has been tested on the Minima and the WiFi board variants. |
| 7 | +
|
| 8 | + Here is a link to the tutorial |
| 9 | + https://pulsesensor.com/pages/getting-advanced |
| 10 | +
|
| 11 | + Copyright World Famous Electronics LLC - see LICENSE |
| 12 | + Contributors: |
| 13 | + Joel Murphy, https://pulsesensor.com |
| 14 | + Yury Gitman, https://pulsesensor.com |
| 15 | + Bradford Needham, @bneedhamia, https://bluepapertech.com |
| 16 | +
|
| 17 | + Licensed under the MIT License, a copy of which |
| 18 | + should have been included with this software. |
| 19 | +
|
| 20 | + This software is not intended for medical use. |
| 21 | +*/ |
| 22 | + |
| 23 | +/* |
| 24 | + We use the FspTimer to setup a timer interrupt for sample acquisition |
| 25 | + FspTimer is part of the hardware core files for this chip |
| 26 | +*/ |
| 27 | +#include "FspTimer.h" |
| 28 | +FspTimer sampleTimer; |
| 29 | + |
| 30 | + |
| 31 | +/* |
| 32 | + Every Sketch that uses the PulseSensor Playground must |
| 33 | + define USE_ARDUINO_INTERRUPTS before including PulseSensorPlayground.h. |
| 34 | + Here, #define USE_ARDUINO_INTERRUPTS true tells the library to use |
| 35 | + interrupts to automatically read and process PulseSensor data. |
| 36 | +
|
| 37 | + See PulseSensorBPM_Alternative.ino for an example of not using interrupts. |
| 38 | +*/ |
| 39 | +#define USE_ARDUINO_INTERRUPTS true |
| 40 | +#include <PulseSensorPlayground.h> |
| 41 | + |
| 42 | +/* |
| 43 | + This is the timer interrupt service routine where we acquire and process samples |
| 44 | +*/ |
| 45 | +void sampleTimerISR(timer_callback_args_t __attribute((unused)) *p_args){ |
| 46 | + PulseSensorPlayground::OurThis->onSampleTime(); |
| 47 | +} |
| 48 | + |
| 49 | +/* |
| 50 | + The format of our output. |
| 51 | +
|
| 52 | + Set this to PROCESSING_VISUALIZER if you're going to run |
| 53 | + the Processing Visualizer Sketch. |
| 54 | + See https://github.com/WorldFamousElectronics/PulseSensor_Amped_Processing_Visualizer |
| 55 | +
|
| 56 | + Set this to SERIAL_PLOTTER if you're going to run |
| 57 | + the Arduino IDE's Serial Plotter. |
| 58 | +*/ |
| 59 | +const int OUTPUT_TYPE = SERIAL_PLOTTER; |
| 60 | + |
| 61 | +/* |
| 62 | + Pinout: |
| 63 | + PULSE_INPUT = Analog Input. Connected to the pulse sensor |
| 64 | + purple (signal) wire. |
| 65 | + PULSE_BLINK = digital Output. Connected to an LED (and 1K series resistor) |
| 66 | + that will flash on each detected pulse. |
| 67 | + PULSE_FADE = digital Output. PWM pin onnected to an LED (and 1K series resistor) |
| 68 | + that will smoothly fade with each pulse. |
| 69 | + NOTE: PULSE_FADE must be a pin that supports PWM. Do not use |
| 70 | + pin 9 or 10, because those pins' PWM interferes with the sample timer. |
| 71 | + THRESHOLD should be set higher than the PulseSensor signal idles |
| 72 | + at when there is nothing touching it. The expected idle value |
| 73 | + should be 512, which is 1/2 of the ADC range. To check the idle value |
| 74 | + open a serial monitor and make note of the PulseSensor signal values |
| 75 | + with nothing touching the sensor. THRESHOLD should be a value higher |
| 76 | + than the range of idle noise by 25 to 50 or so. When the library |
| 77 | + is finding heartbeats, the value is adjusted based on the pulse signal |
| 78 | + waveform. THRESHOLD sets the default when there is no pulse present. |
| 79 | + Adjust as neccesary. |
| 80 | +*/ |
| 81 | +const int PULSE_INPUT = A0; |
| 82 | +const int PULSE_BLINK = LED_BUILTIN; |
| 83 | +const int PULSE_FADE = 5; |
| 84 | +const int THRESHOLD = 550; // Adjust this number to avoid noise when idle |
| 85 | + |
| 86 | +/* |
| 87 | + All the PulseSensor Playground functions. |
| 88 | +*/ |
| 89 | +PulseSensorPlayground pulseSensor; |
| 90 | + |
| 91 | +void setup() { |
| 92 | + /* |
| 93 | + Use 115200 baud because that's what the Processing Sketch expects to read, |
| 94 | + and because that speed provides about 11 bytes per millisecond. |
| 95 | +
|
| 96 | + If we used a slower baud rate, we'd likely write bytes faster than |
| 97 | + they can be transmitted, which would mess up the timing |
| 98 | + of readSensor() calls, which would make the pulse measurement |
| 99 | + not work properly. |
| 100 | + */ |
| 101 | + Serial.begin(115200); |
| 102 | + |
| 103 | + // Configure the PulseSensor manager. |
| 104 | + |
| 105 | + pulseSensor.analogInput(PULSE_INPUT); |
| 106 | + pulseSensor.blinkOnPulse(PULSE_BLINK); |
| 107 | + pulseSensor.fadeOnPulse(PULSE_FADE); |
| 108 | + |
| 109 | + pulseSensor.setSerial(Serial); |
| 110 | + pulseSensor.setOutputType(OUTPUT_TYPE); |
| 111 | + pulseSensor.setThreshold(THRESHOLD); |
| 112 | + |
| 113 | + // Now that everything is ready, start reading the PulseSensor signal. |
| 114 | + if (!pulseSensor.begin()) { |
| 115 | + /* |
| 116 | + PulseSensor initialization failed, |
| 117 | + likely because our particular Arduino platform interrupts |
| 118 | + aren't supported yet. |
| 119 | +
|
| 120 | + If your Sketch hangs here, try PulseSensor_BPM_Alternative.ino, |
| 121 | + which doesn't use interrupts. |
| 122 | + */ |
| 123 | + for(;;) { |
| 124 | + // Flash the led to show things didn't work. |
| 125 | + digitalWrite(PULSE_BLINK, LOW); |
| 126 | + delay(50); Serial.println('!'); |
| 127 | + digitalWrite(PULSE_BLINK, HIGH); |
| 128 | + delay(50); |
| 129 | + } |
| 130 | + } |
| 131 | + |
| 132 | +/* |
| 133 | + We have to get control of a timer on the R4. First, we try and see if there are any free timers available. |
| 134 | + If there are no free timers available, we will just take control of one from some other purpose. |
| 135 | + We shouldn't have to force things, but if you use alot of timers, beware of this force use code! |
| 136 | +*/ |
| 137 | + uint8_t timer_type = GPT_TIMER; |
| 138 | + int8_t tindex = FspTimer::get_available_timer(timer_type); |
| 139 | + if(tindex == 0){ |
| 140 | + FspTimer::force_use_of_pwm_reserved_timer(); |
| 141 | + tindex = FspTimer::get_available_timer(timer_type); |
| 142 | + } |
| 143 | + |
| 144 | +/* |
| 145 | + Begin sets up the timer that we just got control of as a periodic timer with 500Hz frequency. |
| 146 | + It also passes the interrupt service routine that we made above. |
| 147 | +*/ |
| 148 | + sampleTimer.begin(TIMER_MODE_PERIODIC, timer_type, tindex, SAMPLE_RATE_500HZ, 0.0f, sampleTimerISR); |
| 149 | + sampleTimer.setup_overflow_irq(); |
| 150 | + sampleTimer.open(); |
| 151 | + sampleTimer.start(); |
| 152 | +} |
| 153 | + |
| 154 | +void loop() { |
| 155 | + /* |
| 156 | + Wait a bit. |
| 157 | + We don't output every sample, because our baud rate |
| 158 | + won't support that much I/O. |
| 159 | + */ |
| 160 | + delay(20); |
| 161 | + |
| 162 | + // write the latest sample to Serial. |
| 163 | + pulseSensor.outputSample(); |
| 164 | + |
| 165 | + /* |
| 166 | + If a beat has happened since we last checked, |
| 167 | + write the per-beat information to Serial. |
| 168 | + */ |
| 169 | + if (pulseSensor.sawStartOfBeat()) { |
| 170 | + pulseSensor.outputBeat(); |
| 171 | + } |
| 172 | + |
| 173 | +} |
| 174 | + |
0 commit comments