BASIC4MCU | 질문게시판 | ad8232, 블루투스를 이용하여 bpm을 측정하고 led로 표현하기 도와주세여ㅠㅠ
페이지 정보
작성자 아누이노생초보 작성일2020-11-27 19:05 조회16,871회 댓글6건본문
ad8232 모듈을 이용하여 bpm만을 측정하는 프로그램은 완성을 했습니다.
하지만 블루투스를 이용하여 bpm값을 측정한 값을 이용하여 led를 밝히는 프로그램을 만들고 있습니다.
블루투스를 이용하여 bpm이 측정이 되지 않아 led에도 불빛이 들어오지 않고있습니다.
제발 도와주세요ㅠㅠㅠㅠㅠㅠㅠㅠㅠㅠㅠㅠㅠㅠㅠㅠㅠㅠ
#include <SoftwareSerial.h>
#define btRX 8
#define btTX 9
SoftwareSerial Bluetooth(btRX, btTX);
String myString=" ";
int greenPin=2;
int yellowPin=5;
int redPin=4;
#define pulsePin A0// VARIABLES
int rate[10];
unsigned long sampleCounter = 0;
unsigned long lastBeatTime = 0;
unsigned long lastTime = 0, N;
int BPM = 0;
int IBI = 0;
int P = 512;
int T = 512;
int thresh = 512;
int amp = 100;
int Signal;
boolean Pulse = false;
boolean firstBeat = false;
boolean secondBeat = false;
boolean QS = false;void setup() {
Serial.begin(9600);
Bluetooth.begin(9600);
pinMode(greenPin, OUTPUT);
pinMode(yellowPin, OUTPUT);
pinMode(redPin, OUTPUT);pinMode(10, INPUT); // Setup for leads off detection LO +
pinMode(11, INPUT); // Setup for leads off detection LO -
pinMode(pulsePin, INPUT);
}void loop() {
serialOutput();if (QS == true) {
Serial.println("BPM: "+ String(BPM));
QS = false;
}
else if (millis() >= (lastTime + 2)) {
readPulse();
lastTime = millis();
}
}
void serialOutput(){
while(Bluetooth.available()) {
char myChar=(char)Bluetooth.read();
myString+=myChar;
Serial.print(myChar);
delay(5);
}
if(!myString.equals(""))
{
if(myString=="on")
{
Serial.println(BPM);
if(BPM<60||BPM>100){
if(BPM<60){
digitalWrite(yellowPin,HIGH);
delay(3000);
digitalWrite(yellowPin,LOW);
delay(1000);
}
if(BPM>100){
digitalWrite(redPin,HIGH);
delay(3000);
digitalWrite(redPin,LOW);
delay(1000);
}
}
else{
digitalWrite(greenPin,HIGH);
delay(3000);
digitalWrite(greenPin,LOW);
delay(1000);
}
}
}
else
{
}
myString="";
}
void readPulse() {Signal = analogRead(pulsePin);
sampleCounter += 2;
int N = sampleCounter - lastBeatTime;detectSetHighLow();
if (N > 250) {
if ( (Signal > thresh) && (Pulse == false) && (N > (IBI / 5) * 3) )
pulseDetected();
}if (Signal < thresh && Pulse == true) {
Pulse = false;
amp = P - T;
thresh = amp / 2 + T;
P = thresh;
T = thresh;
}if (N > 2500) {
thresh = 512;
P = 512;
T = 512;
lastBeatTime = sampleCounter;
firstBeat = true;
secondBeat = true;
}}
void detectSetHighLow() {if (Signal < thresh && N > (IBI / 5) * 3) {
if (Signal < T) {
T = Signal;
}
}if (Signal > thresh && Signal > P) {
P = Signal;
}}
void pulseDetected() {
Pulse = true;
IBI = sampleCounter - lastBeatTime;
lastBeatTime = sampleCounter;if (firstBeat) {
firstBeat = false;
return;
}
if (secondBeat) {
secondBeat = false;
for (int i = 0; i <= 9; i++) {
rate[i] = IBI;
}
}word runningTotal = 0;
for (int i = 0; i <= 8; i++) {
rate[i] = rate[i + 1];
runningTotal += rate[i];
}rate[9] = IBI;
runningTotal += rate[9];
runningTotal /= 10;
BPM = 60000 / runningTotal;
QS = true;
}
댓글 6
조회수 16,871master님의 댓글
master 작성일
ad8232 모듈을 이용하여 bpm만을 측정하는 프로그램은 완성
블루투스를 이용하여 bpm이 측정이 되지 않고있습니다.
//
블루투스를 이용해서 bpm을 측정한다는 말이 무슨 뜻일까요?
아누이노생초보님의 댓글
아누이노생초보
블루투스 통신을 이용하여 휴대폰을 이용하여 'on'을 입력시키면 bpm을 측정을 시작하고, 측정된 bpm을 이용하여 led를 켜는 프로그램을 만들고 있어요ㅠㅠ그런데 잘 되지 않네요..
master님의 댓글
master 작성일
"on"은 2문자입니다. 1문자로 제어하면 코드가 간단해집니다.
'n' : ON
'f' : OFF
master님의 댓글
master 작성일
#include <SoftwareSerial.h>
#define btRX 8
#define btTX 9
SoftwareSerial Bluetooth(btRX,btTX);
int greenPin=2;
int yellowPin=5;
int redPin=4;
#define pulsePin A0
// VARIABLES
int rate[10];
unsigned long sampleCounter=0;
unsigned long lastBeatTime=0;
unsigned long lastTime=0,N;
int BPM=0;
int IBI=0;
int P=512;
int T=512;
int thresh=512;
int amp=100;
int Signal;
boolean Pulse=false;
boolean firstBeat=false;
boolean secondBeat=false;
boolean QS=false;
char myChar;
//
void setup(){
Serial.begin(9600);
Bluetooth.begin(9600);
pinMode(greenPin,OUTPUT);
pinMode(yellowPin,OUTPUT);
pinMode(redPin,OUTPUT);
pinMode(10,INPUT); // Setup for leads off detection LO +
pinMode(11,INPUT); // Setup for leads off detection LO -
pinMode(pulsePin,INPUT);
}
//
void loop(){
serialOutput();
if(QS==true){ Serial.println("BPM: "+ String(BPM)); QS=false; }
else if(millis()>=(lastTime + 2)){ readPulse(); lastTime=millis(); }
}
//
void serialOutput(){
if(Bluetooth.available()){ myChar=(char)Bluetooth.read(); Serial.print(myChar); }
//
if(myChar=='n'){
Serial.println(BPM);
if(BPM<60||BPM>100){
if(BPM< 60){ digitalWrite(yellowPin,HIGH); delay(3000); digitalWrite(yellowPin,LOW); delay(1000); }
if(BPM>100){ digitalWrite(redPin,HIGH); delay(3000); digitalWrite(redPin,LOW); delay(1000); }
}
else{ digitalWrite(greenPin,HIGH); delay(3000); digitalWrite(greenPin,LOW); delay(1000); }
}
}
//
void readPulse(){
Signal=analogRead(pulsePin);
sampleCounter+=2;
int N=sampleCounter-lastBeatTime;
detectSetHighLow();
if(N>250){ if((Signal>thresh)&&(Pulse==false)&&(N>(IBI/5)*3))pulseDetected(); }
if(Signal<thresh && Pulse==true){ Pulse=false; amp=P-T; thresh=amp/2+T; P=thresh; T=thresh; }
if(N>2500){ thresh=512; P=512; T=512; lastBeatTime=sampleCounter; firstBeat=true; secondBeat=true; }
}
//
void detectSetHighLow(){
if(Signal<thresh && N>(IBI/5)*3){ if(Signal<T)T=Signal; }
if(Signal>thresh && Signal>P){ P=Signal; }
}
//
void pulseDetected(){
Pulse=true;
IBI=sampleCounter-lastBeatTime;
lastBeatTime=sampleCounter;
if(firstBeat){ firstBeat=false; return; }
if(secondBeat){ secondBeat=false; for(int i=0;i<=9; i++)rate[i]=IBI; }
word runningTotal=0;
for(int i=0;i<=8;i++){ rate[i]=rate[i+1]; runningTotal+=rate[i]; }
rate[9]=IBI; runningTotal+=rate[9]; runningTotal/=10;
BPM=60000/runningTotal;
QS=true;
}
아누이노생초보님의 댓글
아누이노생초보
감사합니다 정말 ㅜㅜㅜ
근데 해봤는데 led가 켜지지 않네요
bpm값은 잘 나와요!
bpm값을 통해 led가 켜져야 하는데 bpm값은 시리얼모니터에 출력이 되는데 led가 켜지지 않아요 ㅠㅠ
led의 문제인가 싶어서 led도 교체하고 해봤는데 led가 켜지지 않아요ㅠㅜㅠㅠㅠㅠㅠㅠㅠㅠㅠㅜㅜㅠㅠㅜㅠㅠ
led가 켜지지 않아요 ㅠㅠㅠ
master님의 댓글
master
회로 연결에 문제가 없다면 켜집니다.
//
시리얼모니터에 보면 라인피드 문자를 자동으로 전송하는 체크박스가 있습니다.
입력한 문자외에 아무 것도 출력하지 않도록 수정하세요