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 Laser8App 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_laser8_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. Use the same port as in step 2.
The helper splits the numbers the App reads for you (the same conversion as the SPIKE App blocks page):
| What the App reads | What it holds | Helper function |
|---|---|---|
color_sensor.color() (color) | sensor 1 (far left) | ch(1) |
color_sensor.reflection() (reflection) | sensor 2 (front-left 60°) | ch(2) |
color_sensor.rgbi() item 1 (raw red) | sensor 3 + sensor 4 × 256 | ch(3), ch(4) (remainder and whole part of ÷ 256) |
rgbi() item 2 (raw green) | sensor 5 + sensor 6 × 256 | ch(5), ch(6) |
rgbi() item 3 (raw blue) | sensor 7 + sensor 8 × 256 | ch(7), ch(8) |