Raspberry_Pi_Education_Manual

Now, we need to use the red patch on the car’s bumper as a red sensor. The program will then be able to detect when the line turns left and tell the car to turn to the left too.

Tip...

If your car stops at an odd angle, and you can’t straighten it out, you can import the “yellow_car” sprite again, drag and drop your finished script onto the new sprite and then delete the old one.

Here’s the code you need to finish your LFV program:

Tip...

Run the program in full-screen mode – it is more impressive. Click on the right-hand button at the top-right of the Scratch window,

As you can see, the program is run with a green flag event. After that:

n  A “go to” block is used to position the car on the line. n  We the have used a “forever” loop to keep the car running. n  The car moves 0.5 steps (for each cycle of the loop). n  An “if” block is then used to check if the green spot on the car has touched the black line. If it has, the car will turn to the right by 1 degree. n  Another “if” block is then used to check if the red spot on the car has touched the black line. If it has, the car will turn to the left by 1 degree.

just above the green flag and the red circle.

For an example of a completed version of this project, open RPiScratch/Projects/lfv.

The level crossing

Resources: The sprites “ lights ” and “ barrier ”, the background “ level_crossing ” and the sound “ level_crossing_a ”.

Computer control systems can save lives. At a level crossing we have to make sure that cars and trains never meet. This is done with a sensor on the track that detects the passing train. A computer then responds to this event by stopping the traffic approaching the level crossing and closing the barrier. This control program can be written in Scratch. But before we start, we need to import the necessary sprites and background. First, import the background “ level_crossing ”. As you can see from the picture on the next page, the background shows the road leading up to the crossing, the traffic-light box and the barrier support.

A beginner’s guide to Scratch

40

Made with FlippingBook flipbook maker