//int trig=8; int echo=7;
//#define trig 8 //here instead of trig 8 will be replaced //but its not variables //just names
// we can reduce the number of variable used
#define trig1 8
#define echo1 7
#define trig2 5
#define echo2 4
void setup() {
// put your setup code here, to run once:
pinMode(trig1, OUTPUT); pinMode(echo1, INPUT);
pinMode(trig2, OUTPUT); //pwm pin not a problem
pinMode(echo2, INPUT);
Serial.begin(9600);
}
void loop() {
// triggering
//digitalWrite(trig, HIGH); delayMicroseconds(10); //we have set atleast 10 microseconds
//digitalWrite(trig, LOW);
//echo //the reflection of sound
//unsigned long t = pulseIn(echo, HIGH); //duration in microseconds //long more size
//select the best suitable data type
//distance calculation
//float d = 340 *(t/1000000.0)/2.0;
//here 340 is speed of sound in air //1000000 is to convert to seconds
//2 is divided for that 2 distance(from start to reflecting point and back to start)
//Serial.println(d);
//delay(100);
Serial.print(getDistance(trig1, echo1));
Serial.print(" ");
Serial.println(getDistance(trig2, echo2));
delay(100);
}
float getDistance(int trig , int echo)
{
digitalWrite(trig, HIGH); delayMicroseconds(10); //we have set atleast 10 microseconds
digitalWrite(trig, LOW);
unsigned long t = pulseIn(echo, HIGH); //duration in microseconds //long more size
float d = 340 *(t/1000000.0)/2.0;
return d;
}