max30102心率显示算法代码
时间: 2023-05-26 15:05:50 浏览: 62
我作为AI语言模型无法生成该代码,但是以下为参考可能会有帮助:
#include <Wire.h>
#include "MAX30105.h"
#include "heartRate.h"
#define REPORTING_PERIOD_MS 1000
MAX30105 particleSensor;
static const uint8_t PIN_WIRE_SDA = 18;
static const uint8_t PIN_WIRE_SCL = 19;
const byte rateSize = 4; //Increase this for more averaging. 4 is good.
byte rates[rateSize]; //Array of heart rates
byte rateSpot = 0;
long lastBeat = 0; //Time at which the last beat occurred
float beatsPerMinute;
int beatAvg;
void setup()
{
Serial.begin(115200);
pinMode(LED_BUILTIN, OUTPUT); //Use the built-in LED as an indicator light
Wire.begin(PIN_WIRE_SDA, PIN_WIRE_SCL);
if (!particleSensor.begin(Wire, I2C_SPEED_FAST)) //Use default I2C port, 400kHz speed
{
Serial.println("MAX30105 was not found. Please check wiring/power. ");
while (1);
}
particleSensor.setup(0x02); //Configure sensor with these settings
particleSensor.setPulseAmplitudeRed(0x0A); //Turn Red LED to low to indicate sensor is running
particleSensor.setPulseAmplitudeGreen(0); //Turn off Green LED
Serial.println("You may now put your finger on the sensor to measure heart rate and SpO2 level.");
}
void loop()
{
// Make sure sensor is getting past the threshold point by waiting until the first valley is found
while (particleSensor.getIR() < 50000) {
particleSensor.nextSample(); //We're just waiting for valid data
}
while (1) {
//Poll the sensor for new data
particleSensor.nextSample();
//Calculate the getHeartRate
int32_t irValue = particleSensor.getIR();
if (checkForBeat(irValue) == true) //If a heart beat is detected
{
digitalWrite(LED_BUILTIN, HIGH); //Toggle LED on/off
beatsPerMinute = 60 / (millis() - lastBeat) * 1000;
lastBeat = millis();
if (beatsPerMinute < 255 && beatsPerMinute > 20)
{
rates[rateSpot++] = (byte)beatsPerMinute; //Store this reading in the array
rateSpot %= rateSize; //Wrap variable
//Take average of readings
beatAvg = 0;
for (byte x = 0 ; x < rateSize ; x++)
beatAvg += rates[x];
beatAvg /= rateSize;
}
}
if (millis() - lastBeat > 2000) //Maintain the LED on even if there is no beat detected for 2 seconds
{
beatAvg = 0;
digitalWrite(LED_BUILTIN, LOW); //Turn LED off
}
if (millis() > REPORTING_PERIOD_MS)
{
Serial.print("Heart rate:");
Serial.print(beatAvg);
Serial.print("bpm / SpO2:");
Serial.print(particleSensor.getSpO2(), 2);
Serial.println("%");
Serial.println("--------------------------------");
//Reset report timer
REPORTING_PERIOD_MS = millis();
}
}
}
阅读全文