/* Electronic Safe * by Akshay Momaya * for Mission Critical ( youtube ) * subscribe for more arduino projects https://www.youtube.com/channel/UCM6rbuieQBBLFsxszWA85AQ?sub_confirmation=1 * * */ const int trigPin = 9; const int echoPin = 10; long duration; int distance; void setup() { pinMode(trigPin,OUTPUT); // Sets the trigPin as an Output pinMode(echoPin,INPUT); // Sets the echoPin as an Input Serial.begin(9600); // Starts the serial communication } void loop() { digitalWrite(trigPin, LOW); // Clears the trigPin delayMicroseconds(2); // Sets the trigPin on HIGH state for 10 micro seconds digitalWrite(trigPin, HIGH); // write the Trigger for MCU on Module delayMicroseconds(10); digitalWrite(trigPin, LOW); // Reads the echoPin, returns the sound wave travel time in microseconds duration = pulseIn(echoPin, HIGH); // Using the Formula distance= duration*0.034/2; Serial.print("Distance: "); Serial.println(distance);// Prints the distance on the Serial Monitor }