I'm working with some students on code that triggers a "cheerful message" when it detects a high pulse.
We got it to display a pulse, and to set a trigger if we faked a high pulse (manually set variable), however it never triggers when it actually detects a high pulse.
#define USE_ARDUINO_INTERRUPTS true // Set-up low-level interrupts for most acurate BPM math.
#include <PulseSensorPlayground.h> // Includes the PulseSensorPlayground Library.
// Variables
const int PulseWire = 0; //PulseSensor PURPLE WIRE connected to ANALOG PIN 0
const int LED = LED_BUILTIN; //The on-board Arduino LED, close to PIN 13.
int Threshold = 550; //Determine which Signal to "count as a beat" and which to ignore.
int myBPM; //Variable for recording the Pulse Rate (BPM)
int Triggered = 0; //Variable for triggering the cheer-up event
unsigned long lastTimeCheckPulse = millis();
unsigned long delayBetweenCheckPulse = 5000;
unsigned long timeNow = millis();
int AdjustedBPM = 100;
PulseSensorPlayground pulseSensor; // Creates an instance of the PulseSensorPlayground object called "pulseSensor"
void setup() {
Serial.begin(9600); // For Serial Monitor
// Configure the PulseSensor object, by assigning our variables to it.
pulseSensor.analogInput(PulseWire);
pulseSensor.blinkOnPulse(LED); //auto-magically blink Arduino's LED with heartbeat.
pulseSensor.setThreshold(Threshold);
// Double-check the "pulseSensor" object was created and "began" seeing a signal.
if (pulseSensor.begin()) {
Serial.println("Sensor Is Alive!!"); //This prints one time at Arduino power-up, or on Arduino reset.
}
}
void loop() { //Start of void loop
timeNow = millis();
Serial.println(timeNow);
if(timeNow - lastTimeCheckPulse > delayBetweenCheckPulse){
CheckPulse(); //This runs all the code inside the "void CheckPulse" command below
lastTimeCheckPulse = timeNow;
} //END if
CheckTrigger();
if(Triggered == 1){
SendMessage();
} //End if
delay(1000);
} //End of Void Loop
void CheckPulse(){
Serial.println("Checking Pulse"); //Dubugging check point
if (pulseSensor.sawStartOfBeat()) { // Test to see if "a beat happened".
int myBPM = pulseSensor.getBeatsPerMinute(); // Calls function on our pulseSensor object that returns BPM as an "int".
// "myBPM" hold this BPM value now.
Serial.println("♥ I'm Alive ! "); // If test is "true", print a message "a heartbeat happened".
Serial.print("BPM: "); // Print phrase "BPM: "
Serial.println(myBPM); // Print the value inside of myBPM.
} //END if
Serial.println(myBPM);
// myBPM = 101; //Fake Pulse Sensor (This is where we manually set pulse, which then triggers event)
} //END void CheckPulse
void CheckTrigger(){
if (myBPM > AdjustedBPM){ //Check if BPM is over 100
Triggered = 1; //If it is, trigger cheer-up event
myBPM = 0;
} //END if
} //END void CheckTriggered
void SendMessage(){
Serial.println("Cheer Up!"); //If it is, send message.
Triggered = 0; // I added this line to reset the trigger, so it waits for the next trigger.
} //END void SendMessage
Any suggestions would be appreciated!
-Teacher Tom