我和我的朋友正在研究一个关于arduino的小项目。我们正在尝试使用Arduino和蓝牙模块制作遥控车。除了一个,一切都很顺利。当我们向arduino发送信号以使汽车在特定方向上移动时,它会迟到。接近8到10秒。无论如何要解决它吗?我们使用串口显示检查了延迟。该命令收到很大的延迟。代码如下。我们使用的是Arduino Uno,蓝牙模块是HC-06,Arduino IDE版本是1.6.7。我们正在使用Android应用程序从手机发送命令。
#define trigPin 7
#define echoPin 6
#define yellowLed 10
#define greenLed 9
int motor_left[] = {2, 3};
int motor_right[] = {4, 5};
int vSpeed=255;
long previousMillis = -1000*10;
char state = 'S';
void setup() {
// put your setup code here, to run once:
Serial.begin (9600);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
pinMode(greenLed, OUTPUT);
pinMode(yellowLed, OUTPUT);
for(int i = 0; i < 2; i++){
pinMode(motor_left[i], OUTPUT);
pinMode(motor_right[i], OUTPUT);
}
}
void loop() {
// put your main code here, to run repeatedly:
long duration, inches, cm;
if(Serial.available() > 0){
state = Serial.read();
}
// The PING))) is triggered by a HIGH pulse of 2 or more microseconds.
// Give a short LOW pulse beforehand to ensure a clean HIGH pulse:
digitalWrite (trigPin, LOW);
delayMicroseconds (2);
digitalWrite (trigPin, HIGH);
delayMicroseconds (10);
digitalWrite (trigPin, LOW);
duration = pulseIn (echoPin, HIGH);
inches = microsecondsToInches (duration);
cm = microsecondsToCentimeters (duration);
if (cm <= 10 || state == 'S') {
motor_stop ();
} else {
motor_ready ();
}
delay(100);
}
void motor_stop(){
Serial.print (state);
Serial.println();
digitalWrite(motor_left[0], LOW);
digitalWrite(motor_left[1], LOW);
digitalWrite(motor_right[0], LOW);
digitalWrite(motor_right[1], LOW);
digitalWrite (yellowLed, HIGH);
digitalWrite (greenLed, LOW);
delay(25);
}
void motor_ready () {
Serial.print (state);
Serial.println();
if (state == '0') {
vSpeed = 0;
} else if (state == '1') {
vSpeed = 75;
} else if (state == '2') {
vSpeed = 150;
} else if (state == '3') {
vSpeed = 200;
} else if (state == '4') {
vSpeed = 255;
}
if (state == 'F') {
driveForward ();
} else if (state == 'G') {
driveForwardLeft ();
} else if (state == 'I') {
driveForwardRight ();
} else if (state == 'B') {
driveBackward ();
} else if (state == 'H') {
driveBackwardLeft ();
} else if (state == 'J') {
driveBackwardRight ();
}
digitalWrite (yellowLed, LOW);
digitalWrite (greenLed, HIGH);
}
void driveForward () {
analogWrite(motor_left [0], vSpeed); analogWrite(motor_left [1], 0);
analogWrite(motor_right [0], vSpeed); analogWrite(motor_right [1], 0);
}
void driveForwardLeft () {
analogWrite(motor_left [0], 0); analogWrite(motor_left [1], 0);
analogWrite(motor_right [0], vSpeed); analogWrite(motor_right [1], 0);
}
void driveForwardRight () {
analogWrite(motor_left [0], vSpeed); analogWrite(motor_left [1], 0);
analogWrite(motor_right [0], 0); analogWrite(motor_right [1], 0);
}
void driveBackward () {
analogWrite(motor_left [0], 0); analogWrite(motor_left [1], vSpeed);
analogWrite(motor_right [0], 0); analogWrite(motor_right [1], vSpeed);
}
void driveBackwardLeft () {
analogWrite(motor_left [0], 0); analogWrite(motor_left [1], 0);
analogWrite(motor_right [0], 0); analogWrite(motor_right [1], vSpeed);
}
void driveBackwardRight () {
analogWrite(motor_left [0], 0); analogWrite(motor_left [1], vSpeed);
analogWrite(motor_right [0], 0); analogWrite(motor_right [1], 0);
}
long microsecondsToInches(long microseconds) {
return microseconds / 74 / 2;
}
long microsecondsToCentimeters(long microseconds) {
return microseconds / 29 / 2;
}