const int trig = 12;
int echo = 1;
long duration, dist, average;
long aver[3];
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;
void setup()
{
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);
}
void measure()
{
digitalWrite(trig, LOW);
delayMicroseconds(5);
digitalWrite(trig, HIGH);
delayMicroseconds(15);
digitalWrite(trig, LOW);
pinMode(echo, INPUT);
duration = pulseIn(echo, HIGH);
dist = (duration/2) / 29.1;
}
void loop() {
for (int i=0;i<=2;i++) {
measure();
aver[i]=dist;
delay(50);
}
dist=(aver[0]+aver[1]+aver[2])/3;
if ( dist<=10 ) {
digitalWrite(led10,1);
digitalWrite(led9,1);
digitalWrite(led8,1);
digitalWrite(led7,1);
digitalWrite(led6,1);
digitalWrite(led5,1);
digitalWrite(led4,1);
digitalWrite(led3,1);
digitalWrite(led2,1);
digitalWrite(led1,1);
}
else if (dist<=30){
digitalWrite(led10,1);
digitalWrite(led9,1);
digitalWrite(led8,1);
digitalWrite(led7,1);
digitalWrite(led6,1);
digitalWrite(led5,1);
}
else{
digitalWrite(led10,0);
digitalWrite(led9,0);
digitalWrite(led8,0);
digitalWrite(led7,0);
digitalWrite(led6,0);
digitalWrite(led5,0);
digitalWrite(led4,0);
digitalWrite(led3,0);
digitalWrite(led2,0);
digitalWrite(led1,0);
}
}