Professional Documents
Culture Documents
GPIO, which lets you use the Pi’s GPIO pins – about which you’ll learn more in Chapter 6,
Physical computing with Scratch and Python – from another computer on your network.
Click on the Performance tab to see the third category. Here you can set the amount of
memory used by the Pi’s graphics processing unit (GPU) and, for some models, increase the
performance of the Raspberry Pi through a process known as overclocking. As before, though,
it’s best to leave these settings alone unless you know you need to change them.
Finally, click on the Localisation tab to see the last category. Here you can change your
locale, which controls things like the language used in Raspbian and how numbers are
displayed, change the time zone, change the keyboard layout, and set your country for WiFi
purposes. For now, though, just click on Cancel to close the tool without making any changes.
WARNING!
Different countries have different rules about what frequencies
a WiFi radio can use. Setting the WiFi country in the Raspberry
Pi Configuration Tool to a different country from the one you’re
actually in is likely to make it struggle to connect to your networks
and can even be illegal under radio licensing laws – so don’t do it!
Shutting down
Now you’ve explored the Raspbian desktop, it’s time to learn a very important skill: safely
shutting your Raspberry Pi down. Like any computer, the Raspberry Pi keeps the files you’re
working on in volatile memory – memory which is emptied when the system is switched off.
For documents you’re creating, it’s enough to save each in turn – which takes the file from
volatile memory to non-volatile memory, the microSD card – to ensure you don’t lose anything.
The documents you’re working on aren’t the only files open, though. Raspbian itself keeps
a number of files open while it’s running, and pulling the power cable from the Raspberry Pi
while these are still open can result in the operating system becoming corrupt and needing to
be reinstalled.
To prevent this from happening, you need to make sure you tell Raspbian to save all its
files and make itself ready for being powered off – a process known as shutting down the
operating system.
WARNING!
Never pull the power cable from a Raspberry Pi without
shutting it down first. Doing so is likely to corrupt the
operating system and could also lose any files you have
created or downloaded.
Reboot goes through a similar process to Shutdown, closing everything down, but instead of
turning the Pi’s power off, it restarts the Pi – in almost exactly the same way as if you’d chosen
Shutdown, then disconnected and reconnected the power cable. You’ll need to use Reboot if
you make certain changes which require a restart of the operating system – such as installing
certain updates to its core software – or if some software has gone wrong, known as crashing,
and left Raspbian in an unusable state.
Finally, Logout is only really useful if you have more than one user account on your
Raspberry Pi: it closes any programs you currently have open and takes you to a login screen
on which you are prompted for a user name and password. If you hit Logout by mistake and
want to get back in, simply type ‘pi’ as the user name and whatever password you chose in the
Welcome Wizard at the start of this chapter.
Programming
with Scratch
Learn how to start coding using Scratch, the block-based
programming language
U
sing a Raspberry Pi isn’t just about using software other people have created; it’s
about creating your own software, based on almost anything your imagination can
conjure. Whether you have previous experience with creating your own programs
– a process known as programming or coding – or not, you’ll find the Raspberry Pi a great
platform for creation and experimentation.
Key to the accessibility of coding on the Pi is Scratch, a visual programming language
developed by the Massachusetts Institute of Technology (MIT). Whereas traditional
programming languages have you write text-based instructions for the computer to carry out,
in much the same way as you might write a recipe for baking a cake, Scratch has you build
your program step-by-step using blocks – pre-written chunks of code hidden behind colour-
coded jigsaw pieces.
Scratch is a great first language for budding coders young and old, but don’t be fooled by its
friendly appearance: it’s a powerful and fully functional programming environment for creating
everything from simple games and animations through to complex interactive robotics projects.
B
F
C D
5Figure 4-1: Drag and drop the block into the scripts area
Look at the shape of the block you’ve just dropped: it has a hole at the top, and a matching
part sticking out at the bottom. Like a jigsaw piece, this shows you that the block is expecting
to have something above it and something below it. For this program, that something above
is a trigger.
Click on the Events category of the blocks palette, coloured light brown, then click and drag
the when clicked block – known as a hat block – onto the scripts area. Position it so that
the bit sticking out of the bottom connects into the hole at the top of your say Hello! block
until you see a white outline, then let go of the mouse button. You don’t have to be precise; if
it’s close enough, the block will snap into place just like a jigsaw piece. If it doesn’t, click and
hold on it again to adjust its position until it does.
when clicked
say Hello!
Your program is now complete. To make it work, known as running the program, click on the
green flag icon at the top of the stage area. If all has gone well, the cat sprite on the stage will
greet you with a cheery ‘Hello!’ (Figure 4-2) – your first program is a success!
5Figure 4-2: Click the green flag above the stage and the cat will say ‘Hello’
5Figure 4-4: To delete a block, simply drag it out of the scripts area
Click on the Motion category in the blocks palette, then click and drag the move 10 steps
block so it locks into place under the trigger block on the scripts area. As the name suggests,
this tells your sprite – the cat – to move a number of steps in the direction it’s currently facing.
when clicked
move 10 steps
Add more instructions to your program to create a sequence. Click on the Sound palette,
colour-coded pink, then click and drag the play sound meow until done block so it locks
underneath the move 10 steps block. Keep going: click back on the Motion category and drag
another move 10 steps block underneath your Sound block, but this time click on the ‘10’ to
select it and type ‘-10’ to create a move -10 steps block.
when clicked
move 10 steps
when clicked
move 10 steps
Click the green flag to run your program again, and the cat sprite doesn’t seem to move. The
sprite is moving, in fact, but it moves back again so quickly that it appears to be standing still.
This is because using the play sound meow block doesn’t wait for the sound to finish playing
before the next step; because the Raspberry Pi ‘thinks’ so quickly, the next instruction runs
before you can ever see the cat sprite move. There’s another way to fix this, beyond using the
play sound meow until done block: click on the Control category of the blocks palette,
colour-coded gold, then click and drag a wait 1 secs block between the play sound meow
block and the bottom move -10 steps block.
when clicked
move 10 steps
wait 1 secs