#define PIN_TRIG 8
#define PIN_ECHO 7

#define ledg 4
#define ledy 5
#define ledr 6

int stat;

void setup() 
{
  Serial.begin(115200);

  pinMode(ledg, OUTPUT);
  pinMode(ledy, OUTPUT);
  pinMode(ledr, OUTPUT);

  pinMode(PIN_TRIG, OUTPUT);
  pinMode(PIN_ECHO, INPUT);
}

void loop() 
{
  digitalWrite(PIN_TRIG, HIGH);
  delayMicroseconds(10);
  digitalWrite(PIN_TRIG, LOW);

  int duration = pulseIn(PIN_ECHO, HIGH);
  duration = duration / 58;
  Serial.println(duration);

  if(stat <= 10 && duration <= 15)
  {
    digitalWrite(ledr, 1);
    stat = stat + 1;
    delay(100);
  }

  if(stat == 10)
  {
    digitalWrite(ledr, 0);
    digitalWrite(ledg, 1);
    delay(1000);
    digitalWrite(ledg, 0);
  }

  if(duration >= 15)
  {
    stat = 0;
  }

  delay(100);
}