BASIC4MCU | 질문게시판 | 아두이노 블루투스 통신
페이지 정보
작성자 으아아악 작성일2019-06-14 15:33 조회24,915회 댓글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() ;
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);
}
void serialOutput(){ // Decide How To Output Serial.
switch(outputType){
case PROCESSING_VISUALIZER:
sendDataToSerial('S', Signal); // goes to sendDataToSerial function
break;
case SERIAL_PLOTTER: // open the Arduino Serial Plotter to visualize these data
Serial.print(BPM);
Serial.print(",");
Serial.print(IBI);
Serial.print(",");
Serial.println(Signal);
BTSerial.print(BPM);
break;
default:
break;
}
}
// Decides How To OutPut BPM and IBI Data
void serialOutputWhenBeatHappens(){
switch(outputType){
case PROCESSING_VISUALIZER: // find it here https://github.com/WorldFamousElectronics/PulseSensor_Amped_Processing_Visualizer
sendDataToSerial('B',BPM); // send heart rate with a 'B' prefix
sendDataToSerial('Q',IBI); // send time between beats with a 'Q' prefix
BTSerial.print(BPM);
break;
default:
break;
}
}
// Sends Data to Pulse Sensor Processing App, Native Mac App, or Third-party Serial Readers.
void sendDataToSerial(char symbol, int data ){
Serial.print(symbol);
Serial.println(data);
}
위의 코드로 블루투스 모듈을 사용하여 앱에 bpm을 띄우고 있는데 어쩌다 한번씩 bpm값이 겹쳐서 나오는 경우가 발생합니다. 어느 부분이 문제가 되는 걸까요? 시리얼 모니터 상에서는 그런 현상이 없는데 스마트폰에 디스플레이 할 때만 이런 문제가 발생합니다.
댓글 4
조회수 24,915master님의 댓글
master 작성일
겹쳐서 나온다는 증상이 정확하게 어떤 현상일까요?
//
PC와 스마트폰의 시리얼은 모았다가 한꺼번에 전송하기도 합니다.
저레벨의 임베디드 보드보다 느립니다.
으아아악님의 댓글
으아아악
bpm 75가 정상이라면
bpm 7576 이런식으로 두값이 겹쳐서 나오거나
bpm 757 이렇게 짤려서 겹쳐나오는 현상이 발생합니다.
으아아악님의 댓글
으아아악
또 Serial Output 함수가 2개로 나뉘어져 있는데 제가 사용한 소스코드가 원래 LED 예제와 합쳐져 있는 것을 제가 임의로 뺀것이라 만약 심장 박동이 발생할 때 LED가 깜빡이는 동작을 제외한다고 생각하면 serialOutputWhenBeatHappens 함수는 없어도 되는 것인가요?
master님의 댓글
master
전체 코드를 외우고 있지 않으니 잘 모르겠습니다.
원래 예제를 동작시킬 때는 정상이었다면
바뀐 부분을 검토해보세요