我使用Arduino UNO从事停车系统项目,并希望使用7个超声波传感器,我将所有触发器连接到一个引脚,并将回声连接到7个引脚,并在每个引脚之后设置延迟以避免干扰,但是在仿真中我得到了恒定距离和Proteus虚拟终端棒的打印延迟很多,而且CPU消耗高。
我的代码是:
#include <Servo.h>
Servo myservo;
const byte T1 = 13;
const byte E1 = 6 ;
const byte T2 = 13;
const byte E2 = 7;
const byte T3 = 13;
const byte E3 = 8;
const byte T4 = 13;
const byte E4 = 9;
const byte T5 = 13;
const byte E5 = 10;
const byte T6 = 13;
const byte E6 = 11;
const byte T7 = 13;
const byte E7 = 12;
void setup() {
Serial.begin(9600);
pinMode(T1,OUTPUT);
pinMode (E1,INPUT);
//sensor2
Serial.begin(9600);
pinMode(T2,OUTPUT);
pinMode (E2,INPUT);
//sensor3
Serial.begin(9600);
pinMode(T3,OUTPUT);
pinMode (E3,INPUT);
//sensor4
Serial.begin(9600);
pinMode(T4,OUTPUT);
pinMode (E4,INPUT);
//sensor5
Serial.begin(9600);
pinMode(T5,OUTPUT);
pinMode (E5,INPUT);
//sensor6
Serial.begin(9600);
pinMode(T6,OUTPUT);
pinMode (E6,INPUT);
//sensor7
Serial.begin(9600);
pinMode(T7,OUTPUT);
pinMode (E7,INPUT);
}
void loop() {
long distance1,distance2,distance3,distance4,distance5,distance6,distance7,period1,period2,period3,period4,period5,period6,period7;
int parking1, parking2, parking3, parking4, parking5, parking6, parking7;
// Sensor 1 send
digitalWrite(T1, LOW);
delayMicroseconds(3);
digitalWrite(T1,HIGH);
delayMicroseconds(10);
digitalWrite(T1,LOW);
//Sensor 1 recive
period1 = pulseIn(E1,HIGH);
distance1 = microsecondsToCentimeters(period1);
Serial.print("Slot1: ");
Serial.print(distance1);
Serial.print("cm");
Serial.println();
if (distance1 >50){
parking1=1;
} else if (distance1 <=15){
parking1=0;
}
delay(1000);
// Sensor 2 send
digitalWrite(T2, LOW);
delayMicroseconds(3);
digitalWrite(T2,HIGH);
delayMicroseconds(10);
digitalWrite(T2,LOW);
//Sensor 2 recive
period2 = pulseIn(E2,HIGH);
distance2 = microsecondsToCentimeters(period2);
Serial.print("Slot2: ");
Serial.print(distance2);
Serial.print("cm");
Serial.println();
if (distance2 >50){
parking2=1;
} else if (distance2 <=15){
parking2=0;
}
delay(1000);
// Sensor 3 send
digitalWrite(T3, LOW);
delayMicroseconds(3);
digitalWrite(T3,HIGH);
delayMicroseconds(10);
digitalWrite(T3,LOW);
//Sensor 3 recive
period3 = pulseIn(E3,HIGH);
distance3 = microsecondsToCentimeters(period3);
Serial.print("Slot3: ");
Serial.print(distance3);
Serial.print("cm");
Serial.println();
if (distance3 >50){
parking3=1;
} else if (distance3 <=15){
parking3=0;
}
delay(1000);
// Sensor 4 send
digitalWrite(T4, LOW);
delayMicroseconds(3);
digitalWrite(T4,HIGH);
delayMicroseconds(10);
digitalWrite(T4,LOW);
//Sensor 4 recive
period4 = pulseIn(E4,HIGH);
distance4 = microsecondsToCentimeters(period4);
Serial.print("Slot4: ");
Serial.print(distance4);
Serial.print("cm");
Serial.println();
if (distance4 >50){
parking4=1;
} else if (distance4 <=15){
parking4=0;
}
delay(0100);
// Sensor 5 send
digitalWrite(T5, LOW);
delayMicroseconds(3);
digitalWrite(T5,HIGH);
delayMicroseconds(10);
digitalWrite(T5,LOW);
//Sensor 5 recive
period5 = pulseIn(E5,HIGH);
distance5 = microsecondsToCentimeters(period5);
Serial.print("Slot5: ");
Serial.print(distance5);
Serial.print("cm");
Serial.println();
if (distance5 >50){
parking5=1;
} else if (distance5 <=15){
parking5=0;
}
delay(1000);
// Sensor 6 send
digitalWrite(T6, LOW);
delayMicroseconds(3);
digitalWrite(T6,HIGH);
delayMicroseconds(10);
digitalWrite(T6,LOW);
//Sensor 1 recive
period6 = pulseIn(E6,HIGH);
distance6 = microsecondsToCentimeters(period6);
Serial.print("Slot6: ");
Serial.print(distance6);
Serial.print("cm");
Serial.println();
if (distance6 >50){
parking6=1;
} else if (distance6 <=15){
parking6=0;
}
delay(1000);
// Sensor 7 send
digitalWrite(T7, LOW);
delayMicroseconds(3);
digitalWrite(T7,HIGH);
delayMicroseconds(10);
digitalWrite(T7,LOW);
//Sensor 7 recive
period7 = pulseIn(E7,HIGH);
distance7 = microsecondsToCentimeters(period7);
Serial.print("Slot7: ");
Serial.print(distance7);
Serial.print("cm");
Serial.println();
if (distance7 >50){
parking7=1;
} else if (distance7 <=15){
parking7=0;
}
delay(1000);
}
long microsecondsToCentimeters(long microseconds) // method to covert microsec to centimeters
{
return microseconds / 29 / 2;
}