int echopin = 2; //to receive signal which are coming back from object after reflection
int trigpin =3; //to send ultrasonic signal towards any objects
int Time;
int Distance;
void setup() {
// put your setup code here, to run once:
pinMode(trigpin, OUTPUT);
pinMode(echopin, INPUT);
//9600 : It is the transfer rate(speed), means at 9600 rate transferring of data
//will happen between the connected devices
Serial.begin(9600); //starts transfering data between 2 devices(sensor and arduino)
Serial.print("Ultrasonic Distance:...");
}
void loop()
{
// put your main code here, to run repeatedly:
digitalWrite(trigpin, LOW); //stop the functioning of ultrasonic sensor
digitalWrite(trigpin, HIGH); //starts the functioning of ultrasonic sensor
Time =pulseIn (echopin,HIGH);
//pulseIn() to get the time taken by sound wave to reflect back after hitting the object
Distance= Time * 0.0172; //number 0.0172 is constant(speed)
Serial.print("Time:"); //print time in msec
Serial.print(Time);
Serial.print(" microseconds");
delay(1000);
Serial.print(",Distance:" ); //print distance in cm
Serial.print(Distance);
Serial.println("cm");
delay(1000);
}
//OUTPUT- Click on sensor then you can edit the Distance value to check new Time calculation
// If the distance is higher the duration is higher and vice versa.