#define trig 15
#define echo 19
int threshold=45;
void setup(){
  pinMode(2,OUTPUT);
  Serial.begin(115200);
  pinMode(trig, OUTPUT);
  pinMode(echo, INPUT);


}

void loop(){
  digitalWrite(trig, HIGH);
  delayMicroseconds(10);
  digitalWrite(trig, LOW);
  int time = pulseIn(echo,HIGH);
  int d = (time*0.034)/8;
  if (d == 2) {
    analogWrite(2, 255);
  }
  else if(d==3){
    analogWrite(2,250);
  }
  else if(d==4){
    analogWrite(2,240);
  }
  else if(d==5){
    analogWrite(2,230);
  }
  else if(d==6){
    analogWrite(2,220);
  }
  else if(d==7){
    analogWrite(2,200);
  }
  else if(d==8){
    analogWrite(2,180);
  }
  else if(d==9){
    analogWrite(2,160);
  }
  else if(d==10){
    analogWrite(2,140);
  }
  else if(d==11){
    analogWrite(2,120);
  }
  else if(d==12){
    analogWrite(2,100);
  }
  else if(d==13){
    analogWrite(2,80);
  }
  else if(d==14){
    analogWrite(2,60);
  }
  else if(d==15){
    analogWrite(2,50);
  }
  else if(d==16){
    analogWrite(2,40);
  }
  else if(d==17){
    analogWrite(2,30);
  }
  else if(d==18){
    analogWrite(2,20);
  }
  else if(d==19){
    analogWrite(2,10);
  }
  else if(d==20){
    analogWrite(2,5);
  }

  else if(d>=21){
    analogWrite(2,0);
  }
  else {
    analogWrite(18, 0);
  }
  delay(10);
  Serial.print("Distance: ");
  Serial.print(d);
  Serial.println("cm");
}