写一个max30102的使用代码
时间: 2023-12-29 08:02:31 浏览: 74
以下是一个基础的max30102使用代码,您可以根据自己的需求进行修改:
```
#include <Wire.h>
#include "MAX30105.h"
#include "heartRate.h"
#include <SoftwareSerial.h>
SoftwareSerial mySerial(2, 3); // 软串口引脚2为RX, 3为TX
MAX30105 particleSensor;
const byte RATE_SIZE = 4; // 心率平均滤波器的大小
byte rates[RATE_SIZE];
byte rateDropCounter = 0;
float lastReading=0;
long lastBeat = 0;
float beatsPerMinute;
int beatAvg;
void setup()
{
mySerial.begin(9600); // 初始化软串口
Serial.begin(115200); // 初始化串口
pinMode(4, OUTPUT); // 设置PWM输出引脚
Serial.println("Initializing...");
// Initialise the MAX30105 Particle Sensor
particleSensor.begin(Wire, I2C_SPEED_FAST);
particleSensor.setup();
//读取当前传感器版本号
uint8_t revID = particleSensor.getRevisionID();
Serial.print("Revision ID: ");
Serial.println(revID, HEX);
}
void loop()
{
long irValue = particleSensor.getIR(); //读取红外数值
float irValuef = irValue;
lastReading = irValuef; //记录每次红外数值
//滤波器,每4个数取平均值
rates[rateDropCounter++] = irValue;
if (rateDropCounter == RATE_SIZE) {
rateDropCounter = 0;
float avg = 0;
for (byte i = 0 ; i < RATE_SIZE ; i++)
avg += rates[i];
avg /= RATE_SIZE;
beatsPerMinute = 60 / ((millis() - lastBeat) / 1000.0);
lastBeat = millis();
//输出是否检测到脉冲和平均心率
if(heartRateFound() && doFiltering(averageDCEstimator(irValue))){
Serial.print(beatsPerMinute, 1);
Serial.print(",");
Serial.print(irValue, 1);
Serial.print(",");
Serial.print(avg, 1);
Serial.println("");
mySerial.print(beatsPerMinute, 1);
mySerial.print(",");
mySerial.print(irValue, 1);
mySerial.print(",");
mySerial.print(avg, 1);
mySerial.println("");
analogWrite(4, 255); //输出PWM信号
}else{
Serial.print("No Heartbeat detected");
Serial.println("");
mySerial.print("No Heartbeat detected");
mySerial.println("");
analogWrite(4, 0); //如果没有检测到脉冲,则PWM输出0
}
lastReading = 0;
}
}
```
阅读全文