int trigPin =9;
int echoPin = 10;
int ldrPin = A0;
int led1 = 11;
int led2 = 6;
int led3 = 5;
int led4 = 3;
int led5 = 13;
long duration;
int distance;
int safedistance;
int light;
void setup(){
pinMode(trigPin,OUTPUT);
pinMode(echoPin,INPUT);
pinMode(led1,OUTPUT);
pinMode(led2,OUTPUT);
pinMode(led3,OUTPUT);
pinMode(led4,OUTPUT);
pinMode(led5,OUTPUT);
pinMode(ldrPin,INPUT);
Serial.begin(9600);
}
void loop(){
digitalWrite(trigPin,LOW);
delayMicroseconds(2);
digitalWrite(trigPin,HIGH);
delayMicroseconds(10);
digitalWrite(trigPin,LOW);
duration = pulseIn(echoPin,HIGH);
distance = duration*0.034/2;
safedistance = distance;
int light = analogRead(ldrPin);
if (light<=400)
{
if (safedistance<=5)
{digitalWrite(led1,HIGH);
delay(50);
digitalWrite(led2,HIGH);
delay(70);
digitalWrite(led3,HIGH);
delay(90);
digitalWrite(led4,HIGH);
delay(110);
digitalWrite(led5,HIGH);
delay(130);}
else if(safedistance>5)
{digitalWrite(led1,LOW);
delay(10);
digitalWrite(led2,LOW);
delay(20);
digitalWrite(led3,LOW);
delay(30);
digitalWrite(led4,LOW);
delay(40);
digitalWrite(led5,LOW);
delay(50);}
}
delay(200);
}