In this screencast, you will learn about an important computer science concept called a variable. In computer science, variables are used to hold values. You will make a slider that uses variables to control the frequency of your sound. Watch this screencast to learn how to make and use this variable, then try it on your own.
To make your project easy for anyone to use, it needs a slider that allows the DJ to adjust the speed of the "meow." The computer needs to remember the speed the DJ chooses, so that the code can use the value that the DJ sets when the sound plays. A variable can save that information. To add a variable, go to the data menu and choose "make a variable."
Name the variable something that makes sense for the sound you picked, for example, "meow."
Click OK, and the new variable shows up on the left side of the screen as a number. If you double click on it twice, it changes into a slider control. Now, when you slide the control, the number inside the variable changes. This provides the DJ with a tool to easily set the variable on the fly, but you still need to place the variable in the code blocks to make it do something. Drag the "meow speed" block into the "wait” block.
Once you add the variable to the code, try moving the slider. What happens? The meow noise occurs more or less frequently, depending on where the slider is set. The variable is working! If you put the slider on 88, then the meow won't play for 88 seconds because the code reads: "Wait for the value stored in meow, or 88 seconds, before continuing." In a song, 88 seconds is a really long time... and right now the lowest value you could use would be 1 second...and you might actually want the sound to play every half second. Wow, you need to do a little math to be able to set the values to smaller numbers. Take a look at how you can tinker with the code to try to figure that out. In this example, the “say” block instructs the program to say whatever is in the meow variable. Here, it says “2” because the meow is set to 2. You don’t have to use the say block in your project, but it illustrates the change you are about to make in your code. Go to the operators menu, drag out a "divide by" block, and place it inside the “wait” block. Put the "meow" variable into the first value, and type 10 into the second value. This now reads: "Wait for meow variable divided by 10," which means that instead of waiting for 2 seconds, the code now waits for .2 seconds, because 2 divided by 10 is .2. This makes the meow sound occur more frequently. As the meow variable slider is moved, the sprite says numbers like "3.5" instead of "35" like before. That means the program will pause only 3.5 seconds, instead of 35 seconds, between meows. This gives the DJ much more control over the sound, since it can be set to play at fraction-of-a-second intervals, instead of just at whole-second intervals.
This example used the “say” block as a test to help you see what was happening as the code changed. It’s not an important part of the program, so it can be removed.
Sometimes, it takes a lot of tinkering to figure out how to get to the answer you want.
You might need to temporarily add the "say" block--or another block--just to make it easier to see what happens when you change variables or other parts of the code. It may take several tries to get the math or other parts of your project to work exactly the way you want.
But, the great thing about computer science is that it’s easy to try out ideas and just keep making changes until you are happy with the result. As long as you persist and keep approaching a problem from different angles, you will solve it eventually. There's no harm in being wrong on the first try...or the first 10 tries! Some of the best ideas start as mistakes. Test out the project. As the slider is moved, the meow sound happens faster or slower. Perfect! Now, it's your turn. From the Data menu, add a new variable and name it something that makes sense for the sound you chose. Double click the variable two times to change it into a slider. Use the operators menu to build a block that divides your variable by 10, then place the whole block inside the “wait” block. When you've finished these steps, you'll have completed the core part of the DJ Mixer, but you can return to this tab to try some fun add-ons by clicking the green arrow.
- Create a variable to control the speed of your new sample sound.
- Convert the variable to a slider bar to make it easier to control the sound.
- Use the variable and an "operator" block in the "wait" block that controls the sample sound.