user28069: My objective is if ultrasonic readings ranging between 1 to 10 cm and no distance change for 10 seconds or more, a LED will turn on. Here is my current code.
#define trigPin 7
#define echoPin 8
#define led 13
#define led2 12

long duration, distance, interval = 4000;
int detection = 0;

void setup() {
   Serial.begin (9600);
   pinMode(trigPin, OUTPUT);
   pinMode(echoPin, INPUT);
   pinMode(led, OUTPUT);
   pinMode(led2, OUTPUT);
   detection = millis();
}

void loop() {

   ultraSonic();

   if (distance > 0 && distance < 10 && (millis() - detection >= interval)) {
   digitalWrite(led, HIGH);
   digitalWrite(led2, LOW);
   } else {
   digitalWrite(led, LOW);
   digitalWrite(led2, HIGH);
   }

   Serial.print(millis());
   Serial.print(" - ");
   Serial.println(distance);

   delay(250);
}

void ultraSonic() {
   digitalWrite(trigPin, LOW);
   delayMicroseconds(60);
   digitalWrite(trigPin, HIGH);
   delayMicroseconds(60);
   digitalWrite(trigPin, LOW);
   duration = pulseIn(echoPin, HIGH);
   distance = (duration / 2) / 29.1;
}

Sorry for my bad english.
Cheers.

user28090: void loop() {

   ultraSonic();

   if ((distance > 0) && (distance =< 10)) {
   digitalWrite(led, HIGH);
   digitalWrite(led2, LOW); // turning LED on
   } else {
   digitalWrite(led, LOW);
   digitalWrite(led2, HIGH); // turning LED off
   }

   Serial.print(millis());
   Serial.print(" - ");
   Serial.println(distance);

   delay(250);
}


user28033: Based on previous answer and your comment, maybe you can try this one
I assume if the range is ranging 1-10 cm for 10 secs or more the led will turn on:   
void setup() {
   Serial.begin (9600);
   pinMode(trigPin, OUTPUT);
   pinMode(echoPin, INPUT);
   pinMode(led, OUTPUT);
   pinMode(led2, OUTPUT);
}
int togle = 0;

void loop() {

   ultraSonic();

   if ((distance >= 1) && (distance =< 10)) //dont use (distance>0) 
   //there are possibility you get 0.xx distance
   {
     Serial.print("Distance on range - ");
     if (toggle == 0) 
       {
        toggle=1; 
        detection=millis();
        Serial.println("last detection outside range");
       } else
     if (toggle == 1) 
     {
        Serial.print("last detection in range - ");
        if ((millis()-detection) >= interval)
            {
             Serial.println("objective complete");
            }
        else Serial.println("not enough interval");
     }
   } else
   {
     //turn off the led
     Serial.println("Distance out of range");
     toggle = 0;
   }

}

void ultraSonic() {
   digitalWrite(trigPin, LOW);
   delayMicroseconds(60);
   digitalWrite(trigPin, HIGH);
   delayMicroseconds(60);
   digitalWrite(trigPin, LOW);
   duration = pulseIn(echoPin, HIGH);
   distance = (duration / 2) / 29.1;
}    

Feel free to tell if something is not right

28069: <p>My objective is if ultrasonic readings ranging between 1 to 10 cm and no distance change for 10 seconds or more, a LED will turn on. Here is my current code.</p>

<pre><code>#define trigPin 7
#define echoPin 8
#define led 13
#define led2 12

long duration, distance, interval = 4000;
int detection = 0;

void setup() {
   Serial.begin (9600);
   pinMode(trigPin, OUTPUT);
   pinMode(echoPin, INPUT);
   pinMode(led, OUTPUT);
   pinMode(led2, OUTPUT);
   detection = millis();
}

void loop() {

   ultraSonic();

   if (distance &gt; 0 &amp;&amp; distance &lt; 10 &amp;&amp; (millis() - detection &gt;= interval)) {
   digitalWrite(led, HIGH);
   digitalWrite(led2, LOW);
   } else {
   digitalWrite(led, LOW);
   digitalWrite(led2, HIGH);
   }

   Serial.print(millis());
   Serial.print(" - ");
   Serial.println(distance);

   delay(250);
}

void ultraSonic() {
   digitalWrite(trigPin, LOW);
   delayMicroseconds(60);
   digitalWrite(trigPin, HIGH);
   delayMicroseconds(60);
   digitalWrite(trigPin, LOW);
   duration = pulseIn(echoPin, HIGH);
   distance = (duration / 2) / 29.1;
}
</code></pre>

<p>Sorry for my bad english.
Cheers.</p>
 28090: <pre><code>void loop() {

   ultraSonic();

   if ((distance &gt; 0) &amp;&amp; (distance =&lt; 10)) {
   digitalWrite(led, HIGH);
   digitalWrite(led2, LOW); // turning LED on
   } else {
   digitalWrite(led, LOW);
   digitalWrite(led2, HIGH); // turning LED off
   }

   Serial.print(millis());
   Serial.print(" - ");
   Serial.println(distance);

   delay(250);
}
</code></pre>
 28033: <p>Based on previous answer and your comment, maybe you can try this one<br>
I assume if the range is ranging 1-10 cm for 10 secs or more the led will turn on:   </p>

<pre><code>void setup() {
   Serial.begin (9600);
   pinMode(trigPin, OUTPUT);
   pinMode(echoPin, INPUT);
   pinMode(led, OUTPUT);
   pinMode(led2, OUTPUT);
}
int togle = 0;

void loop() {

   ultraSonic();

   if ((distance &gt;= 1) &amp;&amp; (distance =&lt; 10)) //dont use (distance&gt;0) 
   //there are possibility you get 0.xx distance
   {
     Serial.print("Distance on range - ");
     if (toggle == 0) 
       {
        toggle=1; 
        detection=millis();
        Serial.println("last detection outside range");
       } else
     if (toggle == 1) 
     {
        Serial.print("last detection in range - ");
        if ((millis()-detection) &gt;= interval)
            {
             Serial.println("objective complete");
            }
        else Serial.println("not enough interval");
     }
   } else
   {
     //turn off the led
     Serial.println("Distance out of range");
     toggle = 0;
   }

}

void ultraSonic() {
   digitalWrite(trigPin, LOW);
   delayMicroseconds(60);
   digitalWrite(trigPin, HIGH);
   delayMicroseconds(60);
   digitalWrite(trigPin, LOW);
   duration = pulseIn(echoPin, HIGH);
   distance = (duration / 2) / 29.1;
}    
</code></pre>

<p>Feel free to tell if something is not right</p>
