Categories
Break This Glass

Distance

Checkbox 4

The sensor works with a modified program

Modify the example provided with the library, to create a visual feedback using the on-board LED.

In the setup function, identify the following line:

  Serial.begin(9600);

Add the following line after that:

  pinMode(LED_BUILTIN, OUTPUT); // set up the builtin LED

Then, identify the last lines within the loop function:

  Serial.print(" cm");
  delay(250);

After those lines, add the following:

// turn the LED if the distance is below 12 cm
if (RangeInCentimeters < 12) {
  digitalWrite(LED_BUILTIN, HIGH); // turn the LED on
} else {
  digitalWrite(LED_BUILTIN, LOW); // turn the LED off
}

Upload the modified code.

Upload the sketch to the board.

You should see a green pane at the bottom of the screen, with a success message:

Success. Saved on your online Sketchbook and done uploading […]

If all functions correctly, when the distance from the sensor to an object is less than 12 cm, the on-board LED should turn on.

Observe the on-board LED turning on when the distance to the sensor is less than 12 cm.