Coder Social home page Coder Social logo

Comments (5)

biomurph avatar biomurph commented on September 26, 2024

@Teeblackgold97 I am not sure what you mean...

If you start with the PulseSensor_BPM_Alternative.ino sketch. That will return the latest BPM value whenever you call getBeatsPerMinute() function.

LMK if you have issues.

from pulsesensorplayground.

Teeblackgold97 avatar Teeblackgold97 commented on September 26, 2024

Hi, sorry for the late reply.
May I know where to put getBeatsPerMinute() function in the PulseSensor_BPM_Alternative.ino sketch? If can, how can I declare an integer for it? I want to do a calculation for it, not just serial print the value. Basically, I want to use this method: int myBPM = pulseSensor.getBeatsPerMinute(); which is from Getting_BPM_to_monitor. Since Nucleo can't run Getting_BPM_to_monitor.ino sketch, is there any other way to do it?
Thank you for your attention, much appreciated.

from pulsesensorplayground.

biomurph avatar biomurph commented on September 26, 2024

You pretty much have the right idea.
Declare a variable int myBPM; or something.
Maybe also create a flag to know if your threshold has be reached boolean BPMflag;
Then, in the sketch you will have to check the BPM produced by the library against some threshold (I think that's what you're doing?)
Inside the loop, there is some code that runs when PulseSensor Playground finds the beginning of a heartbeat. That's where you can do your comparison.

if (pulseSensor.sawStartOfBeat()) {
// pulseSensor.outputBeat();
myBPM = pulseSensor.getBeatsPerMinute();
if(myBPM < threshold){
BPMflag = true;
}
}

That example is just for demonstration. I have no idea what you are actually trying to do. If you post a link to your code, then further troubleshooting could happen, but the thing you want to do is on the easy side of hard.

from pulsesensorplayground.

Teeblackgold97 avatar Teeblackgold97 commented on September 26, 2024

Hi, I finally bought a brand new Arduino Uno board to save some of the hassle I had encountered.
I posted the code below to show you what am I trying to do:
`#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <stdlib.h>
#include <math.h>

#define ADC_ref 5
#define zero_x 1.569
#define zero_y 1.569
#define zero_z 1.569
#define sensitivity_x 0.3
#define sensitivity_y 0.3
#define sensitivity_z 0.3
#define USE_ARDUINO_INTERRUPTS true

#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels

#define OLED_RESET 4 // Reset pin # (or -1 if sharing Arduino reset pin)
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);

unsigned int value_x;
unsigned int value_y;
unsigned int value_z;

float xv;
float yv;
float zv;

float angle_x;
float angle_y;
float angle_z;

int led = 7; //indicating bluetooth connection

float temp;
int hum;
String tempC;
int error;
int pulsePin = 0; // Pulse Sensor purple wire connected to analog pin 0
int blinkPin = 13; // pin to blink led at each beat
int fadePin = 5;
int fadeRate = 0;
// Volatile Variables, used in the interrupt service routine!
volatile int BPM; // int that holds raw Analog in 0. updated every 2mS
volatile int Signal; // holds the incoming raw data
volatile int IBI = 600; // int that holds the time interval between beats! Must be seeded!
volatile boolean Pulse = false; // "True" when heartbeat is detected. "False" when not a "live beat".
volatile boolean QS = false; // becomes true when Arduino finds a beat.
volatile int rate[10]; // array to hold last ten IBI values
volatile unsigned long sampleCounter = 0; // used to determine pulse timing
volatile unsigned long lastBeatTime = 0; // used to find IBI
volatile int P =512; // used to find peak in pulse wave, seeded
volatile int T = 512; // used to find trough in pulse wave, seeded
volatile int thresh = 525; // used to find instant moment of heart beat, seeded
volatile int amp = 100; // used to hold amplitude of pulse waveform, seeded
volatile boolean firstBeat = true; // used to seed rate array so we startup with reasonable BPM
volatile boolean secondBeat = false; // used to seed rate array so we startup with reasonable BPM

void setup()
{
pinMode( led ,OUTPUT);

Serial.begin(115200); //or use default 115200.
display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
display.display();
delay(2000);

display.clearDisplay(); // clearing the display
display.setTextSize(1);
display.setCursor(0,0);
display.setTextColor(WHITE);

interruptSetup();
}

void loop()
{
value_x = analogRead(A1);
value_y = analogRead(A2);
value_z = analogRead(A3);

xv=(value_x/1024.0ADC_ref-zero_x)/sensitivity_x;
yv=(value_y/1024.0
ADC_ref-zero_y)/sensitivity_y;
zv=(value_z/1024.0*ADC_ref-zero_z)/sensitivity_z;

angle_x =atan2(-yv,-zv)*RAD_TO_DEG+180;
angle_y =atan2(-xv,-zv)*RAD_TO_DEG+180;
angle_z =atan2(-yv,-xv)*RAD_TO_DEG+180;

display.setCursor(0, 0);
display.print("BPM: ");
display.println(BPM);
display.setCursor(0, 9);
display.print("X: ");
display.println(xv);
display.setCursor(0, 18);
display.print("Y:");
display.println(yv);
display.setCursor(0, 27);
display.print("Z: ");
display.println(zv);
display.setCursor(0, 36);
display.print("Angle X: ");
display.println(angle_x);
display.setCursor(0, 45);
display.print("Angle Y: ");
display.println(angle_y);
display.setCursor(0, 54);
display.print("Angle Z: ");
display.println(angle_z);

display.display(); //you have to tell the display to...display
delay(100);
display.clearDisplay();
}

void interruptSetup()
{
TCCR2A = 0x02; // DISABLE PWM ON DIGITAL PINS 3 AND 11, AND GO INTO CTC MODE
TCCR2B = 0x06; // DON'T FORCE COMPARE, 256 PRESCALER
OCR2A = 0X7C; // SET THE TOP OF THE COUNT TO 124 FOR 500Hz SAMPLE RATE
TIMSK2 = 0x02; // ENABLE INTERRUPT ON MATCH BETWEEN TIMER2 AND OCR2A
sei(); // MAKE SURE GLOBAL INTERRUPTS ARE ENABLED
}
ISR(TIMER2_COMPA_vect)
{ // triggered when Timer2 counts to 124
cli(); // disable interrupts while we do this
Signal = analogRead(pulsePin); // read the Pulse Sensor
sampleCounter += 2; // keep track of the time in mS
int N = sampleCounter - lastBeatTime; // monitor the time since the last beat to avoid noise
// find the peak and trough of the pulse wave
if(Signal < thresh && N > (IBI/5)*3){ // avoid dichrotic noise by waiting 3/5 of last IBI
if (Signal < T){ // T is the trough
T = Signal; // keep track of lowest point in pulse wave
}
}
if(Signal > thresh && Signal > P){ // thresh condition helps avoid noise
P = Signal; // P is the peak
} // keep track of highest point in pulse wave
// NOW IT'S TIME TO LOOK FOR THE HEART BEAT
// signal surges up in value every time there is a pulse
if (N > 250){ // avoid high frequency noise
if ( (Signal > thresh) && (Pulse == false) && (N > (IBI/5)*3) ){
Pulse = true; // set the Pulse flag when there is a pulse
digitalWrite(blinkPin,HIGH); // turn on pin 13 LED
IBI = sampleCounter - lastBeatTime; // time between beats in mS
lastBeatTime = sampleCounter; // keep track of time for next pulse
if(secondBeat){ // if this is the second beat
secondBeat = false; // clear secondBeat flag
for(int i=0; i<=9; i++){ // seed the running total to get a realistic BPM at startup
rate[i] = IBI;
}
}
if(firstBeat){ // if it's the first time beat is found
firstBeat = false; // clear firstBeat flag
secondBeat = true; // set the second beat flag
sei(); // enable interrupts again
return; // IBI value is unreliable so discard it
}
word runningTotal = 0; // clear the runningTotal variable
for(int i=0; i<=8; i++){ // shift data in the rate array
rate[i] = rate[i+1]; // and drop the oldest IBI value
runningTotal += rate[i]; // add up the 9 oldest IBI values
}
rate[9] = IBI; // add the latest IBI to the rate array
runningTotal += rate[9]; // add the latest IBI to runningTotal
runningTotal /= 10; // average the last 10 IBI values
BPM = 60000/runningTotal; // how many beats can fit into a minute? that's BPM!
QS = true; // set Quantified Self flag
// QS FLAG IS NOT CLEARED INSIDE THIS ISR
}
}
if (Signal < thresh && Pulse == true){ // when the values are going down, the beat is over
digitalWrite(blinkPin,LOW); // turn off pin 13 LED
Pulse = false; // reset the Pulse flag so we can do it again
amp = P - T; // get amplitude of the pulse wave
thresh = amp/2 + T; // set thresh at 50% of the amplitude
P = thresh; // reset these for next time
T = thresh;
}
if (N > 2500){ // if 2.5 seconds go by without a beat
thresh = 512; // set thresh default
P = 512; // set P default
T = 512; // set T default
lastBeatTime = sampleCounter; // bring the lastBeatTime up to date
firstBeat = true; // set these to avoid noise
secondBeat = false; // when we get the heartbeat back
}
sei();
// enable interrupts when youre done!
}// end isr`
This is just a simple code, I have yet to develop it further. Basically I am designing a fatigue detection system, as you can see I'm not using getBeatsPerMinute() function because somehow I can't get any BPM value out of it. May I know what are the requirements for me to be able to activate the getBeatsPerMinute() function? The codes above can show BPM value but is not accurate nor consistent.

from pulsesensorplayground.

biomurph avatar biomurph commented on September 26, 2024

@Teeblackgold97
You seem to be using an older version of our code. Are you not able to use the PulseSensor Playground library with the OLED?
Does this code work?

from pulsesensorplayground.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.