// if values are constant use #define if u want to change use a variable
#define trigger1 8
#define trigger2 5
//here trigger used to start transmistion (send ultasonic sound/signal)
#define echo1 7
#define echo2 4
// reflection of the object
void setup() {
// put your setup code here, to run once:
pinMode(trigger1, OUTPUT); pinMode(echo1, INPUT);
pinMode(trigger2, OUTPUT); pinMode(echo2, INPUT);
Serial.begin(9600);
}
void loop() {
// put your main code here, to run repeatedly:
Serial.print(getDistance(trigger1, echo1));
Serial.print(" ");
Serial.println(getDistance (trigger2, echo2));
delay(100);
}
float getDistance(int trig, int echo)
{
digitalWrite(trig, HIGH);
delayMicroseconds(10);
digitalWrite(trig, LOW);
//what happened here for 10 milliseconds it sends a signal and top the signal (ultra sonic sound)
unsigned long t = pulseIn(echo, HIGH);
//returns the dustion in milliseconds
float d = 340 * (t/1000000.0)/2.0;
return d;
}