distance_sensor

The distance_sensor module contains functions to interact with an attached Distance Sensor (e.g. LEGO part 45604) and write code that reacts to specific distances or lights up the device LEDs in different ways.

To use the distance_sensor module, add the following import statement to your project:

import distance_sensor

All functions in the module should be called inside the distance_sensor module as a prefix like so:

distance_sensor.distance(port.A)

Functions

clear(port)

Turn off all lights on the Distance Sensor connected to port.

distance(port)

Get the distance (in millimeters) captured by the Distance Sensor connected to port.

get_pixel(port, x, y)

Get the intensity of a specific light on the Distance Sensor connected to port as a percentage (0–100).

set_pixel(port, x, y, intensity)

Set the intensity of a specific light on the Distance Sensor connected to port.

show(port, pixels)

Set all lights at the same time.

distance_sensor.clear(port)

Turn off all lights on the Distance Sensor connected to port.

Parameters:

port (int) – A port from the port submodule in the hub module

Return type:

None

distance_sensor.distance(port)

Get the distance (in millimeters) captured by the Distance Sensor connected to port. If the Distance Sensor cannot read a valid distance, it will return -1.

Parameters:

port (int) – A port from the port submodule in the hub module

Return type:

int

distance_sensor.get_pixel(port, x, y)

Get the intensity of a specific light on the Distance Sensor connected to port as a percentage (0–100).

Parameters:
  • port (int) – A port from the port submodule in the hub module

  • x (int) – The pixel column (range 0–1)

  • y (int) – The pixel row (range 0–1)

Return type:

int

distance_sensor.set_pixel(port, x, y, intensity)

Set the intensity of a specific light on the Distance Sensor connected to port.

Parameters:
  • port (int) – A port from the port submodule in the hub module

  • x (int) – The pixel column (range 0–1)

  • y (int) – The pixel row (range 0–1)

  • intensity (int) – Brightness of the light as a percentage (0–100)

Return type:

None

distance_sensor.show(port, pixels)

Set all lights at the same time.

from hub import port
import distance_sensor

# Update all pixels on Distance Sensor using the show function

# Create a list with 4 identical intensity values
pixels = [100] * 4

# Update all pixels to show same intensity
distance_sensor.show(port.A, pixels)
Parameters:
  • port (int) – A port from the port submodule in the hub module

  • pixels (list[int]) – A list containing intensity values for all 4 pixels.

Return type:

None