In previous examples, we've seen how easy it is to import images from Scratch's built-in libraries or from files on our computer. Now, it's time to circle back and check out the webcam. This exercise will also use the PicoBoard's sound sensor to detect our voice.
To start, create a new project, and delete or hide the Scratch cat and then complete the following steps:
With the stage selected, click on the Backgrounds (Backdrops in 2.0) tab, and then click on the camera icon. A new window is displayed and shows what the webcam sees.
Look at the camera with your mouth closed, and click on the camera icon to import the image. Then open your mouth, and take another snapshot. Click on Done to close the dialog box. We now have two background images.
Name the first imported image
silent
and the second onetalking
.Now let's script our snapshots. Click back to the Scripts tab, and start with a when flag clicked block.
Add a switch to background () block, and select the silent background. This block is called switch backdrop to ().
Now we'll need to continually evaluate whether or not the detected sound sensor value is greater than 15.
Add a forever block followed by an if (), else block from the Controls palette. Add the greater-than block to the condition value. From the Sensing palette, add the () sensor value block, and select the sound sensor. Enter
15
for the block's second value.If the sound is greater than 15, we want to alternate between the two backgrounds. Add a switch background to (talking) block, followed by a wait (.1) secs block, which is then followed by a switch background to (silent) block.
As the else condition, add a switch backdrop to (silent) block.
Click on the green flag and talk. Feel free to experiment with the detected loudness value and the wait value of the script to see what effects you end up with.
Let's make one more enhancement to this script to give our animation the feel of a 1980s television station that went off air. Add the following block to the if condition: set (ghost) effect to ((100) – ((sound) sensor value). Then to the else condition, add this block: set (ghost) effect to (100). Depending on how loudly you talk, you might want to experiment with multiplying the (sound) sensor value by some number, such as five. Now the image will fade in and out based on the loudness. The following screenshot shows our script:
By combining the backgrounds that we saved from the computer's webcam and the detected sound levels, we very quickly and easily integrated external data into our Scratch project. The fact that our talking head looks a little goofy makes it fun. It reminds me of translated movies where the English translation is dubbed in over the original movie. The sound never quite matches the mouth movements, but it is often close enough to get the point across.
By adjusting the ghosting effect, we took some of the emphasis off the timing of the script. The visual strength of the image depends on the strength (loudness) of the detected sound. With the sound sensor on the PicoBoard, a higher sound value corresponds to a louder noise. The sensor will report values from 0 to 100 like the loudness block.
When we apply the ghosting affect while speaking, we calculate the new ghost value by subtracting the sound value from 100. You'll need to experiment with your sounds, but if the sensor measures a sound value of 20, then the ghost value becomes 80. This ensures our image is visible but not in full strength. Remember that with ghost, 0 is fully visible and 100 is transparent.
The talking head makes a great way to introduce Scratch programming in the physical world because it's quirky and generally makes people chuckle. It's really well suited for an audience such as workshops or presentations. I've seen this project done with animated lips, and I've used it in the past to strobe two colors based on the sound.
You could also use the sound value in other projects to change a sprite's costume, to move a sprite, or to apply a graphical effect. Explore the sound values some more by writing a script that moves a sprite forward and backward based on the detected loudness of the sound.
If you create a project in Scratch 1.4, you can still share that project to the new Scratch 2.0 website. Other people will be able to view your project on the website. The Scratch 2.0 website will not export a 1.4 file, and as soon as you edit the older file online, it will be saved as a new Scratch 2.0 format.
Each of the PicoBoard's sensors is described in the following table: