MBC Ranger 8-Way × Pybricks built-in blocks

Read the Ranger 8-Way with Pybricks built-in blocks, no library

On a SPIKE hub the Ranger 8-Way shows up as a LEGO color sensor, so Pybricks' own color sensor blocks can read it: two numbers, reflection() and ambient(). The Ranger 8-Way packs values into each; split them with math blocks and use them. Work down the steps on the left and watch the blocks grow on the right; the block pictures are exported from Pybricks Code itself.

How this differs from Pybricks blocks (with the library)
  • This page: nothing to download or import, just open Pybricks; but it gives only 2 numbers: the direction and distance of the nearest thing.
  • With the library: import MBC_laser8.py first, but it gives 16 functions (e.g. 8 distances, the most open direction, millimetres, faults per channel), one block per function.

Check first: Pybricks block coding needs a license code or a Patreon membership. Without one, write the MicroPython directly; it is exactly the same (switch the right panel to "MicroPython").

1

set up: create the color sensor and pick the port

No library: under the yellow "set up" put Pybricks' own color sensor block (ColorSensor), pick the port the Ranger 8-Way is on, and name it laser8.

Which port is the Ranger 8-Way on?
  1. In Pybricks Code press "+" to add a file and choose blocks.
  2. From "Sensors" drag the color sensor setup block (sensor icon) under "set up", rename it laser8 (any English name works) and pick the port above.
2

program: read reflection() and ambient()

Under "program" put "repeat while true", print laser8 reflected light (reflection) and laser8 ambient light (ambient) inside, then "wait 100 ms". Reading reflection switches the sensor to SPIKE mode 5 and ambient to mode 7 automatically; the Ranger 8-Way puts one set of numbers in each:

The simulator reads now:

reflection() is a whole number, the direction −8 to +8. Negative numbers are real negatives (the Ranger 8-Way prepares them inside the sensor), so do not add 6399. ambient() is a decimal, the nearest distance (e.g. 40.0).

3

Split with math blocks and use it

Store the reading in a variable with "set to" from Variables: r = reflection, a = ambient, rounded. Then split it with // (whole part of ÷) and % (remainder) from Math, and print or test the values. Click a function and the blocks and output change to its example.

Simulated sensor (seen from above, robot facing up): drag the obstacle and watch the output on the right

The simulation is only a sketch: direction and distance come from a simple position-based estimate, so a real sensor may differ by a step or two. Red sector = that sensor sees something; grey sector = it reads 120 (nothing that way). Sensors 4 and 5 both face straight ahead.

Block examples for every function

Every function has a set of blocks to copy (name laser8, port C). "Try it" jumps to step 3, and the blocks and output on the right change to that example.

For more values use Pybricks blocks with the library or MicroPython.

Good to know

  1. Only 2 numbers. reflection() and ambient() hold one each; everything else is out of reach (listed under the step 2 table). For those use Pybricks blocks with the library.
  2. No fix for negative directions: other sensors teach "if r < 0, add 6399"; on the Ranger 8-Way do not add it, a negative number simply means left.
  3. Nothing around: the nearest distance is 120 and the direction stays at −8 or +8 (the side it last saw).
  4. A broken channel always reads 120; the built-in blocks cannot tell. Use is_ok in the library for that.
  5. Do not use the "color" block or color names. The Ranger 8-Way sends numbers: store them in variables and compare numbers. Unplugging the sensor makes the built-in blocks raise an error and stop the program (the library version does not).
  6. Behind the blocks is MicroPython. The code pane on the right of Pybricks Code shows the program the blocks make; it is the same as "MicroPython" on this page.