Skip to content
Parallax Learn

Parallax Learn

  • Welcome
  • Tutorials
        • Tutorial Series head tag

          Tutorial Series
        • Tutorial Series

          The special, classroom-ready series pages are organized collections of tutorials for our most popular hardware and/or languages. The tutorials for each topic are conveniently accessible from a single page, shown in the order it is recommended that they be completed.
        • Robotics Series Head tag

          Robotics Series
        • Robotics Series

          • Artificial Intelligence
          • Cybersecurity: Radio Data tutorialCybersecurity
          • cyber:bot + Python
          • cyber:bot + MakeCode
          • Boe-Bot Tutorial SeriesBoe-Bot
          • Arduino Shield-Bot
          • ActivityBot with C TutorialsActivityBot + C
          • ActivityBot with BlocklyProp Tutorial SeriesActivityBot + BlocklyProp
          • Scribbler 3 Tutorial SeriesScribbler 3
        • Electronics & Programming Series Head tag

          Electronics & Programming Series
          • BS2 Board of Education Tutorial SeriesBS2 Board of Education
          • Propeller C-Language BasicsPropeller C Basics
          • FLiP Try-It Kit C Tutorial SeriesFLiP Try-It Kit + C
          • FLiP Try-It Kit BlocklyProp TutorialsFLiP Try-It Kit + BlocklyProp
          • Badge WX Tutorial SeriesBadge WX
          • Propeller BlocklyProp Basics and ProjectsPropeller BlocklyProp Basics
          • View All Tutorial Series »
        • Browse Tutorials
        • Browse Tutorials

          Individual tutorials sorted by robot or kit, and language.
        • By Robot or Kit
          • ActivityBot
          • SumoBot WX
          • Boe-Bot
          • Shield-Bot
          • cyber:bot
          • Badge WX
          • ELEV-8
          • ARLO
        • By Language
        • By Language

          • Propeller C
          • Arduino
          • BlocklyProp
          • PBASIC
          • Python
          • MakeCode
          • View All Tutorials »
  • Educators
  • Reference
  • Downloads
  • Home
  • All Courses
  • Tilt Tones with BlocklyProp

Tilt Tones with BlocklyProp

Play Tilt Tones with Blocks

Let’s build the BlocklyProp code for programming your Tilt Tones musical instrument, and take a look at how it works.

Tilt Tones BlocklyProp Code

  • Create a new project named Tilt Tones.
  • Build the project shown below, and save it.
  • Click the Load and run button (save code to EEPROM on your board).
  • Tilt your board forward and backward, left and right. In each position, it should play a different tone and light a different LED for 500 milliseconds.

How the Tilt Tones Project Works

The 4-Directional Tilt Sensor uses two I/O pins; we connected to P14 and P15 in our project.  The diagram below shows what each I/O pin will detect when the sensor is tilted in each direction, either a one or a zero.  There are four output states for the sensor: binary 11 = 3, binary 10 = 2, binary 01= 1, and binary 00 = 0.

The code begins with a set multiple pins block. It is configured to set the directions for I/O pins P14 and P15 to IN (input), so the Propeller can receive signals from the Tilt Sensor.

The next task is to initialize and fill two arrays, each with four elements. Why four elements? One for every output state that the Tilt Sensor can provide.  The LED array holds the I/O pin numbers for the LED circuits built on the breadboard.  The Note array holds four different frequencies, which will be used to play notes on the piezo speaker.

Then, two set variable blocks declare and initialize variables.  First, you will set the beat variable to 500, and it will be used later when playing tones. The index variable gets an initial value of zero, and it will be used to access array elements.

The action happens inside a repeat forever loop.  The binary get pins block reads the input states of a contiguous group of I/O pins, and the state of each I/O pin becomes a bit in a binary number. The highest I/O pin number provides the most significant bit.  Our block uses P15 and P14, so the possible values are binary 11 = 3, binary 10 = 2, binary 01= 1, and binary 00 = 0.  In this way, the output state of the Tilt Sensor is stored as a binary number in the index variable.

Now, the freshly-set index variable will be used to select the element at the value of that position from the LED and Notes arrays in the next three blocks. For example, if you tilt the board towards you, P14 and P15 both detect 0, making the value of index 00, which means it grabs element 0 from the array.          

To turn on an LED, the index variable is put inside a get array [LED] element block, which is inside a programmable make PIN block. Using the above example, the 0th element in the LED array is 4, so the LED on P4 lights up when this block sets it high.

To play a note, the index variable is used inside the get array [LED] element block, which is inside a frequency out block.  To continue with the example, the 0th element of the note array is 1175.  That frequency plays for 500 ms – the value of the beat variable used in the duration (ms) field.                 

After the note plays, the loop finishes with a second make PIN block. Like the first one, it uses the indexed array [LED] element, setting the I/O pin low to turn off the LED off before the loop repeats.


Printer-friendly version
Tune your Tilt Tones
Next

DISCUSSION FORUMS | PARALLAX INC. STORE

About | Terms of Use | Feedback: learn@parallax.com | Copyright©Parallax Inc. 2024

© 2025 Parallax Learn • Built with GeneratePress