Time for action - switch backgrounds on sound

Let's emulate night and day with our backgrounds so that each sound changes the scene. Open a new Scratch project and begin:

  1. Create the second background by copying the default white background. Then, edit background2 and use the fill tool to paint it black.
  2. Add the when flag clicked control block to the scripts area of the stage.
  3. Add a forever if block to the when flag clicked block.
  4. We need to evaluate the sound level before we act on it. From the Operators palette, add the greater than block.
  5. Now, let's evaluate the sound. From the Sensing palette, drag the sensor value block into the first field of the greater than block. Select the sound option from the drop-down list of options.
  6. We want to switch the background whenever the sound is greater than 0. Type a 0 in the second field of the greater than block so that the statement reads sound sensor value > 0.
  7. From the Looks palette, add the next background block to the forever if block.
    Time for action - switch backgrounds on sound
  8. Click the flag to start the script.
  9. Now clap. The background changes to black, then white, and on and on depending on how many times you clap.
  10. Let's watch the sound sensor. From the Sensing palette, select the sound option in the sensor value block. Then click the checkbox next to the sensor value block to display the sensor value watcher on the stage.

Note

If you do not have a PicoBoard, do not enable the sensor value block, as it may lock up your Scratch program or your computer.

Time for action - switch backgrounds on sound

Let's see what effect yodeling has. If anyone hears you, just blame it on an email you received, like this: "I can't believe someone would send that to me in an email." Your background should strobe to the sound of your yodel.

Click the stop sign to make Scratch stop accepting sound input.

What just happened?

Each time we made a sound, the stage flipped between night and day. Each time the PicoBoard registered a sound, the sensor assigned a numeric value to the sound that was measured on a scale from 0 to 100. Since our script was set to act on any sound greater than 0, we didn't need to be very loud to make the backgrounds switch.

Have a go hero

Barely a whisper will make our script swap backgrounds, and that's indicative of the code we used. If the sound sensor value is greater than zero, display the next background. Use the sound sensor value monitor on the stage to find the level of your normal speech. Then, adjust your script to switch to the next background only if the sound sensor value is above the level of normal speech.

Sound as a numeric value

The PicoBoard turns the sound we make into a number from 0 to 100, where 0 represents silence. In addition to the sound sensors, the light, slider, and alligator clips use a numeric value to represent the strength of the stimulus.

Using the numeric sound value, we can perform other math functions by supplying the sensor value block as an input value. Think of all the other places we can use a numeric value to affect how our sprites display on the stage. The Motion, Looks, Pen, and Sound palettes all have blocks that take numeric input to transform some feature of a sprite. For example, the move block accepts a number as a way to control how many steps a sprite takes.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.117.148.105