BASIC4MCU | 질문게시판 | 아두이노 앱인벤터
페이지 정보
작성자 으아아악 작성일2019-05-29 01:21 조회9,207회 댓글4건본문
#define PROCESSING_VISUALIZER 1
#define SERIAL_PLOTTER 2
#include <SoftwareSerial.h>
// Variables
int pulsePin = 0; // Pulse Sensor purple wire connected to analog pin 0
// Volatile Variables, used in the interrupt service routine!
volatile int BPM; // int that holds raw Analog in 0. updated every 2mS
volatile int Signal; // holds the incoming raw data
volatile int IBI = 600; // int that holds the time interval between beats! Must be seeded!
volatile boolean Pulse = false; // "True" when User's live heartbeat is detected. "False" when not a "live beat".
volatile boolean QS = false; // becomes true when Arduoino finds a beat.
SoftwareSerial BTSerial(2, 3);
// SET THE SERIAL OUTPUT TYPE TO YOUR NEEDS
// PROCESSING_VISUALIZER works with Pulse Sensor Processing Visualizer
// https://github.com/WorldFamousElectronics/PulseSensor_Amped_Processing_Visualizer
// SERIAL_PLOTTER outputs sensor data for viewing with the Arduino Serial Plotter
// run the Serial Plotter at 115200 baud: Tools/Serial Plotter or Command+L
static int outputType = SERIAL_PLOTTER;
void setup(){
Serial.begin(9600); // we agree to talk fast!
BTSerial.begin(9600);
interruptSetup(); // sets up to read Pulse Sensor signal every 2mS
// IF YOU ARE POWERING The Pulse Sensor AT VOLTAGE LESS THAN THE BOARD VOLTAGE,
// UN-COMMENT THE NEXT LINE AND APPLY THAT VOLTAGE TO THE A-REF PIN
// analogReference(EXTERNAL);
}
// Where the Magic Happens
void loop(){
serialOutput() ;
BTWerial.print(BPM);
if (QS == true){ // A Heartbeat Was Found
// BPM and IBI have been Determined
// Quantified Self "QS" true when arduino finds a heartbeat
// Set 'fadeRate' Variable to 255 to fade LED with pulse
serialOutputWhenBeatHappens(); // A Beat Happened, Output that to serial.
QS = false; // reset the Quantified Self flag for next time
}
delay(1000);
}
현재 심박센서를 사용하여 BPM을 구해 앱인벤터로 만든 앱에 디스플레이 하고있습니다.
앱인벤터에서 clock time interval을 1000으로 설정하고 위의 소스코드에서도 delay(1000)을 써서 시간을 맞춰줬는데
앱에서 BPM이 디스플레이 될 때 하나씩 나오다가(예: 90BPM) 가끔 두 값이 겹쳐서 나올 때가 있는데(예: 9091BPM) 원인이 무엇일까요?
댓글 4
조회수 9,207master님의 댓글
master 작성일
전체 소스가 아니죠?
올리지 않은 부분을 제가 어떻게 알 수 있을까요?
//
void loop(){
serialOutput() ;
BTWerial.print(BPM);
if (QS == true){ QS = false;
serialOutputWhenBeatHappens();
}
}
루프문에서는 모두 3라인의 시리얼 출력 코드가 있습니다.
이 중에서 간헐적으로 동작하는 것은 if(QS==true) 문입니다.
QS가 true 될 때에 출력이 달라지는 것이죠
조금만 집중해서 분석하면 원인을 알 수 있습니다.
으아아악님의 댓글
으아아악
#include <SoftwareSerial.h>
SoftwareSerial mySerial(2,3); //블루투스의 Tx, Rx핀을 2번 3번핀으로 설정
//
#define PROCESSING_VISUALIZER 1
#define SERIAL_PLOTTER 2
//
int pulsePin=A0;
//
int initCounter=0,initBPM=0;
//
volatile int BPM;
volatile int Signal;
volatile int IBI=600;
volatile bool Pulse=false;
volatile bool QS=false;
static int outputType=SERIAL_PLOTTER;
//
char str[100];
//
void setup(){
pinMode(3,OUTPUT);
Serial.begin(9600);
mySerial.begin(9600);
interruptSetup();
}
//
void loop(){
serialOutput();
//
if(QS==true){serialOutputWhenBeatHappens(); QS=false; }
delay(1000);
}
//
void serialOutput(){
switch(outputType){
case PROCESSING_VISUALIZER: sprintf(str,"Signal: %d\r\n",Signal); Serial.print(str); break;
case SERIAL_PLOTTER: sprintf(str,"BPM: %d,IBI: %d,Signal: %d\r\n",BPM,IBI,Signal); Serial.print(str); mySerial.print(BPM); break;
}
}
//
void serialOutputWhenBeatHappens(){
switch(outputType){
case PROCESSING_VISUALIZER: sprintf(str,"BPM: %d,IBI: %d\r\n",BPM,IBI); Serial.print(str); mySerial.print(BPM); break;
}
}
//
volatile int rate[10];
volatile unsigned long sampleCounter=0;
volatile unsigned long lastBeatTime=0;
volatile int P=512;
volatile int T=512;
volatile int thresh=530;
volatile int amp=0;
volatile boolean firstBeat=true;
volatile boolean secondBeat=false;
//
void interruptSetup(){
TCCR2A=0x02; TCCR2B=0x06; OCR2A=0X7C; TIMSK2=0x02; sei();
}
//
ISR(TIMER2_COMPA_vect){
cli();
Signal=analogRead(pulsePin);
sampleCounter+=2;
int N=sampleCounter-lastBeatTime;
//
if(Signal<thresh&&N>(IBI/5)*3){ if(Signal<T)T=Signal; }
//
if(Signal>thresh&&Signal>P){ P=Signal; }
//
if(N>250){
if((Signal>thresh)&&(Pulse==false)&&(N>(IBI/5)*3)){
Pulse=true;
IBI=sampleCounter-lastBeatTime;
lastBeatTime=sampleCounter;
//
if(secondBeat){ secondBeat=false; for(int i=0; i<=9; i++)rate[i]=IBI; }
//
if(firstBeat){ firstBeat=false; secondBeat=true; sei(); return; }
//
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;
//
if(initCounter==0){ initBPM=BPM; initCounter=1; }
//
QS=true;
}
}
if(Signal<thresh&&Pulse==true){
Pulse=false; amp=P-T; thresh=amp/2+T; P=thresh; T=thresh;
}
//
if(N>2500){
thresh=530; P=512; T=512; lastBeatTime=sampleCounter; firstBeat=true; secondBeat=false;
}
sei();
}
다른 소스 코드를 참고하여 바꾸어 보았는데도 시리얼 모니터 상에서는 제대로 나오지만 앱 디스플레이 상에서만 BPM값이 겹쳐 나오는 현상이 발생합니다. 이건 앱인벤터 상 문제일까요? 같은 질문을 반복하게 된 것 같아 죄송합니다.
으아아악님의 댓글
으아아악
이곳 질문게시판에서 같은 주제로 진행하신 분이 있는것 같아 그 소스 코드를 사용해봤는데도 똑같은 문제점이 발생하네요ㅠㅠ
master님의 댓글
master
운영상의 사소한 문제점은 직접 찾아서 해결하셔야합니다.
(포기하고 사용하시든지..)