//Aditya-with love from INDIA// #define trigPin 7//ultra pin #define echoPin 6// ultra pin #define motorOne 3//output for mosfet motor driver #define led 12//backlight int motor1 = 250; void setup() { pinMode(trigPin, OUTPUT); pinMode(echoPin, INPUT); pinMode(motorOne, OUTPUT); pinMode(motorTwo, OUTPUT); pinMode(led,OUTPUT); } void loop() { long duration, distance; digitalWrite(trigPin, LOW); delayMicroseconds(2); digitalWrite(trigPin, HIGH); delayMicroseconds(10); digitalWrite(trigPin, LOW); duration = pulseIn(echoPin, HIGH); distance = (duration/2) / 29.1; if (distance <= 30) { motor1 = 254; } if (distance < 25) { motor1 = 200; } if (distance < 20) { motor1 = 150; } if (distance < 15) { motor1= 100; } if (distance < 10) { motor1= 50; } if (distance < 5) { motor1 = 0; digitalWrite(led,HIGH); }else{ digitalWrite(led,LOW); } if (distance > 30 || distance <= 0){ analogWrite(motorOne,254); } else { analogWrite(motorOne,motor1); } delay(500); }