Duplo train project, how to read the color sensor #1372
Replies: 4 comments 17 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.
-
|
Cool. I had no idea how to even find or install beta firmware before. I initially did try a City Hub, as it was what I was using prior for this train, and it seemed to have the same build-4763-gitc135fd98 ... But it couldn't import DuploTrain So, as recommended, I switched to using a Mindstorms Hub with the following code. I slowed it right down as my little test track is just a circle, and all five of the color plates are fairly close together. This resulted in fairly good colour detections! Nice! Although the white plate was often flipping between White and Violet. Anyhow, thanks for this! I will now continue to learn me some new stuff with this impulsive purchase of a $10 Duplo Steam Train (10864) set, that I just picked up yesterday (And I didn't even know that PyBricks was an option for it at the time, Hah!) |
Beta Was this translation helpful? Give feedback.

Uh oh!
There was an error while loading. Please reload this page.
-
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