#include <MsTimer2.h>
/*-----------------------------------------------
�ִ� bpm �� 150���� �Ѵ�
60*100/150 = 40
p2p ������ 40 ���� ���� �� ����.
-----------------------------------------------*/
#define MIN_P2P_INTERVAL 40
//#define bpm_DEBUG
int LPFln[4];
byte iLPF;
int DiffLn[4];
byte iDiff;
int P2Ptbl[8];
byte iP2Ptbl;
int preTilt;
int prePeakTime;
int lowPeak;
byte onTime;
/* ----------------------------------------------------------------------------
S E T U P
---------------------------------------------------------------------------- */
void setup() {
Serial.begin(9600);
Serial.println(F("wait 0.5second for brake ready..."));
delay(500);
onTime = 0;
iLPF = 0xff;
iDiff = 0xff;
iP2Ptbl = 0xf7;
preTilt = 0;
MsTimer2::set(10, samplingTimer);
MsTimer2::start();
}
/* ----------------------------------------------------------------------------
L O O P
---------------------------------------------------------------------------- */
void loop() {
if(onTime){
onTime = 0;
Diff(LPF(analogRead(0)));
}
}
//-----------------------------------------------
// �̺�
//-----------------------------------------------
void Diff(int data){
int tilt;
int avrWidthP2P, ovrWidthP2P;
int i;
byte status;
float bpm;
status = 0;
if(prePeakTime >= 0) prePeakTime++;
if(iDiff == 0xff){ //1st time
DiffLn[0] = data;
DiffLn[1] = data;
DiffLn[2] = data;
DiffLn[3] = data;
iDiff = 1;
return;
}
else {
DiffLn[iDiff++] = data;
iDiff &= 0x03;
tilt = data - DiffLn[iDiff]; //data : new, DiffLn[iDiff] : old
//���Ⱑ + ���� - �Ǵ� ��
if( (preTilt > 0 && tilt < 0) &&
((data - lowPeak) > 128) ) {
//-------------------------------
if(prePeakTime < MIN_P2P_INTERVAL){
#ifdef bpm_DEBUG
Serial.println("*RESET");
#endif
prePeakTime = 0;
}
//-------------------------------
else if(iP2Ptbl > 0x80){
P2Ptbl[iP2Ptbl & 0x07] = prePeakTime; //ó�� P2Ptbl �� ä���.
iP2Ptbl++;
prePeakTime = 0;
} //if(iP2Ptbl > 0x80)
//-------------------------------
else{
avrWidthP2P = 0;
for(int i=0;i<8;i++) avrWidthP2P += P2Ptbl[i];
avrWidthP2P /= 8;
ovrWidthP2P = avrWidthP2P / 4; //����� 25% �� �̳��� �־�� �Ѵ�.
P2Ptbl[iP2Ptbl++] = prePeakTime;
iP2Ptbl &= 0x07;
#ifdef bpm_DEBUG
Serial.print("Curr=");
Serial.print(prePeakTime);
Serial.print(",Avr=");
Serial.print(avrWidthP2P);
Serial.print(" ");
#endif
if(prePeakTime < avrWidthP2P){
if(avrWidthP2P - prePeakTime < ovrWidthP2P){ //�ð����� ��պ��� ������ �� ���̰� 25% ���� �۴�.
status++;
}
else{
prePeakTime = -1;
#ifdef bpm_DEBUG
Serial.print("*UR");
#endif
}
}
else if(prePeakTime - avrWidthP2P < ovrWidthP2P){ //�ð����� ��պ��� ũ�� �� ���̰� 25% ���� �۴�.
status++;
}
else { //�ð����� ��պ��� 25% �̻� Ŀ����.
prePeakTime = -1;
#ifdef bpm_DEBUG
Serial.print("*OR");
#endif
}
if(status){
#ifdef bpm_DEBUG
Serial.print("[");
#endif
lowPeak = data;
prePeakTime = 0;
//������ 8���� �ð����� ����Ͽ� bpm�� ����Ѵ�.
avrWidthP2P = 0;
for(i=0;i<8;i++){
avrWidthP2P += P2Ptbl[i];
#ifdef bpm_DEBUG
Serial.print(P2Ptbl[i]);
Serial.print(",");
#endif
}
#ifdef bpm_DEBUG
Serial.print("] --> ");
#endif
if(avrWidthP2P){
bpm = avrWidthP2P; //10ms unit
bpm = 60.0 * 100.0 * 8 / bpm;
}
else bpm = 0.0;
Serial.println(bpm, 1);
}//if(status)
}//else
}//if(preTilt > 0 && tilt < 0)
else {
if(lowPeak > data) lowPeak = data;
}
if(tilt) preTilt = tilt;
}//else{
}
//-----------------------------------------------
// 4�� LPF
//-----------------------------------------------
int LPF(uint16_t data){
if(iLPF == 0xff){ //1st time
LPFln[0] = data;
LPFln[1] = data;
LPFln[2] = data;
LPFln[3] = data;
iLPF = 1;
return data;
}
else {
LPFln[iLPF++] = data;
iLPF &= 0x03;
return ( (LPFln[0] + LPFln[1] + LPFln[2] + LPFln[3]) / 4);
}
}
/* ----------------------------------------------------------------------------
T I M E R
---------------------------------------------------------------------------- */
void samplingTimer(){
onTime++;
}
심박센서로 심박센서값을 축적시켜놧다가 5분에 한번씩 스마트워치 블루투스로 값을 전송시키는 소스를 짤려는데..감이오지않습니다...
어떤 변수와 함수를 써야되는지 가르쳐주세요... 요즘 스마트폰을보면 심박bmp표시를 할려고합니다... 근데 실시간으로 값을받아버리면 배터리소모가 너무커서 5분동안 값을 저장시켰다가 저장시킨값을 평균화하여 전송하는 방식으로 소스를 짜는거 도와주세요...
|