我想将4个超声波传感器与单个Arduino板一起使用。在当前代码中,这是不可能的,因为我正在使用冻结Arduino的延迟功能。
我尝试使用在线帮助代码,但无法改善
使用延迟功能没有错误消息,但是当我使用Millis时有
void loop() {
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = (duration*.0343)/2;
Serial.print("Distance: ");
Serial.println(distance);
time_now = 0;
TurnOn = false;
while ((distance >=10) && (distance<= 50))
{
digitalWrite(valvePin, LOW);
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = (duration*.0343)/2;
Serial.print("Inside zone :");
Serial.println(distance);
time_now ++;
delay(1000);
if(time_now == 5){
Serial.print("5 sec");
delay(1000);
TurnOn = true;
}
}
if(TurnOn){
Serial.print("Turn On");
digitalWrite(valvePin, HIGH);
delay(5000);
digitalWrite(valvePin, LOW);
}
}