Raspberry_Pi_Education_Manual

Notes:

Once the program can detect two colours touching, then we can tell it how to respond when this happens. The colour event becomes a trigger, causing the program to do something. To set the colours in your block, click on one of the little coloured squares. Your mouse-pointer will turn into an “eye-dropper”. Use the eye-dropper to click on the colours you want to use in your block.

Tip...

If you can’t stop your car for long enough to sample a colour, hit the red “stop scripts” button at the top-right of the screen.

But how can we use the ability to detect colour to help make our LFV follow a line on the road? Well, let’s start by importing our “ line_background ” and the sprite “ yellow_car ”.

Look closely at the car, and you’ll see that there’s a patch of green on its bumper. We’re going to use that green as our first “sensor”. Build the script that you can see in the next screenshot.

Now, click on the green flag and see what happens. Oh dear, everything is fine as long as the line curves to the left but as soon as it curves to the right the car wanders off. What’s going on? The problem is that the car needs two sensors. Currently, if the line bends to the right, the black line will touch the green sensor – the program will detect this and tell the car to turn to the right.

A beginner’s guide to Scratch

39

Made with FlippingBook flipbook maker