BASIC4MCU | 질문게시판 | 답변 : 아두이노 특정 상황에서 초음파 센서가 작동을 안하는거 같습니다.
페이지 정보
작성자 master 작성일2022-12-01 20:08 조회538회 댓글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;
int tempI;
//
void setup(){
lcd.init(); lcd.backlight();
//
servoLeft.attach(13); servoRight.attach(12);
// 초음파
pinMode(trig,OUTPUT); pinMode(echo,INPUT);
// 화염 감지
pinMode(flame, INPUT);
Serial.begin(9600);
// 워터펌프
pinMode(WaterPumpA_L,OUTPUT); pinMode(WaterPumpA_R,OUTPUT);
//
Serial.begin(9600);
HC06.begin(9600);
}
//
void loop(){
// 화염 감지
state = digitalRead(flame);
// 초음파
float Length, distance;
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
조회수 538등록된 댓글이 없습니다.