#define Trig1 6
#define Echo1 5
#define Trig2 3
#define Echo2 2
#define Led 11
void setup() {
// put your setup code here, to run once:
pinMode(Trig1,OUTPUT);
pinMode(Echo1,INPUT);
pinMode(Trig2,OUTPUT);
pinMode(Echo2,INPUT);
Serial.begin(115200);
}
int readPerson() {
digitalWrite(Trig1, LOW);
delayMicroseconds(2);
digitalWrite(Trig1, HIGH);
delayMicroseconds(10);
digitalWrite(Trig1, LOW);
int duration1 = pulseIn(Echo1, HIGH);
int distance1= duration1 * 0.034 / 2;
digitalWrite(Trig2, LOW);
delayMicroseconds(2);
digitalWrite(Trig2, HIGH);
delayMicroseconds(10);
digitalWrite(Trig2, LOW);
int duration2 = pulseIn(Echo2, HIGH);
int distance2= duration2 * 0.034 / 2;
Serial.println(distance1 - distance2);
return distance1 - distance2;
}
void loop() {
// put your main code here, to run repeatedly:
int Differ = readPerson();
if (abs(Differ) >100 ){
if (Differ >0){
while (Differ >0 ){
Differ = readPerson();
}
digitalWrite(Led,LOW);
}
else if (Differ <0){
while (Differ < 0 ){
Differ = readPerson();
}
digitalWrite(Led,HIGH);
}
}
}