const int trig = 3;
const int echo = 2;
const int LED1 = 5;
const int LED2 = 6;
const int LED3 = 7;
const int LED4 = 8;
int duration = 0;
int distance = 0;
void setup()
{
pinMode(trig, OUTPUT);
pinMode(echo, INPUT);
pinMode(LED1, OUTPUT);
pinMode(LED2, OUTPUT);
pinMode(LED3, OUTPUT);
pinMode(LED4, OUTPUT);
Serial.begin(9600);
}
void loop()
{
digitalWrite(trig, HIGH);
delayMicroseconds(1000);
digitalWrite(trig, LOW);
duration = pulseIn(echo, HIGH);
distance = duration / 500.0; // Adjusted the conversion factor for more accuracy
//Serial.println(distance);
updateLED(LED1, 30);
updateLED(LED2, 20);
updateLED(LED3, 10);
updateLED(LED4, 5);
Serial.println(" ");
}
void updateLED(int ledPin, int threshold)
{
digitalWrite(ledPin, distance <= threshold);
}