代码如下


手机上输入31,小车应该向前走,但是没有动静

有人知道为什么吗?
关注让 “紫雾凌寒” 来帮你回答,本回答结合 DeepSeek
如果还有问题,请评论或点击头像私信,如果有帮助到您, 请关注、采纳、点赞
#include <SoftwareSerial.h>
SoftwareSerial mySerial(10, 11); // RX, TX
void setup() {
Serial.begin(9600);
mySerial.begin(9600);
}
void loop() {
if (mySerial.available()) {
char receivedChar = mySerial.read();
Serial.print("Received: ");
Serial.println(receivedChar);
if (receivedChar == '31') {
// 这里添加控制小车前进的代码,比如控制电机驱动引脚输出高电平
digitalWrite(motorPin1, HIGH);
digitalWrite(motorPin2, LOW);
digitalWrite(motorPin3, HIGH);
digitalWrite(motorPin4, LOW);
}
}
}
- 上述代码中,`motorPin1`、`motorPin2`、`motorPin3`、`motorPin4`需要根据实际连接小车电机驱动的引脚进行修改。并且要确保在使用`digitalWrite`函数之前已经正确定义了这些引脚为输出模式,例如:
const int motorPin1 = 2;
const int motorPin2 = 3;
const int motorPin3 = 4;
const int motorPin4 = 5;
void setup() {
Serial.begin(9600);
mySerial.begin(9600);
pinMode(motorPin1, OUTPUT);
pinMode(motorPin2, OUTPUT);
pinMode(motorPin3, OUTPUT);
pinMode(motorPin4, OUTPUT);
}
希望以上解答对您有所帮助。如果您有任何疑问,欢迎在评论区提出。