int trigPin = A0;
int echoPin = A3;
int LED1 = 2;
int LED2 = 3;
int LED3 = 4;
int LED4 = 5;
int LED5 = 6;
int LED6 = 7;
int LED7 = 8;
int LED8 = 9;
int LED9 = 10;
int LED10 = 11;
float Speed = 0.0343; // Sound speed at cm/us
long duration, distance;
void setup()
{
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
pinMode(LED1, OUTPUT);
pinMode(LED2, OUTPUT);
pinMode(LED3, OUTPUT);
pinMode(LED4, OUTPUT);
pinMode(LED5, OUTPUT);
pinMode(LED6, OUTPUT);
pinMode(LED7, OUTPUT);
pinMode(LED8, OUTPUT);
pinMode(LED9, OUTPUT);
pinMode(LED10, OUTPUT);
digitalWrite (LED1 , LOW);
digitalWrite (LED2 , LOW);
digitalWrite (LED3 , LOW);
digitalWrite (LED4 , LOW);
digitalWrite (LED5 , LOW);
digitalWrite (LED6 , LOW);
digitalWrite (LED7 , LOW);
digitalWrite (LED8 , LOW);
digitalWrite (LED9 , LOW);
digitalWrite (LED10 , LOW);
}
void loop()
{
digitalWrite(trigPin, LOW); // Make sure that the TRIG is deactivated
delayMicroseconds(2); // Make sure that the TRIG is in LOW
digitalWrite(trigPin, HIGH); // Activate the output pulse
delayMicroseconds(10); // Wait for 10µs, the pulse remains active during this time
digitalWrite(trigPin, LOW); //Stop the pulse and wait for ECHO
duration = pulseIn(echoPin, HIGH) ; // pulseIn measures the time )
distance = Speed* duration / 2; //Divide by 2 because we want to have only the “go” time, not the “go and back” time
if ( distance < 40)
{
digitalWrite (LED1 , HIGH);
}
else
{
digitalWrite (LED1 , LOW);
}
if ( distance < 80)
{
digitalWrite (LED2 , HIGH);
}
else
{
digitalWrite (LED2 , LOW);
}
if ( distance < 120)
{
digitalWrite (LED3 , HIGH);
}
else
{
digitalWrite (LED3 , LOW);
}
if ( distance < 160)
{
digitalWrite (LED4 , HIGH);
}
else
{
digitalWrite (LED4 , LOW);
}
if ( distance < 200)
{
digitalWrite (LED5 , HIGH);
}
else
{
digitalWrite (LED5 , LOW);
}
if ( distance < 240)
{
digitalWrite (LED6 , HIGH);
}
else
{
digitalWrite (LED6 , LOW);
}
if ( distance < 280)
{
digitalWrite (LED7 , HIGH);
}
else
{
digitalWrite (LED7 , LOW);
}
if ( distance < 320)
{
digitalWrite (LED8 , HIGH);
}
else
{
digitalWrite (LED8 , LOW);
}
if ( distance < 360)
{
digitalWrite (LED9 , HIGH);
}
else
{
digitalWrite (LED9 , LOW);
}
if ( distance < 400)
{
digitalWrite (LED10 , HIGH);
}
else
{
digitalWrite (LED10 , LOW);
}
}