keyestudio
www.keyestudio.com
if (distance >= maximumRange || distance <= minimumRange){
/* Send a negative number to computer and Turn LED ON
to indicate "out of range" */
Serial.println("-1");
digitalWrite(LEDPin, HIGH);
}
else {
/* Send the distance to the computer using Serial protocol, and
turn LED OFF to indicate successful reading. */
Serial.println(distance);
digitalWrite(LEDPin, LOW);
}
//Delay 50ms before next reading.
delay(50);
}
*******************************************************************************
Result
After connection and uploading, when ultrasonic sensor senses obstacle within sensing area, it is measuring the distance between itself and obstacle and the value of
distance is displayed on serial monitor as shown in bellow figure.