Let's start with the low C. As defined in the previous section, "Low C will be played when acceleration in the x direction is less than -775mg"

Again there are two conditions that must be met for the low C to be played. The first is that button A must be pressed. The second is that the acceleration in the x direction is less than -775mg. To do this we must add a second if statement that checks for accelerometer value within the first that checked for button A being pressed.

  • Add an if statement within the first if statement we made.

Next we need a comparison operator to check the condition of the accelerometer value.

  • Go to the logic tab and grab a comparison operator and drag it where "true" is next to the if statement.

We now need to determine what we are comparing. We want to compare the acceleration in the x direction to -775mg. If it is lower than -775mg the low C should be played.

  • Go to the input tab and grab an "acceleration (mg)" statement.
  • Make sure the axis is set to x.
  • Change the operator sign to be less than ("<")
  • Then change the value in the white oval to be -775

Now that our logic is sound, we can go ahead and tell the program to play a low C if all the above conditions are met.

  • Add the Middle C from the music tab then click on the "Middle C" white oval and select the lower C on the piano.

We will program the next note to play between -775mg and -550mg.

  • Click the plus sign on the bottom of the inside if statement. An "else" should have popped in as part of the block.
  • Now click the plus sign again. You will see an "else if then" pop into the block.

The "else if" part of the statement will allow us to check for the other desired accelerometer positions.

Because we need to check if the accelerometer is between two values, we need an extra statement that will do this for us.

  • Go to the logic tab and grab the first "boolean" operator and drag it in the empty slot next to the else if statement.
  • Since we want the accelerometer value to be greater than -775mg and less than -550mg, choose the "and" operator.
  • To create the two comparisons you can copy and paste the same one we made from above and change the values to be > -775mg and < -550mg.
  • Then, drag another music tone into the new condition and change it to low D.

Rinse and Repeat

Now that you know how to create new conditional statements within the inner if statement, continue the process for the rest of the pitches and accelerometer values.

Remember you can copy and paste entire elements and groups of elements!

Your code should look like this when all said and done... Pretty!

Changing the "Tone" of Your Tilt Trumpet

You can change the character of the tone of the sound by changing the length of the beat. My settings play each pitch for an 1/8th of a beat repeatedly but you can change yours to whatever you wish. This code is inside a forever loop and thus is continuously looping through the code. Thus it will continue playing that same 1/8th or 1/2 or what ever chosen length of a beat as it gets looped, simulating a constant flow of sound.

This guide was first published on Apr 11, 2018. It was last updated on Apr 11, 2018.

This page (Programming the Accelerometer) was last updated on Apr 08, 2018.

Text editor powered by tinymce.