MAX30102读取完16个红光和红外数据计算血氧心率值的代码
时间: 2024-05-15 11:14:30 浏览: 209
MAX30102.zip_MAX30102IIC读取_MAX30102心率_max30102_stm32 max30102_血氧
5星 · 资源好评率100%
以下是使用Arduino和MAX30102传感器读取血氧和心率值的示例代码:
```
#include <Wire.h>
#include "MAX30105.h"
#include "heartRate.h"
MAX30105 particleSensor;
const byte RATE_SIZE = 4; // Increase this for more averaging. 4 is good.
byte rates[RATE_SIZE]; // 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); // initialize serial communication at 115200 baud
Serial.println("Initializing...");
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();
particleSensor.setPulseAmplitudeRed(0x0A); // Turn Red LED to low to indicate sensor is running
particleSensor.setPulseAmplitudeGreen(0);
}
void loop()
{
long irValue = particleSensor.getIR(); // Reading IR value
if (checkForBeat(irValue) == true)
{
// We sensed a beat!
long delta = millis() - lastBeat;
lastBeat = millis();
beatsPerMinute = 60 / (delta / 1000.0);
if (beatsPerMinute < 255 && beatsPerMinute > 20)
{
rates[rateSpot++] = (byte)beatsPerMinute; // Store the value in the array
rateSpot %= RATE_SIZE; // Wrap variable
// Take average of readings
beatAvg = 0;
for (byte x = 0 ; x < RATE_SIZE ; x++)
beatAvg += rates[x];
beatAvg /= RATE_SIZE;
}
}
Serial.print("Heart rate: ");
Serial.print(beatAvg);
Serial.print(" bpm");
Serial.print("\tSpO2: ");
Serial.print(particleSensor.getSpO2());
Serial.println();
delay(20);
}
```
这个代码使用了MAX30105库和心率库来计算心率和血氧饱和度。在主循环中,首先读取红外线值并检查是否有心跳。如果检测到心跳,则计算心率并存储在一个数组中。然后,计算数组中所有值的平均值,并输出到串口。最后,读取红外线和红光值以计算血氧饱和度,并输出到串口。整个循环延迟20毫秒以降低CPU负载。
阅读全文