arduino八路循迹小车代码
时间: 2024-06-13 17:01:32 浏览: 310
Arduino八路循迹小车代码是一种控制八路循迹小车运动的程序代码,主要实现小车的自动寻迹、避障等功能。具体实现方式可以参考以下代码:
```
#include <SoftwareSerial.h>
SoftwareSerial BTSerial(10, 11); // SoftwareSerial连接到蓝牙模块
int M1_1 = 6;
int M1_2 = 5;
int M2_1 = 7;
int M2_2 = 4;
int M3_1 = 9;
int M3_2 = 8;
int M4_1 = 12;
int M4_2 = 2;
void setup() {
pinMode(M1_1, OUTPUT);
pinMode(M1_2, OUTPUT);
pinMode(M2_1, OUTPUT);
pinMode(M2_2, OUTPUT);
pinMode(M3_1, OUTPUT);
pinMode(M3_2, OUTPUT);
pinMode(M4_1, OUTPUT);
pinMode(M4_2, OUTPUT);
BTSerial.begin(9600); // 初始化蓝牙串口
}
void loop() {
if (BTSerial.available()) {
char c = BTSerial.read();
if (c == 'W') { // 前进
digitalWrite(M1_1, HIGH);
digitalWrite(M1_2, LOW);
digitalWrite(M2_1, HIGH);
digitalWrite(M2_2, LOW);
digitalWrite(M3_1, HIGH);
digitalWrite(M3_2, LOW);
digitalWrite(M4_1, HIGH);
digitalWrite(M4_2, LOW);
}
else if (c == 'S') { // 后退
digitalWrite(M1_1, LOW);
digitalWrite(M1_2, HIGH);
digitalWrite(M2_1, LOW);
digitalWrite(M2_2, HIGH);
digitalWrite(M3_1, LOW);
digitalWrite(M3_2, HIGH);
digitalWrite(M4_1, LOW);
digitalWrite(M4_2, HIGH);
}
else if (c == 'A') { // 左转
digitalWrite(M1_1, HIGH);
digitalWrite(M1_2, LOW);
digitalWrite(M2_1, LOW);
digitalWrite(M2_2, HIGH);
digitalWrite(M3_1, HIGH);
digitalWrite(M3_2, LOW);
digitalWrite(M4_1, LOW);
digitalWrite(M4_2, HIGH);
}
else if (c == 'D') { // 右转
digitalWrite(M1_1, LOW);
digitalWrite(M1_2, HIGH);
digitalWrite(M2_1, HIGH);
digitalWrite(M2_2, LOW);
digitalWrite(M3_1, LOW);
digitalWrite(M3_2, HIGH);
digitalWrite(M4_1, HIGH);
digitalWrite(M4_2, LOW);
}
else if (c == 'Q') { // 停止
digitalWrite(M1_1, LOW);
digitalWrite(M1_2, LOW);
digitalWrite(M2_1, LOW);
digitalWrite(M2_2, LOW);
digitalWrite(M3_1, LOW);
digitalWrite(M3_2, LOW);
digitalWrite(M4_1, LOW);
digitalWrite(M4_2, LOW);
}
}
}
```
此代码实现了一个简单的控制八路循迹小车运动的蓝牙程序,其中,'W'表示前进,'S'表示后退,'A'表示左转,'D'表示右转,'Q'表示停止。通过在手机上发送相应的命令,即可控制八路循迹小车运动。
阅读全文