|
最近用ArduIno uno 做的心率监测输出小玩意儿,用的MStimer库做定时oled输出,然后loop里面一直检测心率。上传程序后发现loop里面只能循环一会儿,大概是两到三次定时时间,然后就停止运行loop一直在运行中断程序。看了半天也不懂到底是哪出了问题,向各位大佬请教,以下是代码:#include <Wire.h>
#include "U8glib.h"
#include <DS3231.h>
#include "MAX30105.h"
#include "heartRate.h"
#include <MsTimer2.h>
MAX30105 particleSensor;
U8GLIB_SSD1306_128X64 u8g(U8G_I2C_OPT_NONE|U8G_I2C_OPT_DEV_0);
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);
Wire.begin();
MsTimer2::set(1000, XianShi);
MsTimer2::start();
if (!particleSensor.begin(Wire, I2C_SPEED_FAST)) //Use default I2C port, 400kHz speed
{
while (1);
}
while (Serial.available() == 1) ; //wait until user presses a key
Serial.read();
particleSensor.setup(); //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
}
void loop() {
long irValue = particleSensor.getIR();
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 this reading 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("IR=");
Serial.print(irValue);
Serial.print(", BPM=");
Serial.print(beatsPerMinute);
Serial.print(", Avg BPM=");
Serial.print(beatAvg);
if (irValue < 50000)
Serial.print(" No finger?");
Serial.println();
}
void XianShi()
{
if
(-1<gForceX&&gForceX<1&&gForceY<0.1&&gForceY>-0.5&&gForceZ<1.5 )
{u8g.firstPage();
do {
draw();
}
while( u8g.nextPage() );
}
else
{u8g.firstPage();
do {
u8g.setFont(u8g_font_ncenB14);
u8g.drawStr(0,20,"iii ");
} while( u8g.nextPage() );
}
}
draw的代码我就不贴了无关紧要
|
|