BASIC4MCU | 질문게시판 | 아두이노 라인트레이서 질문 있습니다.
페이지 정보
작성자 99999 작성일2023-12-14 18:45 조회590회 댓글0건본문
#include <SoftwareSerial.h>
#define motor1EnablePin 9
#define motor1_1 4
#define motor1_2 7
#define motor2EnablePin 10
#define motor2_1 8
#define motor2_2 12
int bluetoothTx = 2;
int bluetoothRx = 3;
int infrared1 = 13;
int infrared2 = 5;
int infrared3 = 11;
int infrared4 = 6;
int motor1Speed = 0;
int motor2Speed = 0;
SoftwareSerial bluetooth(bluetoothTx, bluetoothRx);
void setup() {
Serial.begin(9600);
bluetooth.begin(9600);
pinMode(motor1_1, OUTPUT);
pinMode(motor1_2, OUTPUT);
pinMode(motor2_1, OUTPUT);
pinMode(motor2_2, OUTPUT);
pinMode(motor1EnablePin, OUTPUT);
pinMode(motor2EnablePin, OUTPUT);
pinMode(infrared1, INPUT);
pinMode(infrared2, INPUT);
pinMode(infrared3, INPUT);
pinMode(infrared4, INPUT);
}
void loop() {
char cmd=' ';
int state1 = digitalRead(infrared1);
int state2 = digitalRead(infrared2);
int state3 = digitalRead(infrared3);
int state4 = digitalRead(infrared4);
motor1Speed = constrain(motor1Speed, -255, 255);
motor2Speed = constrain(motor2Speed, -255, 255);
/*if (bluetooth.available())
{
cmd = (char)bluetooth.read();
while(cmd=='w')
{ */
if (state1 == 0 && state2 == 0 && state3 == 0 && state4 == 0)
{
do{
motor1Speed = 130;
motor2Speed = 130;
state1 = digitalRead(infrared1);
state2 = digitalRead(infrared2);
state3 = digitalRead(infrared3);
state4 = digitalRead(infrared4);
Serial.println("w");
}while (state1 == 0 && state2 == 0 && state3 == 0 && state4 == 0);
}
else if (state1 == 0 && state2 == 0 && state3 == 0 && state4 == 1)
{
do{
Serial.println("6");
motor1Speed = 10;
motor2Speed = 130;
state1 = digitalRead(infrared1);
state2 = digitalRead(infrared2);
state3 = digitalRead(infrared3);
state4 = digitalRead(infrared4);
}while (!(state1== 1 && state2 == 0 && state3 == 0 && state4 == 0));
}
else if (state1 == 0 && state2 == 1 && state3 == 0 && state4 == 0)
{
do{
Serial.println("0");
motor1Speed = 10;
motor2Speed = 130;
state1 = digitalRead(infrared1);
state2 = digitalRead(infrared2);
state3 = digitalRead(infrared3);
state4 = digitalRead(infrared4);
}while (!(state1== 0 && state2 == 0 && state3 == 0 && state4 == 0));
}
else if (state1 == 0 && state2 == 1 && state3 == 0 && state4 == 1)
{
do{
motor1Speed = 10;
motor2Speed = 130;
Serial.println("1");
state1 = digitalRead(infrared1);
state2 = digitalRead(infrared2);
state3 = digitalRead(infrared3);
state4 = digitalRead(infrared4);
}while (!(state1== 1 && state2 == 0 && state3 == 0 && state4 == 0));
}
else if (state1 == 1 && state2 == 0 && state3 == 1 && state4 == 0)
{
do{
motor1Speed = 130;
motor2Speed = 10;
Serial.println("2");
state1 = digitalRead(infrared1);
state2 = digitalRead(infrared2);
state3 = digitalRead(infrared3);
state4 = digitalRead(infrared4);
}while (!(state1== 0 && state2 == 1 && state3 == 0 && state4 == 0));
}
else if (state1 == 0 && state2 == 0 && state3 == 1 && state4 == 0)
{
do{
motor1Speed = 130;
motor2Speed = 10;
Serial.println("3");
state1 = digitalRead(infrared1);
state2 = digitalRead(infrared2);
state3 = digitalRead(infrared3);
state4 = digitalRead(infrared4);
}while (!(state1== 0 && state2 == 1 && state3 == 0 && state4 == 0));
}
else if (state1 == 1 && state2 == 0 && state3 == 0 && state4 == 0)
{
do{
motor1Speed = 130;
motor2Speed = 10;
Serial.println("4");
state1 = digitalRead(infrared1);
state2 = digitalRead(infrared2);
state3 = digitalRead(infrared3);
state4 = digitalRead(infrared4);
}while (!(state1== 0 && state2 == 0 && state3 == 0 && state4 == 0));
}
else if (state1 == 1 && state2 == 1 && state3 == 1 && state4 == 1)
{
do{
motor1Speed = 0;
motor2Speed = 0;
state1 = digitalRead(infrared1);
state2 = digitalRead(infrared2);
state3 = digitalRead(infrared3);
state4 = digitalRead(infrared4);
}while (state1 == 1 && state2 == 1 && state3 == 1 && state4 == 1);
}
else
{
motor1Speed = 0;
motor2Speed = 0
;
}
if (motor1Speed > 0) {
digitalWrite(motor1_1, HIGH);
digitalWrite(motor1_2, LOW);
} else if (motor1Speed < 0) {
digitalWrite(motor1_1, LOW);
digitalWrite(motor1_2, HIGH);
} else {
digitalWrite(motor1_1, LOW);
digitalWrite(motor1_2, LOW);
}
analogWrite(motor1EnablePin, abs(motor1Speed));
if (motor2Speed > 0) {
digitalWrite(motor2_1, HIGH);
digitalWrite(motor2_2, LOW);
} else if (motor2Speed < 0) {
digitalWrite(motor2_1, LOW);
digitalWrite(motor2_2, HIGH);
} else {
digitalWrite(motor2_1, LOW);
digitalWrite(motor2_2, LOW);
}
analogWrite(motor2EnablePin, abs(motor2Speed));
}
//}
//}
적외선 센서 4개를 이용하여 라인트레이서를 제작하였는데 실제 주행을 해보면 0000일때 회전을 하는 등의 문제가 생기는데 어디서 문제가 있는 지 모르겟습니다
0100좌회전
1000우회전
0001 1000이 될때까지 좌회전
0010 0100이 될때까지 우회전
0101 1000이 될때까지 좌회전
1010 0100이 될때까지 우회전
0000 직진입니다.
댓글 0
조회수 590등록된 댓글이 없습니다.