Exploring the PicoBoard sensors

Use Your Senses

This project uses the slider, light sensor, and resistance sensors on the PicoBoard to connect your Scratch project to the physical world. The two examples create musical fruits and veggies and a PicoBoard joystick.

One of the great all-around add-ons for Scratch is the PicoBoard, which plugs in very easily to the Raspberry Pi. In a previous issue of Raspberry Pi Geek, I introduced the resistance sensors on the PicoBoard by attaching them to a thermistor and measuring the resistance values of melting or warming water [1]. This time around, I'll show you two animations you can do with the PicoBoard as educational projects or part of a larger project.

The first example uses musical fruits and veggies to explore the sound sensor and create some music scripts using the sound blocks in Scratch. If you don't have a PicoBoard [2], the scripts shown here will still give you ideas and actual snippets of code that you can use to add music to other projects.

The second example creates a joystick to control the speed and direction of a sprite by using the slider and light sensor. The second half of the script demonstrates a universal way to move sprites to the opposite side of the stage after it passes the boundary.

Jamming with Musical Fruits and Veggies

We often think that veggies taste good, but have you ever wondered how they sound? If you connect a set of alligator clips to various vegetables, Scratch can capture the resistance values of those vegetables. The resistance values can be used in musical scripts to select instruments, tempo, or notes; that's the fun part of the project. The educational value could lead you to talk about why one vegetable might have a higher resistance than another. Or, you might try to guess the resistance of a vegetable.

Remember that resistance refers to a circuit's ability to pass a current. The more resistance a material has, the more it impedes the current. The PicoBoard reports resistance on a scale between 0 and 100, where 0 offers no resistance and 100 allows no current to pass.

My fruit and veggie jam band included a carrot, a leaf of kale, a pickle, an orange, and an orange peel. To measure the resistance of any given vegetable, you connect one of the alligator clips to an end connector (PicoBoard ports A through D) then touch or attach each clip on the port to the vegetable to complete the circuit. Make sure everything is working by enabling the (resistance-C) sensor value stage monitor from the Sensing palette. Make sure that it is reporting a value. My scripts will use resistance-C, but if you have the clips plugged into port A, make sure you use resistance-A.

When the clips are not connected, the value should show 100, and when the clips are attached to each other, the value should be 0. The vegetables will be somewhere in between.

The script in Figure 1 plays "Happy Birthday" to a tempo that is set according to a resistance value. Intuitively, you might expect that vegetables with low resistance would play up-tempo, but in reality, the low resistance corresponds to a slow tempo. The set tempo to () bpm block calculates a tempo by subtracting the resistance value from 100.

Figure 1: Play "Happy Birthday" to the tempo set by the resistance values of the vegetables.

If the pickle has a resistance of 18, then the tempo is calculated as 82. As a result, the low-resistance pickle now plays an up-tempo tune. By contrast, a high-resistance kale leaf would turn a resistance value of 90 into a tempo of 10.

If you're not sure how to play "Happy Birthday," don't worry, I don't either. I got an arrangement courtesy of Let's Play Music [3]. You can also find other popular songs there.

The Let's Play Music website shows the piano keys required to play the song, which is really helpful in this case. If you click on the note drop-down in the play () note for () beats block, you will see a small keyboard appear (Figure 2).

Figure 2: Clicking on the note block displays a keyboard. The red key was added for emphasis, and the highlighted key corresponds to the selected value.

As you mouse over the keys, the note displays along with the Scratch value. Using the arrangement provided by Let's Play Music, I was able to find the appropriate note values for use in my script. The play note block accepts values between 0 and 127.

The script in Figure 3 shows a different way to work with the resistance value. This time, I used the value to calculate a note to play. The notes follow a pattern by using the addition block. In my original music clip, the blocks played the notes 55, 57, 55, 57, 59, and 60. To correlate the original note pattern to the note pattern shown in Figure 3, think about the relative change in each note from the starting note of 55. Rewritten it becomes 55, 55+2, 55, 55+2, 55+4, and 55+5. Now you can substitute the 55 with (resistance-C) sensor value to change the notes based on resistance of the vegetables while maintaining the pattern. This short sound clip just provides an example for interacting with and changing a sound clip based on some variable inputs. (It's not shown as an example of musical genius.)

Figure 3: The resistance value is used to select a note to play in a short jingle.

There's another fun sound block in Scratch called play () drum for () beats, which offers a range of percussion instruments. I won't show an example here, because it only accepts values between 32 and 82. By using the vegetables, you're likely to get values outside the block's range. The set instrument to () block provides an alternative to the play drum for () beats and could be used to select an instrument based on a resistance value.

Turning the PicoBoard into a Joystick

The script in Figure 4 uses the PicoBoard slider and sound sensor values to control the movement of the sprite. Additionally, the sprite will be able to go off the stage and reappear on the opposite side.

Figure 4: This script turns the PicoBoard into a joystick to drive sprites around the stage.

The turn <clockwise> () degrees block is calculating a number of degrees based on the slider position. The script subtracts the slider value from 50 to control which way the sprite turns. The slider reports a value from 0 to 100, so the calculation will yield a positive, a negative, or zero value.

If you isolate the turn (50 - (slider)) degrees sensor value from the script, you can observe the actions of this block and calculation. You will find that a slider value greater than 50 will cause the sprite to spin counterclockwise. For example, a slider value of 90 will yield a calculated value of -40 degrees. A slider value of 40 will yield a calculated value of 10 degrees and rotate the sprite clockwise. A value of 50 will not turn the sprite because the calculated value will be zero. Effectively, this creates a steering wheel (of sorts). You can turn the PicoBoard so that the slider corresponds to the rotation of the sprite.

If you experimented with the turn block, you'll note that the rotation is very fast. By dividing the value by five, as seen in the script in Figure 4, a smaller degree value is calculated. Doing this will slow down the rotation, helping you react and control the movement.

Next, the if () block checks whether the light sensor value is greater than 5. If so, the sprite moves based on the light sensor value. I chose 5 because when I cover the light sensor as much as I can with my finger, my sensor still reports a value, and that value is 5. This feature gives me a way to stop the forward movement and offer more control. It also illustrates an important point when working with the PicoBoard sensors: All values are relative.

You'll notice that I'm reducing the light value that the PicoBoard reports by dividing it by 15, so that the sprite movement slows down. Before you build the rest of the script in Figure 4, I recommend trying to control the sprite using the turn and move blocks as described. If the sprite gets stuck on the side of the stage, you can restart by clicking the green flag to go back to the center.

To slow down the sprite, cover more of the light sensor. The more you block the sensor, the lower the reported value and vice versa. If the sprite doesn't appear to be moving correctly, check the rotation style in the sprite properties and choose the can rotate option.

Some practice is required to control the movement of the sprite, but you can do it. Next, I'll examine the rest of the script to make the sprite's movement even smoother.

Buy this article as PDF

Express-Checkout as PDF

Pages: 4

Price $2.95
(incl. VAT)

Buy Raspberry Pi Geek

SINGLE ISSUES
 
SUBSCRIPTIONS
 
TABLET & SMARTPHONE APPS
Get it on Google Play

US / Canada

Get it on Google Play

UK / Australia

Related content