Advertisement
Guest User

Untitled

a guest
Jan 22nd, 2023
427
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Arduino 2.72 KB | Software | 0 0
  1. /*
  2.   Optical Heart Rate Detection (PBA Algorithm) using the MAX30105 Breakout
  3.   By: Nathan Seidle @ SparkFun Electronics
  4.   Date: October 2nd, 2016
  5.   https://github.com/sparkfun/MAX30105_Breakout
  6.  
  7.   This is a demo to show the reading of heart rate or beats per minute (BPM) using
  8.   a Penpheral Beat Amplitude (PBA) algorithm.
  9.  
  10.   It is best to attach the sensor to your finger using a rubber band or other tightening
  11.   device. Humans are generally bad at applying constant pressure to a thing. When you
  12.   press your finger against the sensor it varies enough to cause the blood in your
  13.   finger to flow differently which causes the sensor readings to go wonky.
  14.  
  15.   Hardware Connections (Breakoutboard to Arduino):
  16.   -5V = 5V (3.3V is allowed)
  17.   -GND = GND
  18.   -SDA = A4 (or SDA)
  19.   -SCL = A5 (or SCL)
  20.   -INT = Not connected
  21.  
  22.   The MAX30105 Breakout can handle 5V or 3.3V I2C logic. We recommend powering the board with 5V
  23.   but it will also run at 3.3V.
  24. */
  25.  
  26. #include <Wire.h>
  27. #include "MAX30105.h"
  28.  
  29. #include "heartRate.h"
  30.  
  31. MAX30105 particleSensor;
  32.  
  33. const byte RATE_SIZE = 4; //Increase this for more averaging. 4 is good.
  34. byte rates[RATE_SIZE]; //Array of heart rates
  35. byte rateSpot = 0;
  36. long lastBeat = 0; //Time at which the last beat occurred
  37.  
  38. float beatsPerMinute;
  39. int beatAvg;
  40.  
  41. void setup()
  42. {
  43.   Serial.begin(115200);
  44.   Serial.println("Initializing...");
  45.  
  46.   // Initialize sensor
  47.   if (!particleSensor.begin(Wire, I2C_SPEED_FAST)) //Use default I2C port, 400kHz speed
  48.   {
  49.     Serial.println("MAX30105 was not found. Please check wiring/power. ");
  50.     while (1);
  51.   }
  52.   Serial.println("Place your index finger on the sensor with steady pressure.");
  53.  
  54.   particleSensor.setup(); //Configure sensor with default settings
  55.   particleSensor.setPulseAmplitudeRed(0x0A); //Turn Red LED to low to indicate sensor is running
  56.   particleSensor.setPulseAmplitudeGreen(0); //Turn off Green LED
  57. }
  58.  
  59. void loop()
  60. {
  61.   long irValue = particleSensor.getIR();
  62.  
  63.   if (checkForBeat(irValue) == true)
  64.   {
  65.     //We sensed a beat!
  66.     long delta = millis() - lastBeat;
  67.     lastBeat = millis();
  68.  
  69.     beatsPerMinute = 60 / (delta / 1000.0);
  70.  
  71.     if (beatsPerMinute < 255 && beatsPerMinute > 20)
  72.     {
  73.       rates[rateSpot++] = (byte)beatsPerMinute; //Store this reading in the array
  74.       rateSpot %= RATE_SIZE; //Wrap variable
  75.  
  76.       //Take average of readings
  77.       beatAvg = 0;
  78.       for (byte x = 0 ; x < RATE_SIZE ; x++)
  79.         beatAvg += rates[x];
  80.       beatAvg /= RATE_SIZE;
  81.     }
  82.   }
  83.  
  84.   //Serial.print(",");
  85.   Serial.print(irValue);
  86.   Serial.print(",");
  87.   Serial.print(beatsPerMinute);
  88.   Serial.print(",");
  89.   Serial.print(beatAvg);
  90.  
  91.   if (irValue < 50000)
  92.     Serial.print("false");
  93.  
  94.   Serial.println();
  95.  
  96. }
  97.  
  98.  
  99.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement