#define Tpin 11 #define Epin 10 #define motor 7 #define buzz 8 void setup() { pinMode(Tpin, OUTPUT); pinMode(Epin, INPUT); pinMode(motor, OUTPUT); pinMode(buzz,OUTPUT); } void loop() { long x, dist; digitalWrite(Tpin, LOW); delayMicroseconds(20); digitalWrite(Tpin, HIGH); delayMicroseconds(10); digitalWrite(Tpin, LOW); timetaken = pulseIn(Epin, HIGH); dist = ((timetaken/2)*340)/10000;//By the formula- distance=(time/2)*speed of sound if (dist< 100) //Stating that if distance is lees than 100, motor and buzzer are activated { digitalWrite(motor,HIGH); digitalWrite(buzz,HIGH); } else { digitalWrite(motor,LOW);// Stating that if distance greater than 100,motor and buzzer don't work digitalWrite(buzz,LOW); } delay(500); }