#include #define trigPin 7 #define echoPin 6 #define servoPin 5 float duration, distance; Servo Servo1; void setup() { Serial.begin (9600); pinMode(trigPin, OUTPUT); pinMode(echoPin, INPUT); Servo1.attach(servoPin); } void loop() { //Clears the 'trigPin' condition digitalWrite(trigPin, LOW); delayMicroseconds(2); //Sets the 'trigPin' HIGH (ACTIVE) for 10 microseconds digitalWrite(trigPin, HIGH); delayMicroseconds(10); digitalWrite(trigPin, LOW); //Reads the 'echoPin', returns the sound wave travel time in microseconds duration = pulseIn(echoPin, HIGH); //Calculating the 'distance' // Use 343 metres per second as speed of sound distance = (duration * .0343) / 2; //Display the 'distance' in cm on the serial monitor Serial.print(distance); Serial.println(" cm"); delay(500); //Turn on LED if obstacle is detected and vice versa if (distance < 10) { Servo1.write(90); } else { Servo1.write(0); } }