아두이노의 자이로센서와 초음파센서를 이용하여 회전각도값과 거리값을 블루투스(HC-05)를 통하여 앱 인벤터로 전달하는 프로젝트를 하고있습니다.
아래는 아두이노 스케치 코드입니다.
#include
SoftwareSerial BTSerial(2,3);
#define SRF_TXRX 0x05 // Defines pin 5 to be used as RX and TX for SRF01
#define SRF_ADDRESS 0x01 // Address of the SFR01
#define GETSOFT 0x5D // Byte to tell SRF01 we wish to read software version
#define GETRANGE 0x54 // Byte used to get range from SRF01
#define GETSTATUS 0x5F // Byte used to get the status of the transducer
SoftwareSerial srf01 = SoftwareSerial(SRF_TXRX, SRF_TXRX); // Sets up software serial port for the SRF01
#include "I2Cdev.h"
#include "MPU6050.h"
#include "Wire.h"
MPU6050 accelgyro;
int16_t ax, ay, az, gx, gy, gz;
double timeStep, time, timePrev;
double arx, ary, arz, grx, gry, grz, gsx, gsy, gsz, rx, ry, rz;
int i;
double gyroScale = 131;
void setup() {
Wire.begin();
srf01.begin(9600);
Serial.begin(9600);
BTSerial.begin(9600);
srf01.listen(); // Make sure that the SRF01 software serial port is listening for data as only one software serial port can listen at a time
delay(200); // Waits some time to make sure everything is powered up
byte softVer;
SRF01_Cmd(SRF_ADDRESS, GETSOFT); // Request the SRF01 software version
while (srf01.available() < 1);
softVer = srf01.read(); // Read software version from SRF01
accelgyro.initialize();
time = millis();
i = 1;
}
void loop() {
gyro_calculation();
byte hByte, lByte, statusByte, b1, b2, b3;
SRF01_Cmd(SRF_ADDRESS, GETRANGE); // Get the SRF01 to perform a ranging and send the data back to the arduino
while (srf01.available() < 2);
hByte = srf01.read(); // Get high byte
lByte = srf01.read(); // Get low byte
unsigned long range = ((hByte<<8)+lByte); // Put them together
int rrz = (int)rz+90;
Serial.print(rrz); Serial.print("|");
Serial.println(range, DEC);
BTSerial.print(rrz); BTSerial.print("|");
BTSerial.println(range, DEC);
}
void gyro_calculation(){
// set up time for integration
timePrev = time;
time = millis();
timeStep = (time - timePrev) / 1000; // time-step in s
// collect readings
accelgyro.getMotion6(&ax, &ay, &az, &gx, &gy, &gz);
// apply gyro scale from datasheet
gsx = gx/gyroScale; gsy = gy/gyroScale; gsz = gz/gyroScale;
// calculate accelerometer angles
arx = (180/3.141592) * atan(ax / sqrt(square(ay) + square(az)));
ary = (180/3.141592) * atan(ay / sqrt(square(ax) + square(az)));
arz = (180/3.141592) * atan(sqrt(square(ay) + square(ax)) / az);
// set initial values equal to accel values
if (i == 1) {
grx = arx;
gry = ary;
grz = arz;
}
// integrate to find the gyro angle
else{
grx = grx + (timeStep * gsx);
gry = gry + (timeStep * gsy);
grz = grz + (timeStep * gsz);
}
// apply filter
rx = (0.1 * arx) + (0.9 * grx);
ry = (0.1 * ary) + (0.9 * gry);
rz = (0.1 * arz) + (0.9 * grz);
// print result
i = i + 1;
delay(50);
}
void SRF01_Cmd(byte Address, byte cmd){ // Function to send commands to the SRF01
pinMode(SRF_TXRX, OUTPUT);
digitalWrite(SRF_TXRX, LOW); // Send a 2ms break to begin communications with the SRF01
delay(2);
digitalWrite(SRF_TXRX, HIGH);
delay(1);
srf01.write(Address); // Send the address of the SRF01
srf01.write(cmd); // Send commnd byte to SRF01
pinMode(SRF_TXRX, INPUT);
int availbleJunk = srf01.available(); // As RX and TX are the same pin it will have recieved the data we just sent out, as we dont want this we read it back and ignore it as junk before waiting for useful data to arrive
for(int x = 0; x < availbleJunk; x++) byte junk = srf01.read();
}
구글 플레이스토어에서 Bluetooth spp tools pro를 다운받아 아두이노에서 안드로이드 폰으로 전달되는 데이터가 정상 수신되는 것은 아래의 사진과 같이 확인했습니다.
정상 동작 확인 후
앱 인벤터 디자인과 블록을 작성했습니다.
디자인
블록
앱 인벤터 작성 후 폰과 연동하여 확인을 해봤는데 데이터값 처리가 이상하게 되어 아래와 같은 오류가 뜹니다.
어디서 오류가 났는지 모르겠습니다. 간절한 도움을 바랍니다.
|