Go Down

Topic: Is it possible to stop receiving BPM after a while?  (Read 510 times) previous topic - next topic

RooneyH

Hello everyone! I'm doing a school project and i need your help with the code. I'm using Pulse Sensor in my project to measure heartbeat (BPM- beats per minute) and i'm using the examples code from its website: https://pulsesensor.com/. I did change the code a little bit for my purpose.
My idea is that, i want a sensor to measure BPM for about 10 seconds and stop getting the values, and then i'll get all that BPM numbers as a array, and find the most frequent values in array and display it to LCD beacuse i want to get the stable BPM. This is the first time i'm doing this so i dont know what to do next. This is my code: 
Code: [Select]


/*  Getting_BPM_to_Monitor prints the BPM to the Serial Monitor, using the least lines of code and PulseSensor Library.
 *  Tutorial Webpage: https://pulsesensor.com/pages/getting-advanced
 *
--------Use This Sketch To------------------------------------------
1) Displays user's live and changing BPM, Beats Per Minute, in Arduino's native Serial Monitor.
2) Print: "♥  A HeartBeat Happened !" when a beat is detected, live.
2) Learn about using a PulseSensor Library "Object".
4) Blinks LED on PIN 13 with user's Heartbeat.
--------------------------------------------------------------------*/

#define USE_ARDUINO_INTERRUPTS true    // Set-up low-level interrupts for most acurate BPM math.
#include <PulseSensorPlayground.h>     // Includes the PulseSensorPlayground Library.   
#include <LiquidCrystal.h>
//  Variables
const int PulseWire = 0;       // PulseSensor PURPLE WIRE connected to ANALOG PIN 0
const int LED13 = 13;          // The on-board Arduino LED, close to PIN 13.
int Threshold = 550;           // Determine which Signal to "count as a beat" and which to ignore.
                               // Use the "Gettting Started Project" to fine-tune Threshold Value beyond default setting.
                               // Otherwise leave the default "550" value.

LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
PulseSensorPlayground pulseSensor;  // Creates an instance of the PulseSensorPlayground object called "pulseSensor"
String choice;
void setup() {   
 
  Serial.begin(9600);          // For Serial Monitor
  // Configure the PulseSensor object, by assigning our variables to it.
  pulseSensor.analogInput(PulseWire);   
  pulseSensor.blinkOnPulse(LED13);       //auto-magically blink Arduino's LED with heartbeat.
  pulseSensor.setThreshold(Threshold);   
  pulseSensor.setSerial(Serial);
  // Double-check the "pulseSensor" object was created and "began" seeing a signal.
   if (pulseSensor.begin()) {
    lcd.setCursor(0,0);
    lcd.println("Loading...      ");  //LCD Starting Display 
    delay (3000);
    lcd.clear();
  }
}


void loop() {

 int myBPM = pulseSensor.getBeatsPerMinute();  // Calls function on our pulseSensor object that returns BPM as an "int".
 int Count=0;                                             // "myBPM" hold this BPM value now.
 String StrBPM = "BPM: ";
 String Space = "          ";
 pulseSensor.outputSample();
if (pulseSensor.sawStartOfBeat()) {            // Constantly test to see if "a beat happened".
  pulseSensor.outputBeat();
 //Serial.print("BPM:");                        // Print phrase "BPM: "
 //Serial.println(myBPM);                  // Print the value inside of myBPM.
 String StrPrint=StrBPM+myBPM+Space;
 lcd.setCursor(0,0);
 lcd.print(StrPrint);
}
  delay(20);                    // considered best practice in a simple sketch. 100


}
 

 

Anyone please help me! I really appreciate it!

wvmarle

Then measure for 10 seconds and stop:

Code: [Select]


bool measuring = false;
unsigned long startMeasuring;

void loop() {
  if (measuring == false) {

    // Start measuring.
    measuring = true;
    startMeasuring = millis();
  }

  if (millis() - startMeasuring < 10000) {
    measureBPM();
  }
  else {
    processBPM();

    // To do another measurement, trigger the first if block again:
    measuring = false;

  }
}
Quality of answers is related to the quality of questions. Good questions will get good answers. Useless answers are a sign of a poor question.

RooneyH

Then measure for 10 seconds and stop:

Code: [Select]


bool measuring = false;
unsigned long startMeasuring;

void loop() {
  if (measuring == false) {

    // Start measuring.
    measuring = true;
    startMeasuring = millis();
  }

  if (millis() - startMeasuring < 10000) {
    measureBPM();
  }
  else {
    processBPM();

    // To do another measurement, trigger the first if block again:
    measuring = false;

  }
}

I'll try it and tell you soon

RooneyH


wvmarle

Glad it works.

Hope you also understand what's going on, as it showcases two basic techniques that come back time and again: millis() based non-blocking timing, and a very basic finite state machine having just two states: measuring and not measuring.
Quality of answers is related to the quality of questions. Good questions will get good answers. Useless answers are a sign of a poor question.

Go Up