Skip to content

Instantly share code, notes, and snippets.

@halldorel
Created January 27, 2018 20:35
Show Gist options
  • Save halldorel/42a666b444d4f361eda60e7e88b8802f to your computer and use it in GitHub Desktop.
Save halldorel/42a666b444d4f361eda60e7e88b8802f to your computer and use it in GitHub Desktop.
Heartbeat Arduino sketch with spam rejection 😅
/*
Code to detect pulses from the PulseSensor,
using an interrupt service routine.
Here is a link to the tutorial\
https://pulsesensor.com/pages/getting-advanced
Copyright World Famous Electronics LLC - see LICENSE
Contributors:
Joel Murphy, https://pulsesensor.com
Yury Gitman, https://pulsesensor.com
Bradford Needham, @bneedhamia, https://bluepapertech.com
Licensed under the MIT License, a copy of which
should have been included with this software.
This software is not intended for medical use.
*/
/*
Every Sketch that uses the PulseSensor Playground must
define USE_ARDUINO_INTERRUPTS before including PulseSensorPlayground.h.
Here, #define USE_ARDUINO_INTERRUPTS true tells the library to use
interrupts to automatically read and process PulseSensor data.
See ProcessEverySample.ino for an example of not using interrupts.
*/
#define USE_ARDUINO_INTERRUPTS true
#include <PulseSensorPlayground.h>
/*
The format of our output.
Set this to PROCESSING_VISUALIZER if you're going to run
the Processing Visualizer Sketch.
See https://github.com/WorldFamousElectronics/PulseSensor_Amped_Processing_Visualizer
Set this to SERIAL_PLOTTER if you're going to run
the Arduino IDE's Serial Plotter.
*/
const int OUTPUT_TYPE = SERIAL_PLOTTER;
typedef enum MODE {
IDLING,
BEATING
} unsigned short;
/*
Pinout:
PIN_INPUT = Analog Input. Connected to the pulse sensor
purple (signal) wire.
PIN_BLINK = digital Output. Connected to an LED (and 220 ohm resistor)
that will flash on each detected pulse.
PIN_FADE = digital Output. PWM pin onnected to an LED (and resistor)
that will smoothly fade with each pulse.
NOTE: PIN_FADE must be a pin that supports PWM. Do not use
pin 9 or 10, because those pins' PWM interferes with the sample timer.
*/
const int PIN_INPUT = A0;
const int PIN_BLINK = 13; // Pin 13 is the on-board LED
const int PIN_FADE = 5;
const int THRESHOLD = 550; // Adjust this number to avoid noise when idle
/*
All the PulseSensor Playground functions.
*/
PulseSensorPlayground pulseSensor;
void setup() {
/*
Use 115200 baud because that's what the Processing Sketch expects to read,
and because that speed provides about 11 bytes per millisecond.
If we used a slower baud rate, we'd likely write bytes faster than
they can be transmitted, which would mess up the timing
of readSensor() calls, which would make the pulse measurement
not work properly.
*/
Serial.begin(115200);
// Configure the PulseSensor manager.
pulseSensor.analogInput(PIN_INPUT);
pulseSensor.blinkOnPulse(PIN_BLINK);
pulseSensor.fadeOnPulse(PIN_FADE);
// pulseSensor.setSerial(Serial);
// pulseSensor.setOutputType(OUTPUT_TYPE);
pulseSensor.setThreshold(THRESHOLD);
// Now that everything is ready, start reading the PulseSensor signal.
if (!pulseSensor.begin()) {
/*
PulseSensor initialization failed,
likely because our particular Arduino platform interrupts
aren't supported yet.
If your Sketch hangs here, try ProcessEverySample.ino,
which doesn't use interrupts.
*/
for(;;) {
// Flash the led to show things didn't work.
digitalWrite(PIN_BLINK, LOW);
delay(50);
digitalWrite(PIN_BLINK, HIGH);
delay(50);
}
}
}
// HE & THHBLEDZAÐUR
const int TIMEOUT = 3000;
const int MAX_COUNTDOWN = 2;
unsigned long currentTime;
MODE currentMode = IDLING;
unsigned long lastBeatTime = 0;
unsigned long oldBeatTime = 0;
unsigned long spamRefsing = 0;
void loop() {
/*
Wait a bit.
We don't output every sample, because our baud rate
won't support that much I/O.
*/
delay(20);
currentTime = millis();
switch (currentMode) {
case IDLING:
// Woopdee doop LED mambo
break;
case BEATING:
// Check if we are timing out
if((currentTime - lastBeatTime) > TIMEOUT) {
currentMode = IDLING;
Serial.println("MODE: IDLING");
}
break;
}
/*
If a beat has happened since we last checked,
write the per-beat information to Serial.
*/
if (pulseSensor.sawStartOfBeat()) {
lastBeatTime = millis();
if((currentTime - oldBeatTime) < 320) {
Serial.println("SPAMMING DEVIL; IDLING NOW!");
spamRefsing = MAX_COUNTDOWN;
currentMode = IDLING;
}
if(currentMode == IDLING) {
if(spamRefsing > 0) {
spamRefsing--;
}
else {
currentMode = BEATING;
Serial.println("MODE: BEATING");
}
}
else {
Serial.println("HJARTASLAG");
}
oldBeatTime = lastBeatTime;
}
//(currentTime - pulseSensor.getLastBeatTime()) > TIMEOUT*1000;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment