How to turn a LED if ultranonic HC-SR04 reading is on defined range for some interval
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);
}