Most musicians have a way to control the volume of their instrument. Whether it's a trumpet and the instrumentalist simply uses more air at a faster speed to play louder or a piano player that strikes the keys with more intensity, musicians need some way to play louder and quieter.
We just programmed the board to change pitch based on how far forward or backward it is tilted on the x axis. Can we use the y axis and tilt the board left and right to do something else? How about changing volume?!
Let's add another if statement after the first massive one we just programmed. This new if statement code block is separate from the previous one but still lies within the overall forever loop.
- Grab an if statement and drag it below our first one.
This if statement block will check the accelerometer value of the y axis to dictate volume.
I wanted to have more variability in volume control compared to pitch control so I decided to make 11 different volume levels mostly because of the simpler math that splitting the mgs into gave me.
Here is a map of the logic we will use to program the volume on the y axis of the accelerometer.
Tilting to the left:
- If acceleration is less than -900 mgs, play the loudest volume at 220 (Tilted all the way to the left)
- If acceleration is in between -900 and -700 mgs play volume at 200
- If acceleration is in between -700 and -500 mgs play volume at 180
- If acceleration is in between -500 and -300 mgs play volume at 160
- If acceleration is in between -300 and -100 mgs play volume at 140
Not tilted (level)
- If acceleration is in between -100 and 100 mgs play volume at 120
Tilting to the right:
- If acceleration is in between 100 and 300 mgs play volume at 100
- If acceleration is in between 300 and 500 mgs play volume at 80
- If acceleration is in between 500 and 700 mgs play volume at 60
- If acceleration is in between 700 and 900 mgs play volume at 40
- If acceleration is greater than 900 mgs play volume at 20 (Tilted all the way to the right)
Programming the Volume
- Let's start by grabbing another comparison operator from the logic tab.
- Go ahead and place that in the new if statement we created.
- Now go to the input tab and take an acceleration input to place into the first part of the comparison. Next make sure the comparison is less than (<).
- Lastly change the value of the white bubble to be 900.
Next we'll set the volume for this condition.
- Go to the music tab and grab a "set volume" element.
- Drag it inside the new if statement and set the volume to 220.
- Now let's click the plus sign twice like before so we can continue to build out this if statement.
For the next condition it will be much faster to copy and paste a condition we wrote earlier for the first if statement.
- Once you copy, paste and drag the new conditional statement into the new if else statement, make sure you change the values to reflect the new statement.
- Then copy and paste a "set volume" element, throw it in the new if else and change it to 200.
- Like before, repeat this process until all the conditions are accounted for.