Pi-Cars and Scratch – Driving Lesson #1

  • Pre-driving checks

If you are here you should have set up your Raspberry Pi and also understand how to connect your Pi-Car to your Raspberry Pi. If not don’t worry:

For more information on setting up your Raspberry Pi click here.

For more information on how to connect your Pi-Car to your Raspberry Pi click here.

  • What’s in this driving lesson?

In this session we are going to get the Pi-Car working through Scratch which is a tool that comes installed on the Raspberry Pi. It is a great way of learning programming without having to write lines of code as it is all done graphically.

To do this you need to use the Pi-Cars software that is available on the Pi-Store as detailed here. Or buy one of our SD cards that has it installed on already. If you cannot do any of these there are some detailed instructions for how to create and run the Python script that communicates with the GPIO pins – these are available here.

If you have any problems just leave a comment or email info@pi-cars.com.

  • Lets get started

Open up Scratch by double clicking on the Scratchet icon on the desktop. This will start Scratch up and also the Python program that will listen to Scratch and send it onto the  GPIO pins.

Scrachet icon

When you double click on this it will open up an example Scratch file that enables you to move the car around with the arrow keys. This is explained in this post.

Create a Scratch program

To learn about how to use Scratch the following instructions show you how to create something similar to the example file from an empty Scratch file. This will show you how some of Scratch works and how to use it.

Alternatively you can just start modifying the example Scratch file that opens when you click on the Scratchet to make the Pi-Car do different things – just save it as a different name so you can always go back to the original file that works.

So create a new empty Scratch file and then the first thing you need to do is let Scratch know that you want to communicate with something outside of Scratch itself.

To do this click on the blue sensing button in the window at the top left of Scratch then right click on the second item from the bottom. This should pop up a screen that gives you the option to ‘enable remote sensor connections’:

Enable remote sensor in Scratch

Enable remote sensing on Scratch by clicking on the Slider Sensor value

When this is done it should inform you that Remote Sensing is Connected. You can also click back on the terminal window and see that it is now connected and waiting for information from Scratch:

picars script

Terminal window showing picars script connected to Scratch and awaiting instruction.

What we now need to do in Scratch is have a way of setting the GPIO pins on and off. The way this is done is by using things called variables. These will be sent by Scratch to the picars python program that is running which will then either turn the relevant GPIO pin on or off. When the variables are set to a 1 they will turn the pin on, when they are set to a 0 they will turn the pin off.

To create a variable click on the variables tab and make variable:

Variables in Scratch

Create a variable in Scratch which will be able to set a GPIO pin on and off.

The names of the variables that the picars program is expecting are as follows:

  • gpio-output1
  • gpio-output2
  • gpio-output3
  • gpio-output4

So when you create a new variable name them exactly as above – make sure you do not have capital letters and the names should be exactly the same. This should mean you end up with four variables:

GPIO variables in Scratch

All four GPIO variables now created in Scratch – note that in the window with the Cat the value of the variables is shown.

We now need to be able to set the variables based on certain triggers or in response to some action. If you know how to use Scratch you can choose any of the controls that are available. If you don’t know Scratch click on the control menu in the top left box and click and drag the box that says ‘When space key pressed’ into the stage area. Do this twice:

Control in Scratch

Select two controls in Scratch – change one of them in the drop down box for when ‘a’ is pressed.

Note that the top control has been changed through the drop down menu to say when a key is pressed as opposed to space bar.

You can now set your variables to turn the GPIO pins on and off. To start with click on the variables button in the top left window and then click and drag the button that says ‘set gpio-output1 to 0’ and dock it into place underneath your control that says ‘when a key pressed’:

set gpio variable

Set the GPIO variable by docking it underneath the control

By default it will set the variable to ‘0’. We want to set it to a ‘1’ which will turn the GPIO pin on so click where it currently has a ‘0’ and enter a 1. We could try this now with the Pi-Car but before we do we will want a quick way of turning the GPIO pin off so the Pi-Car does not run away.

To do this click and drag the ‘set gpio-output1 to 0’ out and dock it under the ‘when space key pressed’. Do this four times and in the drop down box for the others set it to gpio-output2, gpio-output3 and gpio-output4 .

Scratch with GPIO pins on and off

Scratch setup to turn a GPIO pin on when the a key is pressed and turn them all off when the space is pressed.

So try it out! Press the a key which should make the car go backwards and the space bar should stop the car going backwards. If you look in the picars program that should still be running you will see it print out that it has received an update from Scratch and will be changing the GPIO pin whenever the a key or space bar key is pressed:

Setting GPIO pin on

When the a button is pressed the picars script shows it has received something and will set the GPIO pin. Also notice in Scratch in the window with the Cat the variable is showing as 1 as it has been set.

Press the space bar and you should see the variables return to 0 and the car stop moving backwards. Try changing the variable that is turned on when the a key is pressed and work out which one results in which movement of the car.

So there you have it you have moved the Pi-Car from Scratch and you now have the building blocks to do much more complicated things in Scratch where you can use any of the control loops to turn the variables on and off.

If it hasn’t worked, don’t despair leave a comment or email to info@pi-cars.com make sure you mention which bits (if any!) have worked and which not.

You can then see which number results in which direction on the Pi-Car and then use the variables in any Scratch control loops and reacting to anything that you want.

Advertisements

About Barnaby Kent

http://www.myactionreplay.com
This entry was posted in Under the bonnet and tagged , , , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s