MBC Ranger 2-Way × Pybricks built-in blocks

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

On a SPIKE hub the Ranger 2-Way shows up as a LEGO color sensor, so Pybricks' own color sensor blocks can read it: two numbers, reflection() and ambient(). The Ranger 2-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 the left and right cm values (average, nearer side and so on you work out yourself), and you cannot tell a broken laser.
  • With the library: import MBC_laser2.py first, but it gives 17 functions (e.g. average, nearer distance, which side is nearer, millimetres, laser faults), 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 2-Way is on, and name it laser2.

Which port is the Ranger 2-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 laser2 (any English name works) and pick the port above.
2

program: read reflection() and ambient()

Under "program" put "repeat while true", print laser2 reflected light (reflection) and laser2 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 2-Way puts one set of numbers in each:

The simulator reads now:

On Pybricks 4.1 reflection() carries one decimal; made whole it is the left distance (0–120), never negative. ambient() is a decimal, the right distance (e.g. 35.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.

Sensor simulator (seen from above, front of the robot facing up): drag the left and right obstacle distances and watch the output on the right change

The simulator is only a picture: the distance comes straight from where the obstacle is on the screen. A real laser can be off by 1 to 2 cm and only updates about every 0.1 seconds. 120 = nothing in front (or farther than 120 cm).

Block examples for every function

Every function has a set of blocks to copy (name laser2, 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. Pybricks 4.1 readings carry decimals: reflection() has one decimal (e.g. 81.3), so make it whole first (floor if ≥ 0, ceiling if negative) before // and %; ambient() keeps only one decimal, so split the integer part and the first decimal separately (floor, round(a × 10) % 10).
  3. 120 = nothing in front. Over 120 cm, or a broken laser, both read 120.
  4. Both sides: read reflection and then ambient in the same loop and work out the average or the nearer side yourself. Pybricks waits a little each time it switches modes, so it is slower than the library version.
  5. Do not use the "color" block or color names. The Ranger 2-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.