Paste the reading helper
A SPIKE App Python project is a single file and cannot import your own .py, so paste the whole "reading helper" at the very top (its first lines say so: copy it as is). Without it the hub does not know Laser2App and stops with NameError.
- Open the LEGO SPIKE App (Education, yellow SPIKE Prime hub), create a project and choose "Python".
- Paste the program marked ① on the right at the top (only the first helper line shows; press "Show helper" to see it all, and "Copy program" always copies everything). The helper alone: MBC_laser2_App_helper.py (open it in a text editor and copy all; the file has Chinese and English comments).
- Plug the Ranger into any port A–F; the hub powers it. The App sees it as a color sensor, so there is nothing to install.
The helper reads three of the App's color sensor values and converts them the same way as the SPIKE App blocks page:
| App value | Holds | Helper function |
|---|---|---|
Raw red rgbi()[0] | left distance × 256 | left(): ÷ 256, whole number → 0 to 120 cm |
Raw green rgbi()[1] | right distance × 256 | right(): ÷ 256, whole number → 0 to 120 cm |
Raw blue rgbi()[2] | average distance × 256 | average(): ÷ 256, whole number → 0 to 120 cm |