HC SR04 - detection 5 seconds

I want to detect if an obstacle is for 5 seconds in front of my sensor then have to happen something... but i'm stuck... thanks for any help

int led = 12;
int trigger = 5;
int echo = 4;

void setup() {

  pinMode(led, OUTPUT);
  pinMode(trigger, OUTPUT);
  pinMode(echo, INPUT);


  // put your setup code here, to run once:

}

void loop() {

  unsigned long startTime = 0;

  digitalWrite(trigger, LOW);

  digitalWrite(trigger, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigger, LOW);

  long duration = pulseIn(echo, HIGH);
  long distance = 0.034 * duration / 2;

  if (distance > 10)
  {
    startTime = millis();
  }
  else {

    if (millis() - startTime >= 5000) 
    {
      ....
    }
  }
}

Assuming that you are trying to trigger something if distance stays below 10 for 5 seconds then your logic is basically OK- whatever you place in the "if (millis() - startTime >= 5000)" clause will get executed after 5 seconds. However as it looks now the clause will trigger continuously once 5 seconds has elapsed- if this is not what you want then add a flag or set starttime to be far in the future once you execute the trigger.

I have to find a way to detect an obstacle after 5 seconds that it's in front of sensor.
If put my hand in front of it and after 5 seconds play a song, after 10 seconds play another song, etc...

What is your code to play the song? Are you going to generate tones directly using the arduino tone() function or are you using the arduino to trigger something else?