Duplo train project, how to read the color sensor #1372
Replies: 3 comments 8 replies
-
@NStrijbosch, did you perhaps have an existing example to read some data from a LWP3 device? Our example (above) only sends stuff. |
Beta Was this translation helpful? Give feedback.
-
This one worked at some point (reading the color sensor of Mario). With some small changes this should work with the duplo train. from pybricks.hubs import ThisHub
from pybricks.iodevices import LWP3Device
from pybricks.parameters import Color
MARIO = const(0x43)
PORT_VALUE_MSG = const(0x45)
hub = ThisHub()
# Connect to the Mario
mario = LWP3Device(MARIO)
print("connected")
#subscribe to Mario barcode/color sensor
mario.write(b'\x0a\x00\x41\x01\x00\x01\x00\x00\x00\x01')
while True:
msg = mario.read()
kind = msg[2]
if kind != PORT_VALUE_MSG:
continue
color = msg[6]
print("color:", color)
if color == 21: # Mario is on red
hub.light.on(Color.RED)
else:
hub.light.on(Color.BLUE) |
Beta Was this translation helpful? Give feedback.
-
Thank you for the fast replys. PORT_VALUE_MSG = const(0x45)
so the color sensor must be a different value, but probably not 0x43 ? |
Beta Was this translation helpful? Give feedback.
-
Hi
I just started to code with pybricks. I have a bit of an idea how to code, but I get stuck in this Duplo train project.
I do build Lego GBC Modules (Great Ball Contraption) and I already have a module with the Duplo train, The problem is, that the train stops after 15 minutes. I have a video here:
https://youtu.be/VLBbY-jCAsI
So I was very happy, when I found this projekt:
https://pybricks.com/projects/tutorials/wireless/hub-to-hub/pybricks-to-official-fw/
The train should go forward and backward (not in an endless round) to go from loading to unloading. So I still have to check, were is the end of the tracks. And for this reason I need to read the color sonsor in the train. And unfortunatly I have no idea how to do that. I have no problem to use different colors and both ends.
Thank you for any help
Beta Was this translation helpful? Give feedback.
All reactions