const int ledPin0 = 0; const int ledPin1 = 1; const int ledPin2 = 2; const int ledPin3 = 4; const int ledPin4 = 3; const int trigPin = 27; const int echoPin = 28; const int LP = 16; // defines variables double duration; double distance; void setup() { pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output pinMode(echoPin, INPUT); // Sets the echoPin as an Input pinMode(LP,OUTPUT); //LED pinMode(ledPin0, OUTPUT); pinMode(ledPin1, OUTPUT); pinMode(ledPin2, OUTPUT); pinMode(ledPin3, OUTPUT); pinMode(ledPin4, OUTPUT); } void loop() { // Clears the trigPin digitalWrite(trigPin, LOW); delayMicroseconds(2); // Sets the trigPin on HIGH state for 10 micro seconds 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 distance= duration*0.034/2; if (distance<3) { digitalWrite(ledPin0, LOW); digitalWrite(ledPin1, LOW); digitalWrite(ledPin2, LOW); digitalWrite(ledPin3, LOW); digitalWrite(ledPin4, LOW); delay(250); } if (distance>=3 && distance<4) { digitalWrite(ledPin0, HIGH); digitalWrite(ledPin1, LOW); digitalWrite(ledPin2, LOW); digitalWrite(ledPin3, LOW); digitalWrite(ledPin4, LOW); delay(250); } if (distance>=4 && distance<5) { digitalWrite(ledPin0, HIGH); digitalWrite(ledPin1, HIGH); digitalWrite(ledPin2, LOW); digitalWrite(ledPin3, LOW); digitalWrite(ledPin4, LOW); delay(250); } if (distance>=5 && distance<6) { digitalWrite(ledPin0, HIGH); digitalWrite(ledPin1, HIGH); digitalWrite(ledPin2, HIGH); digitalWrite(ledPin3, LOW); digitalWrite(ledPin4, LOW); delay(250); } if (distance>=6 && distance<7) { digitalWrite(ledPin0, HIGH); digitalWrite(ledPin1, HIGH); digitalWrite(ledPin2, HIGH); digitalWrite(ledPin3, HIGH); digitalWrite(ledPin4, LOW); delay(250); } if (distance>=7 && distance<8) { digitalWrite(ledPin0, HIGH); digitalWrite(ledPin1, HIGH); digitalWrite(ledPin2, HIGH); digitalWrite(ledPin3, HIGH); digitalWrite(ledPin4, HIGH); delay(250); } }