Search icon
Arrow left icon
All Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletters
Free Learning
Arrow right icon
Raspberry Pi Projects for Kids (Second Edition)

You're reading from  Raspberry Pi Projects for Kids (Second Edition)

Product type Book
Published in Apr 2015
Publisher
ISBN-13 9781785281525
Pages 146 pages
Edition 1st Edition
Languages
Author (1):
Daniel Leonard Bates Daniel Leonard Bates
Profile icon Daniel Leonard Bates

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...

lock icon The rest of the chapter is locked
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime}