Improved Lego Powered Up Automatic Train Control [Color Sensor 88007] [Tutorial]
Vložit
- čas přidán 6. 06. 2024
- Learn how to program two Powered Up Lego trains to run back and forth on tracks with a passing loop in the middle
Links to pictures:
ibb.co/w6y3Wdb
ibb.co/h93j8Hg
ibb.co/2yY8t0s
ibb.co/QcwjWQZ
Transcript:
a while ago i made a video about two
trains
running back and forth using the powered
up app
and some color sensors but i made a
improved version of the program after
placing the trains in my city and
building the trains
i saw some flaws one of the flaws was
that
the trains couldn't run at different
speeds
and at different directions one of the
trains where the motor faced
the other way and the one of the trains
were heavier than the other
so they weren't actually actually
running at the same speed
so i made a program that fixed this
basically the new program is made
by selecting a controller not coding
in this when you create a new project
that gives you
some some extra graphical
stuff let me just try to show
the components it consists of these
controls and some coding behind it
these controls here are for train zero
or the first train
controls here are for the second frame
train one
this is a start button and this here is
a track
section occupies if it's if the
track section is clear or not so
basically
you have a track that goes something
like
this so down here it's a green and
blue and red and and all the red
so you can see if it's occupied or not
that section of the track
and basically when you start the program
you need to select if
train one should start and then press
start
live stream sorry trade zero of change
train one should start or if
both strings you start you have to
select both of these and press down
and these here control the speed of the
trains
and this one here will show the actual
speed
what speed the train is running at and
these here will
show if which direction it will show
zero if it runs in if it isn't running
minus 100 in one direction and plus 100
in the other direction
and this one down here will show
33 if it's drain 0 that has occupied it
or 66 if it's train one that has
occupied
occupied it but enough talking let's
show
how it works
too fast let's take it step by step
train 0 is in the middle at top red
ready to drive to green and green is
clear
so train 0 start running with speed -30.
train 1 is at blue but cannot run until
top red is clear
train 0 is on its way from top red to
green
train 0 arrives at green so top red is
now clear
and train 1 starts running at speed plus
50.
train 1 arrives at top red
bottom red is clear and has been clear
all the time
so train 0 starts its journey from green
towards bottom red at speed
plus 30. uh what up
okay let's see how it's programmed
these components here are for train zero
and these are for train one
the numbers the components have is vital
for the way the train works
the program works you can see this one
has a zero it's for train zero
and this one has a one it's for train
one so if you say that
x is either zero for train zero or
x is one for train one then
we take x and plus it with 2 for this
component
plus it with 4 for this component
and plus it with 6 for this component if
that makes any sense
and also down here if a track is clear
or not
the numbers are here are vital so track
8
the next section is track 9 and after
that track 10 and track 11
after 11 we go back to 8 and with some
basic math
we can easily calculate what the next
track section is
is and the track section before that was
so let's look at the the code
behind this okay this is a primary
program
basically it tells only to start when we
press the start button
this one here sets all the track
sections to zero
so we start by assuming it they they
aren't occupied
and this here is for train zero and this
for train one
so if the button is activated for train
0
we start train 0 or if the button is
activated for train 1 we start train 1
and which start train 0 in one direction
and train one in the other
and train 0 starts at speed 30 and the
other one starts at speed minus
50. that's minus because
the motor is facing the other direction
and the 50 because it's a heavier train
so that's two carriages so
it it takes more power to run it at the
same speed
and then we just set the this one here
to zero
and finally we start this sub program
with zero for train zero and one for
train one
so we have the sub program here
the subprogram does something if it
detects green
blue or red in the previous version of
the program
i used yellow instead of blue but i had
some problems with the
yellow tiles they were sometimes picked
up as red tiles
so that's why i use blue instead but
what it basically does
is that if it detects green
it calls a sub program with some
parameters
...
and even more - read the auto generated subtitles
#legopoweredup #legotrain #lego
Love the evolution of your coding from your first iteration. Finally got around to programming this into my layout with 4 Hubs and using distance over colour sensors because the sensor are at each station as my trams have lights on them. Your videos is the best start/ help I could find so far. Thanks.
It's been a fun learning curve with the New UI.
Thanks for subscribeing
Looks great
Thanks 👍
Love this! I was putting in a second line and it has a large messy loop. If I use this I can make it run in both directions
Glad you some inspiration 😁
This needs larger then 7 inches