mixly小车避障循迹代码
时间: 2024-06-11 13:02:16 浏览: 174
嵌入式智能避障循迹小车代码
Mixly小车避障循迹代码,是用于控制小车进行避障和循迹的代码。以下是一个简单的代码示例,仅供参考:
```
#include <MsTimer2.h>
int enA = 6;
int in1 = 7;
int in2 = 8;
int enB = 11;
int in3 = 9;
int in4 = 10;
int sensorPin1 = A0;
int sensorPin2 = A1;
int sensorPin3 = A2;
int sensorPin4 = A3;
int sensorPin5 = A4;
int leftSpeed, rightSpeed;
void setup() {
pinMode(enA, OUTPUT);
pinMode(in1, OUTPUT);
pinMode(in2, OUTPUT);
pinMode(enB, OUTPUT);
pinMode(in3, OUTPUT);
pinMode(in4, OUTPUT);
pinMode(sensorPin1, INPUT);
pinMode(sensorPin2, INPUT);
pinMode(sensorPin3, INPUT);
pinMode(sensorPin4, INPUT);
pinMode(sensorPin5, INPUT);
MsTimer2::set(20, control);
MsTimer2::start();
}
void loop() {
}
void control() {
int sensorValue1 = digitalRead(sensorPin1);
int sensorValue2 = digitalRead(sensorPin2);
int sensorValue3 = digitalRead(sensorPin3);
int sensorValue4 = digitalRead(sensorPin4);
int sensorValue5 = digitalRead(sensorPin5);
leftSpeed = 200;
rightSpeed = 200;
if (sensorValue3 == LOW) {
leftSpeed = -100;
rightSpeed = -100;
delay(300);
leftSpeed = -100;
rightSpeed = 100;
delay(500);
} else if (sensorValue2 == LOW) {
leftSpeed = -100;
rightSpeed = -100;
delay(300);
leftSpeed = -100;
rightSpeed = 100;
delay(500);
} else if (sensorValue4 == LOW) {
leftSpeed = -100;
rightSpeed = -100;
delay(300);
leftSpeed = 100;
rightSpeed = -100;
delay(500);
} else if (sensorValue1 == LOW) {
leftSpeed = -100;
rightSpeed = -100;
delay(300);
leftSpeed = 100;
rightSpeed = -100;
delay(500);
} else if (sensorValue5 == LOW) {
leftSpeed = -100;
rightSpeed = -100;
delay(300);
leftSpeed = -100;
rightSpeed = -100;
delay(500);
} else {
leftSpeed = 200;
rightSpeed = 200;
}
analogWrite(enA, abs(leftSpeed));
digitalWrite(in1, leftSpeed > 0 ? HIGH : LOW);
digitalWrite(in2, leftSpeed > 0 ? LOW : HIGH);
analogWrite(enB, abs(rightSpeed));
digitalWrite(in3, rightSpeed > 0 ? HIGH : LOW);
digitalWrite(in4, rightSpeed > 0 ? LOW : HIGH);
}
```
这段代码的功能是通过5个红外线传感器读取地面的黑线情况,根据读取结果实现小车的避障和循迹。如果传感器检测到了黑线,小车会做出相应的避障或者循迹动作。
阅读全文