Book Image

Raspberry Pi Projects for Kids (Second Edition)

By : Daniel Leonard Bates
Book Image

Raspberry Pi Projects for Kids (Second Edition)

By: Daniel Leonard Bates

Overview of this book

Table of Contents (14 chapters)

A real tune


So, we've had fun creating random tunes; now let's try making something a little more structured. Open a fresh workspace by clicking on one of the Workspace tabs underneath the Code Editor. This is where we'll create our new tune. You can switch back to your previous program any time by clicking on its tab.

In this section, we're going to build on the same piece of code, getting the sound we produce closer and closer to a tune you might know. How far will you get before you recognize the tune? A complete code listing is at the end of this chapter just in case you get stuck.

Here's the very first version of the code— you might recognize it already!

  play :G
  sleep 1
  play :G
  sleep 1
  play :G
  sleep 1
  play :Eb
  sleep 1
  play :Bb
  sleep 1
  play :G
  sleep 1
  play :Eb
  sleep 1
  play :Bb
  sleep 1
  play :G
  sleep 1

This code is simple but long. It's very easy to miss a line, and it's boring to write sleep 1 so many times. Let's tidy it up a little by writing a function...