Conductor’s Baton
Learn how to turn your Bluebird into a conductor’s baton. By applying more complex math and programming skills, this project demonstrates how to correlate the volume and tempo of a song to the speed of Bluebird’s movements; similar to how a conductor conducts an orchestra!
Coding Concepts:
Input (Accelerometer), If-Then Statements, Map, Math
Part 1: Program the Conductor’s Baton
1) The [forever] block should already be in the web editor once you open it. Open the Music menu and drag in the [play melody at tempo ‘120’ (bpm)] block. Nest it in the [forever] block. This will allow us to play the melody of our choosing on loop.
2) Click on the melody bubble to open the melody editor. Then select the Gallery tab. Listen to the tracks by pressing the play button beside each one and select any tune you like. Now our forever loop will play that tune on repeat.
3) Our goal in the following steps is to correlate Bluebird’s up and down acceleration with tempo at an exponential rate. This means that when you move your conductor’s baton, the tempo of the song will change according to a curve rather than a linear line.
4) Open the Math menu and drag in the [map ‘0’ from low ‘0’ high ‘1023’ to low ‘0’ high ‘4’] block. Use it to replace ‘120’ in (bpm)value.
Open the Math menu again and drag in the [‘0’ + ‘0’] block. For now, drag in onto any free space in the editor. Open the operations dropdown menu beside ‘+’ and select . The symbol is the exponent operator. For example, ab = ab. Then open the Sensors menu and drag in the [acceleration (mg) ‘x’] block. Use it to replace the first ‘0’ in the [‘0’ ‘0’] block. Open the dropdown menu beside ‘x’ and select ‘z’. Input the number 2 as the second ‘0’ in the [[acceleration (mg) ‘z’] ‘0’] block so that it reads [[acceleration (mg) ‘z’] ** ‘2’] or [acceleration (mg) ‘z’]2.
Then drag the entire [[acceleration (mg) ‘z’] ** ‘2’] block over the [map] block to replace the first ‘0’ in the [map] block. Now, we can map Bluebird’s up and down acceleration to the tempo of the song at an exponential rate of 2.
5) In the z direction, Bluebird’s accelerometer reading will return a value between -1023 and 1023. Because we are squaring that return value, the resulting range is 0 and 1046529; 0 as the lowest possible value that a number squared can return and 1046529 as both (-1023)2 and 10232.
Keep the first low value in the [map] block as ‘0’, then replace the first high value with ‘1046529’. These values will act as our initial inputs for the [map] block.
6) The possible range for tempo is 4 (slowest) to 400 (fastest). When the conductor’s baton is moving slowly, we want the song to slow down but still sound like an intentional part of the song. 50 is a good value for the slow end of the tempo. On the fast end, we can use 400 since the song is still clearly audible. Feel free to play around with these values to match your personal preference.
Replace the second low value in the [map] block with ‘50’ and the second high value with ‘400’. Your code should now look like the following:
forever(function () {
music.playMelody("C5 B A G F E D C ", Math.map(input.acceleration(Dimension.X) ** 2, 0, 1046529, 50, 400))
});
7) To map the volume of the song exponentially, we can use the same method. Open the Music menu and drag in the [set volume ‘20’] block. Nest it underneath the [play melody] block. Then repeat steps 4 and 5 for the [set volume] block, since we will be mapping the acceleration reading at the same range.
8) The possible range for volume is 0 (silent) to 255 (loudest). When the conductor’s baton is moving slowly, we want the song to be softer but not silent. 10 is a good value for the quieter end of the volume. On the loud end, we can use 255. Feel free to play around with these values to match your personal preference.
Replace the second low value in the [map] block with ‘10’ and the second high value with ‘255’. Your code should now look like the following:
forever(function () {
music.playMelody("C5 B A G F E D C ", Math.map(input.acceleration(Dimension.Z) ** 2, 0, 1046529, 50, 400))
music.setVolume(Math.map(input.acceleration(Dimension.Z) ** 2, 0, 1046529, 10, 255))
});
Part 2: Upload your code to Bluebird
Click the orange Download button under the simulator. A pop-up menu will appear on your screen. Download the file and follow the instructions on the pop-up.