아두이노 블루투스 통신이후 데이터값 출력에서 자꾸 오류가 발생합니다.

2,007 views
Skip to first unread message

이이경찬

unread,
Dec 2, 2017, 11:10:31 PM12/2/17
to Ardunity Forum (Korea)
온도센서  초음파센서를 이용하여 측정값을 LCD와 블루투스 센서를 이용하여 어플리케이션에 출력하게끔 하려고하는데
값이 자꾸 깨진채 나옵니다.. 각각 센서는 hc sr04 (초음파) , DS18S20(온도) hc 06 (블루투스) , lcd를 사용하였습니다
 
어떻게 해결해야할까요?
 
#include <SoftwareSerial.h>
SoftwareSerial BTserial(6,7);
#include <Wire.h>                        // i2C 통신을 위한 라이브러리
#include <LiquidCrystal_I2C.h>        // LCD 1602 I2C용 라이브러리
LiquidCrystal_I2C lcd(0x3F,16,2);     // 접근주소: 0x3F or 0x27
#define TRIG 12
#define ECHO 13
#include <OneWire.h>
int DS18S20_Pin = 2; //DS18S20 Signal pin on digital 2
//Temperature chip i/o
OneWire ds(DS18S20_Pin); // on digital pin 2

void setup() {
  lcd.init();                      // LCD 초기화
  lcd.backlight();                // 백라이트 켜기
  Serial.begin(9600);   //시리얼모니터
  BTserial.begin(9600);
  pinMode(TRIG, OUTPUT);
  pinMode(ECHO, INPUT);
}
void loop() {
  digitalWrite(TRIG,LOW);
  delayMicroseconds(2);
  digitalWrite(TRIG,HIGH);
  delayMicroseconds(10);
  digitalWrite(TRIG,LOW);
  long distance = pulseIn(ECHO, HIGH) * 17 / 1000;
  Serial.print(distance);
  Serial.println("cm");
 
  //if(BTserial.available()) {
  //BTserial.print("Distance = ");
  BTserial.print(distance);
  //BTserial.print("cm");
  //}
  lcd.setCursor(0,0);
  lcd.print("Distance = ");
  if(distance >0 && distance <10)
  {
    lcd.print("00");
  }
  else if(distance >=10 && distance <100)
  {
    lcd.print("0");
  }
  lcd.print(distance);
  lcd.print("cm");
  delay(1000);
  float temperature = getTemp();
 Serial.println(temperature);
 
 //if(BTserial.available()) {
 //BTserial.print("Temp = ");
 BTserial.print(temperature);
 //BTserial.print(" C");
 //}
 
 lcd.setCursor(0,1);
 lcd.print("Temp = ");
 lcd.print(temperature);
 lcd.print(" C");
 delay(1000); //just here to slow down the output so it is easier to read
}
float getTemp(){
 //returns the temperature from one DS18S20 in DEG Celsius
 byte data[12];
 byte addr[8];
 if ( !ds.search(addr)) {
   //no more sensors on chain, reset search
   ds.reset_search();
   return -1000;
 }
 if ( OneWire::crc8( addr, 7) != addr[7]) {
   Serial.println("CRC is not valid!");
   return -1000;
 }
 if ( addr[0] != 0x10 && addr[0] != 0x28) {
   Serial.print("Device is not recognized");
   return -1000;
 }
 ds.reset();
 ds.select(addr);
 ds.write(0x44,1); // start conversion, with parasite power on at the end
 byte present = ds.reset();
 ds.select(addr); 
 ds.write(0xBE); // Read Scratchpad
 
 for (int i = 0; i < 9; i++) { // we need 9 bytes
  data[i] = ds.read();
 }
 
 ds.reset_search();
 
 byte MSB = data[1];
 byte LSB = data[0];
 float tempRead = ((MSB << 8) | LSB); //using two's compliment
 float TemperatureSum = tempRead / 16;
 
 return TemperatureSum;
 
}
 
아두이노 소스이고  이런식으로 꺠져서 출력됩니다.
해당 어플리케이션 블루투스 데이터값을 받아오는 앱인벤터 블록구현은 저렇게 구현했습니다.
 

Jaehong Oh

unread,
Dec 3, 2017, 3:49:59 AM12/3/17
to Ardunity Forum (Korea)
본 포럼은 아두니티 사용자를 위한 그룹입니다.
아두니티는 아두이노와 유니티를 손쉽게 연결시킬 수 있는 솔루션입니다.
이와 관련하여 아두이노 혹은 유니티에 대한 기초적 문의도 대응해드리고 있습니다.
위 질문은 아두이노와 앱인벤터를 연동시키는 내용입니다.
따라서, 자세한 도움은 드리기 어려울 것 같습니다.

다만, 조금의 힌트는 드리겠습니다.
데이터가 깨지는 가장 큰 이유는 신호의 간섭이나 통신 속도 불일치입니다.
1. 보레이트가 양쪽 다 일치되는지 확인해야 합니다.
2. SoftwareSerial의 경우 직접 배선을 하기때문에 신호 간섭이 있을 수 있습니다.
3. 일반적으로 시리얼 통신은 ASCII 포맷입니다. 앱에서 Unicode기반 포맷을 사용할 시 깨질 수 있습니다.


eastse...@gmail.com

unread,
Feb 3, 2018, 8:56:36 PM2/3/18
to Ardunity Forum (Korea)
제 생각에는

아두이노에서 온도값과 거리값 측정 간격 딜레이를 설정하세요 delay(1000);
앱 인벤터 클락에 타임인터발을 조정해보세요(1)

임의의 값으로 예시를 드린 것이고, 적절한 값을 찾기 바랍니다

Reply all
Reply to author
Forward
0 new messages