arduino避障循迹小车代码 用到有hc-sr04超声波测距模块和五路红外循迹模块
时间: 2023-09-27 15:08:37 浏览: 196
基于arduino的循迹避障小车
以下是一个基于Arduino的避障循迹小车的代码,其中包含了HC-SR04超声波测距模块和五路红外循迹模块的使用:
```
// 引入需要使用的库
#include <NewPing.h>
// HC-SR04超声波测距模块的引脚定义
#define TRIGGER_PIN 12
#define ECHO_PIN 11
#define MAX_DISTANCE 200
// 五路红外循迹模块的引脚定义
#define IR1 2
#define IR2 3
#define IR3 4
#define IR4 5
#define IR5 6
// 定义小车的左右电机引脚
#define LEFT_MOTOR_PIN1 7
#define LEFT_MOTOR_PIN2 8
#define RIGHT_MOTOR_PIN1 9
#define RIGHT_MOTOR_PIN2 10
// 定义小车的运行状态
enum State {
OBSTACLE_AVOIDANCE,
LINE_FOLLOWING
};
// 创建一个NewPing对象,用于超声波测距
NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE);
// 初始化小车的左右电机
void setupMotors() {
pinMode(LEFT_MOTOR_PIN1, OUTPUT);
pinMode(LEFT_MOTOR_PIN2, OUTPUT);
pinMode(RIGHT_MOTOR_PIN1, OUTPUT);
pinMode(RIGHT_MOTOR_PIN2, OUTPUT);
}
// 控制小车的左轮电机
void setLeftMotor(int direction, int speed) {
switch (direction) {
case 1:
digitalWrite(LEFT_MOTOR_PIN1, HIGH);
digitalWrite(LEFT_MOTOR_PIN2, LOW);
break;
case -1:
digitalWrite(LEFT_MOTOR_PIN1, LOW);
digitalWrite(LEFT_MOTOR_PIN2, HIGH);
break;
default:
digitalWrite(LEFT_MOTOR_PIN1, LOW);
digitalWrite(LEFT_MOTOR_PIN2, LOW);
}
analogWrite(LEFT_MOTOR_PIN1, speed);
}
// 控制小车的右轮电机
void setRightMotor(int direction, int speed) {
switch (direction) {
case 1:
digitalWrite(RIGHT_MOTOR_PIN1, HIGH);
digitalWrite(RIGHT_MOTOR_PIN2, LOW);
break;
case -1:
digitalWrite(RIGHT_MOTOR_PIN1, LOW);
digitalWrite(RIGHT_MOTOR_PIN2, HIGH);
break;
default:
digitalWrite(RIGHT_MOTOR_PIN1, LOW);
digitalWrite(RIGHT_MOTOR_PIN2, LOW);
}
analogWrite(RIGHT_MOTOR_PIN1, speed);
}
// 将小车的左右电机都停止
void stopMotors() {
setLeftMotor(0, 0);
setRightMotor(0, 0);
}
// 检测小车前方是否有障碍物
bool obstacleDetected() {
delay(50);
int distance = sonar.ping_cm();
return (distance < 20);
}
// 将小车切换到避障状态
void switchToObstacleAvoidance() {
stopMotors();
delay(1000);
setLeftMotor(1, 100);
setRightMotor(1, 100);
delay(1000);
stopMotors();
}
// 将小车切换到循迹状态
void switchToLineFollowing() {
stopMotors();
delay(1000);
setLeftMotor(1, 100);
setRightMotor(-1, 100);
delay(1000);
stopMotors();
}
// 将小车的左右电机控制为按照红外循迹模块的状态行驶
void followLine(int ir1, int ir2, int ir3, int ir4, int ir5) {
int leftSpeed = 100;
int rightSpeed = 100;
if (ir1 == LOW && ir2 == LOW && ir3 == HIGH && ir4 == LOW && ir5 == LOW) {
leftSpeed = 80;
rightSpeed = 120;
} else if (ir1 == LOW && ir2 == HIGH && ir3 == HIGH && ir4 == LOW && ir5 == LOW) {
leftSpeed = 100;
rightSpeed = 100;
} else if (ir1 == LOW && ir2 == HIGH && ir3 == LOW && ir4 == LOW && ir5 == LOW) {
leftSpeed = 120;
rightSpeed = 80;
} else if (ir1 == LOW && ir2 == HIGH && ir3 == LOW && ir4 == HIGH && ir5 == LOW) {
leftSpeed = 100;
rightSpeed = 100;
} else if (ir1 == LOW && ir2 == HIGH && ir3 == HIGH && ir4 == HIGH && ir5 == LOW) {
leftSpeed = 100;
rightSpeed = 100;
} else if (ir1 == LOW && ir2 == LOW && ir3 == HIGH && ir4 == LOW && ir5 == LOW) {
leftSpeed = 80;
rightSpeed = 120;
} else if (ir1 == LOW && ir2 == LOW && ir3 == LOW && ir4 == HIGH && ir5 == LOW) {
leftSpeed = 100;
rightSpeed = 100;
} else if (ir1 == LOW && ir2 == LOW && ir3 == HIGH && ir4 == HIGH && ir5 == LOW) {
leftSpeed = 100;
rightSpeed = 100;
} else if (ir1 == LOW && ir2 == LOW && ir3 == LOW && ir4 == LOW && ir5 == LOW) {
leftSpeed = 0;
rightSpeed = 0;
}
setLeftMotor((leftSpeed > 0) ? 1 : -1, abs(leftSpeed));
setRightMotor((rightSpeed > 0) ? 1 : -1, abs(rightSpeed));
}
void setup() {
// 初始化串口通信
Serial.begin(9600);
// 初始化小车的左右电机
setupMotors();
}
void loop() {
// 检测小车是否需要切换状态
if (obstacleDetected()) {
switchToObstacleAvoidance();
} else {
switchToLineFollowing();
}
// 读取红外循迹模块的状态
int ir1 = digitalRead(IR1);
int ir2 = digitalRead(IR2);
int ir3 = digitalRead(IR3);
int ir4 = digitalRead(IR4);
int ir5 = digitalRead(IR5);
// 控制小车按照红外循迹模块的状态行驶
followLine(ir1, ir2, ir3, ir4, ir5);
// 输出调试信息
Serial.print(ir1);
Serial.print(ir2);
Serial.print(ir3);
Serial.print(ir4);
Serial.println(ir5);
}
```
需要注意的是,上述代码只是一个示例,具体实现可能会因为硬件设备的不同而有所差异。在使用时,请根据自己的实际情况对代码进行调整和修改。
阅读全文