写一个arduino程序,控制车(四个电机驱动轮)实现循迹避障功能,要求如下:1.两个红外传感器辅助车实现沿着单一黑色线行驶;2.超声波传感器(HC-SR04)判断前方是否有障碍,如果有障碍,车子优先避障,然后绕过障碍物之后再次回到黑色线轨迹上。3.配备光敏传感器控制LED灯,外界环境亮度不够时点亮LED。
时间: 2023-05-24 13:03:30 浏览: 163
A:
以下是一个基本的arduino程序,主要通过红外传感器判断车辆是否在单一的黑色线上行驶,超声波传感器判断前方是否有障碍,如果有障碍,车子优先避障,然后绕过障碍物之后再次回到黑色线轨迹上。另外,通过光敏传感器控制LED灯,在低亮度环境下点亮LED。
~~~arduino
#define LED_PIN 11
#define TRIG_PIN 2
#define ECHO_PIN 3
#define LEFT_SENSOR_PIN A0
#define RIGHT_SENSOR_PIN A1
#define MOTOR1_PIN1 9
#define MOTOR1_PIN2 8
#define MOTOR2_PIN1 7
#define MOTOR2_PIN2 6
#define MOTOR3_PIN1 5
#define MOTOR3_PIN2 4
#define MOTOR4_PIN1 3
#define MOTOR4_PIN2 2
int distance = 0;
int sensor_l_value = 0;
int sensor_r_value = 0;
void setup() {
pinMode(LED_PIN, OUTPUT);
pinMode(TRIG_PIN, OUTPUT);
pinMode(ECHO_PIN, INPUT);
pinMode(MOTOR1_PIN1, OUTPUT);
pinMode(MOTOR1_PIN2, OUTPUT);
pinMode(MOTOR2_PIN1, OUTPUT);
pinMode(MOTOR2_PIN2, OUTPUT);
pinMode(MOTOR3_PIN1, OUTPUT);
pinMode(MOTOR3_PIN2, OUTPUT);
pinMode(MOTOR4_PIN1, OUTPUT);
pinMode(MOTOR4_PIN2, OUTPUT);
digitalWrite(LED_PIN, LOW);
Serial.begin(9600);
}
void loop() {
//读取传感器数值
sensor_l_value = analogRead(LEFT_SENSOR_PIN);
sensor_r_value = analogRead(RIGHT_SENSOR_PIN);
distance = sonar();
//判断车子行驶状态
if (sensor_l_value < 700 && sensor_r_value < 700) {
stop_car();
delay(50);
reverse();
delay(800);
turn_left();
}
else if (sensor_l_value < 700) {
left();
}
else if (sensor_r_value < 700) {
right();
}
else if (distance <= 15) {
stop_car();
delay(50);
back();
delay(800);
turn_left();
}
else {
forward();
}
//光敏传感器控制LED灯
int light = analogRead(A2);
if (light < 300) {
digitalWrite(LED_PIN, HIGH);
}
else {
digitalWrite(LED_PIN, LOW);
}
}
//超声波传感器探测距离
int sonar() {
digitalWrite(TRIG_PIN, LOW);
delayMicroseconds(2);
digitalWrite(TRIG_PIN, HIGH);
delayMicroseconds(10);
digitalWrite(TRIG_PIN, LOW);
int duration = pulseIn(ECHO_PIN, HIGH);
int distance = (duration/2) / 29.1;
return distance;
}
void left() {
digitalWrite(MOTOR1_PIN1, HIGH);
digitalWrite(MOTOR1_PIN2, LOW);
digitalWrite(MOTOR2_PIN1, HIGH);
digitalWrite(MOTOR2_PIN2, LOW);
digitalWrite(MOTOR3_PIN1, HIGH);
digitalWrite(MOTOR3_PIN2, LOW);
digitalWrite(MOTOR4_PIN1, HIGH);
digitalWrite(MOTOR4_PIN2, LOW);
}
void right() {
digitalWrite(MOTOR1_PIN1, LOW);
digitalWrite(MOTOR1_PIN2, HIGH);
digitalWrite(MOTOR2_PIN1, LOW);
digitalWrite(MOTOR2_PIN2, HIGH);
digitalWrite(MOTOR3_PIN1, LOW);
digitalWrite(MOTOR3_PIN2, HIGH);
digitalWrite(MOTOR4_PIN1, LOW);
digitalWrite(MOTOR4_PIN2, HIGH);
}
void forward() {
digitalWrite(MOTOR1_PIN1, HIGH);
digitalWrite(MOTOR1_PIN2, LOW);
digitalWrite(MOTOR2_PIN1, LOW);
digitalWrite(MOTOR2_PIN2, HIGH);
digitalWrite(MOTOR3_PIN1, LOW);
digitalWrite(MOTOR3_PIN2, HIGH);
digitalWrite(MOTOR4_PIN1, HIGH);
digitalWrite(MOTOR4_PIN2, LOW);
}
void back() {
digitalWrite(MOTOR1_PIN1, LOW);
digitalWrite(MOTOR1_PIN2, HIGH);
digitalWrite(MOTOR2_PIN1, HIGH);
digitalWrite(MOTOR2_PIN2, LOW);
digitalWrite(MOTOR3_PIN1, HIGH);
digitalWrite(MOTOR3_PIN2, LOW);
digitalWrite(MOTOR4_PIN1, LOW);
digitalWrite(MOTOR4_PIN2, HIGH);
}
void stop_car() {
digitalWrite(MOTOR1_PIN1, LOW);
digitalWrite(MOTOR1_PIN2, LOW);
digitalWrite(MOTOR2_PIN1, LOW);
digitalWrite(MOTOR2_PIN2, LOW);
digitalWrite(MOTOR3_PIN1, LOW);
digitalWrite(MOTOR3_PIN2, LOW);
digitalWrite(MOTOR4_PIN1, LOW);
digitalWrite(MOTOR4_PIN2, LOW);
}
void turn_left() {
digitalWrite(MOTOR1_PIN1, HIGH);
digitalWrite(MOTOR1_PIN2, LOW);
digitalWrite(MOTOR2_PIN1, HIGH);
digitalWrite(MOTOR2_PIN2, LOW);
digitalWrite(MOTOR3_PIN1, LOW);
digitalWrite(MOTOR3_PIN2, HIGH);
digitalWrite(MOTOR4_PIN1, LOW);
digitalWrite(MOTOR4_PIN2, HIGH);
}
void turn_right() {
digitalWrite(MOTOR1_PIN1, LOW);
digitalWrite(MOTOR1_PIN2, HIGH);
digitalWrite(MOTOR2_PIN1, LOW);
digitalWrite(MOTOR2_PIN2, HIGH);
digitalWrite(MOTOR3_PIN1, HIGH);
digitalWrite(MOTOR3_PIN2, LOW);
digitalWrite(MOTOR4_PIN1, HIGH);
digitalWrite(MOTOR4_PIN2, LOW);
}
~~~
阅读全文