Hello and welcome to the 7th part of my lego powered up tutorial
that can be used for control+ and lego boost, too. In this part, we will stick to the hardware that we had in the last part.
So you can either use the lego move hub from the lego boost sets with the color & distance sensor from lego boost or a technic hub with 2 motors and the same sensor. Last time I used the
technic hub combination and this time I will use the move hub combination. I have a move hub that includes 2 motors: one for the left side and one for the right side
and I use the color and distance sensor. Today's goal is to avoid obstacles so I want to detect an
obstacle with the sensor and the vehicle should move into another direction when it
sees something. For that we will use a second function of the
color and distance sensor. We will use it's function to measure the distance. So far we only had the color for the linefollower
and similar programs and this time we will use the distance.
It can be measured with this block. My sensor is on port C, make sure to select the right port.
10 is the maximum distance. So it can only detect distances from 1 to 10 and it can't detect objects that are far away.
It doesn't reach very far. That is a problem for bigger cars. Make sure that you
keep that in mind when you try it on your own. The sensor can only detect stuff that's like 10cm infront of it.
The program is pretty similar to the second solution for the linefollower.
But first, let's start with a start block.
Now we can think about the default state. Normally we want to drive straight until it reaches
something. So first, we will need a block to drive straight.
We can use this one for that. It powers the motors on port A and B. For the move hub,
these are the included motors. With a speed of 50 and 50. I don't have to change the direction for the
second motor of the move hub because both drive into the same direction if both are positive.
You might have to do that if you use a control+ hub with 2 motors.
Now the vehicle drives and we want it to react to an object that it detecs. For that, we can use a similar block to the "detect color" block
or "wait for color"-block: we can use the "wait for object"-block.
Here we can select the port of the color and distance sensor
and we can select the distance that we want it to trigger it. In my case it's 9 because
that's the second farest option and because the sensor can only detect close objects, I want to
use the highest value here. For 10 it would always trigger because 10 is the maximum value as you can see here.
So now we have a second state that is after it detected a wall or something similar.
In that case we want to drive around the wall or we want to drive into another direction
and for that we can change the motor direction with the same motor block. Normally it makes sense to drive a bit backwards
but only for one motor so that it turns at the same time. There's still a small mistake
because both of these blocks only change how the motor behaves and then the program continues to the next block
and we want it to drive backwards for a bit of time so that it actually drives into another direction. For that, we can use a simple wait block,
half a second is enough
and then it can drive again. But there's still a small mistake because
if you run this program, it will drive forwards until it detects a wall. Then it will drive backwards or to the side for half a second and then the program will stop.
But it should drive around so we want to repeat this. After it tourned to another direction, it should start to drive forwards again and we can do that with a simple loop.
That's the program. Now we can test it. I will run the program.
You can see that it works but that the sensor doesn't detect things that are far away. That's actually an issue with the sensor and I'm not very happy about it.
But we can't change that. That was this part of the tutorial. Thanks for watching.
Feel free to ask me questions if you didn't understand something or have some problems or something.
I'd be happy to answer them in the comments. See you in the next part.