我需要一个arduino小车代码
时间: 2023-05-29 10:06:14 浏览: 274
这是一个基础的Arduino小车代码,可以让小车前进、后退、左转、右转。
```
//定义马达控制引脚
int motor1Pin1 = 2;
int motor1Pin2 = 3;
int motor2Pin1 = 4;
int motor2Pin2 = 5;
void setup() {
//设置马达控制引脚为输出模式
pinMode(motor1Pin1, OUTPUT);
pinMode(motor1Pin2, OUTPUT);
pinMode(motor2Pin1, OUTPUT);
pinMode(motor2Pin2, OUTPUT);
}
void loop() {
//向前走
moveForward();
delay(2000);
//向后走
moveBackward();
delay(2000);
//左转
turnLeft();
delay(2000);
//右转
turnRight();
delay(2000);
}
//向前走
void moveForward() {
digitalWrite(motor1Pin1, HIGH);
digitalWrite(motor1Pin2, LOW);
digitalWrite(motor2Pin1, HIGH);
digitalWrite(motor2Pin2, LOW);
}
//向后走
void moveBackward() {
digitalWrite(motor1Pin1, LOW);
digitalWrite(motor1Pin2, HIGH);
digitalWrite(motor2Pin1, LOW);
digitalWrite(motor2Pin2, HIGH);
}
//左转
void turnLeft() {
digitalWrite(motor1Pin1, LOW);
digitalWrite(motor1Pin2, HIGH);
digitalWrite(motor2Pin1, HIGH);
digitalWrite(motor2Pin2, LOW);
}
//右转
void turnRight() {
digitalWrite(motor1Pin1, HIGH);
digitalWrite(motor1Pin2, LOW);
digitalWrite(motor2Pin1, LOW);
digitalWrite(motor2Pin2, HIGH);
}
```
注意:这只是一个基础的代码示例,实际运行时需要根据具体的小车硬件进行调整。
阅读全文