안녕하세요 센서 측정값을 블루투스를 통해 전송하는 것 질문입니다.
페이지 정보
작성자 지지 쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 작성일16-06-03 15:02 조회2,958회 댓글1건본문
올려주신 초음파 센서로 거리측정하는 코드와 시리얼 통신 코드를 이용해서 전체 코드를 작성했습니다.
문제는 시리얼 모니터를 통해 어떠한 값을 보내줬을때만 거리 값이 핸드폰으로 보여지기 시작합니다.
즉 자체 건전지로 아두이노와 연결했을 때는 핸드폰으로 측정 값이 보여지지 않습니다. 아두이노와 컴퓨터를 연결해 시리얼 모니터에 아무 문자나 쳐서 보냈을때만 핸드폰에 측정값이 보여지기 시작하고 그 이후에는 작동을 합니다.... 문제가 무엇인지 잘 모르겠습니다.. 부탁드립니다..
#include <SoftwareSerial.h>
SoftwareSerial BTSerial(8, 7);
void setup(){
BTSerial.begin(9600);
Serial.begin(9600);
pinMode(2,OUTPUT); // 센서 Trig 핀
pinMode(3,INPUT); // 센서 Echo 핀
}
void loop(){
long duration, cm;
digitalWrite(2,HIGH); // 센서에 Trig 신호 입력
delayMicroseconds(10); // 10us 정도 유지
digitalWrite(2,LOW); // Trig 신호 off
duration = pulseIn(3,HIGH); // Echo pin: HIGH->Low 간격을 측정
cm = microsecondsToCentimeters(duration);
Serial.print(cm);
Serial.print("cm");
Serial.println();
if (Serial.available()) {
BTSerial.print(cm);
}
delay(1000); // 0.3초 대기 후 다시 측정
}
long microsecondsToInches(long microseconds)
{
// According to Parallax's datasheet for the PING))), there are
// 73.746 microseconds per inch (i.e. sound travels at 1130 feet per
// second). This gives the distance travelled by the ping, outbound
// and return, so we divide by 2 to get the distance of the obstacle.
// See: http://www.parallax.com/dl/docs/prod/acc/28015-PING-v1.3.pdf
return microseconds / 74 / 2;
}
long microsecondsToCentimeters(long microseconds)
{
// The speed of sound is 340 m/s or 29 microseconds per centimeter.
// The ping travels out and back, so to find the distance of the
// object we take half of the distance travelled.
return microseconds / 29 / 2;
}
댓글목록
최고관리자님의 댓글

아래 부분이 잘못됐어요.
if (Serial.available()) {
BTSerial.print(cm);
}
Serial.available() 이 아니라 BTSerial.available() 로 바뀌어야 합니다.