BASIC4MCU | 질문게시판 | 아두이노 특정 상황에서 초음파 센서가 작동을 안하는거 같습니다.
페이지 정보
작성자 KMMS 작성일2022-12-01 18:28 조회453회 댓글0건본문
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27,20,4);
//
#include <SoftwareSerial.h>
SoftwareSerial HC06(2,3); // RX,TX
//
#include <Servo.h>
Servo servoLeft;
Servo servoRight;
// 초음파
int trig = 10;
int echo = 11;
// 주행 상태 변수
int count = 0;
// 화염 감지 및 상태 변수
int flame = 7;
int state = 0;
// 워터펌프
int WaterPumpA_L = 8;
int WaterPumpA_R = 9;
void setup(){
lcd.init(); lcd.backlight();
servoLeft.attach(13);
servoRight.attach(12);
Serial.begin(9600);
HC06.begin(9600);
// 초음파
pinMode(trig,OUTPUT);
pinMode(echo,INPUT);
// 화염 감지
pinMode(flame, INPUT);
Serial.begin(9600);
// 워터펌프
pinMode(WaterPumpA_L,OUTPUT);
pinMode(WaterPumpA_R,OUTPUT);
}
//
void loop(){
int tempI;
// 화염 감지
state = digitalRead(flame);
// 초음파
float Length, distance;
digitalWrite(trig, LOW);
delay(2);
digitalWrite(trig, HIGH);
delay(10);
digitalWrite(trig, LOW);
Length = pulseIn(echo, HIGH);
distance = ((float)(340*Length)/10000)/2;
if(HC06.available()){
String temp=HC06.readStringUntil(0x0D);
Serial.println(temp);
tempI = temp.toInt();
if(tempI>=30){
tone(4,5000,500);
lcd.setCursor(0,0); lcd.println("Temperature: ");
lcd.setCursor(0,1); lcd.print(tempI); lcd.print("C");
if(distance > 0 && distance < 10.0 && count == 0){
forward(20);
count++;
} else if(distance > 0 && distance < 10.0 && count == 1){
backward(200);
turnRight(600);
count++;
} else if (distance > 0 && distance < 10.0 && count == 2) {
backward(300);
forward(20);
turnLeft(1200);
count = 0;
}
else {
count = 0;
forward(20);
}
if(state == 0 ){
servoLeft.detach();
servoRight.detach();
Serial.println("ON");
digitalWrite(WaterPumpA_L,HIGH);
digitalWrite(WaterPumpA_R,LOW);
delay(30000);
digitalWrite(WaterPumpA_L,LOW);
digitalWrite(WaterPumpA_R,LOW);
delay(30000);
}
else{
}
}
else{ lcd.setCursor(0,0); lcd.println("Temperature: ");
lcd.setCursor(0,1); lcd.print(tempI); lcd.print("C");
}
}
}
//
void forward(int time){
servoLeft.writeMicroseconds(1600);
servoRight.writeMicroseconds(1400);
delay(time);
}
//
void turnLeft(int time){
servoLeft.writeMicroseconds(1300);
servoRight.writeMicroseconds(1300);
delay(time);
}
//
void turnRight(int time){
servoLeft.writeMicroseconds(1700);
servoRight.writeMicroseconds(1700);
delay(time);
}
//
void backward(int time){
servoLeft.writeMicroseconds(1400);
servoRight.writeMicroseconds(1600);
delay(time);
}
특정한 장소 블루투스로 일정온도이상의 값을 받아오면 모터가 동작하고, 앞의 장애물과의 거리를 감지하여 그 장소까지 움직이도록하는 코드를 작성하고 실행하고 있었습니다.
그런데 여기서 문제점이 생겼습니다.
일정 온도이상이 되면 모터가 작동하여 움직이는 것까지는 성공했는데, 앞의 장애물을 인지하지 못하고 직진만 하는 상황이 벌어졌습니다. 혹시 무엇을 고처야 초음파 센서가 잘 작동해서 장애물을 피해갈수 있을지 조언해주시면 감사합니다..
그리고 블루투스 통신 부분을 제외하고 소스를 입력해서 해보았을때는 초음파 센서가 앞의 장애물을 잘 인지하고 피해가는 동작을 실행하였기 때문에 초음파 센서 자체의 문제는 아닌것 같다고 생각합니다.
자그마한 조언이라도 해주시면 감사합니다..
댓글 0
조회수 453등록된 댓글이 없습니다.