Skip to content

Please update your browser

Your browser version is not supported by this site. CS First may not work properly. Learn more.

4. Control The Speed

Transcript

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.

To make your program easy for anyone to use it needs a slider that allows the DJ to adjust the speed of the ya sound.

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 variable in new and choose make a variable.

Name that variable something that makes sense for the sound you picked.

For example ya.

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 slid 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 need to place the variable in the code blocks to make it do something.

Drag the ya speed block into the wait block.

Once you add the variable to the code try moving the slider.

♪ Ya ya ya ya ya ♪ (upbeat music)

The ya sound occurs more or less frequently depending on where the slider is set.

The variable is working.

Nice.

♪ Ya ♪ If you put the slider on 100 then the ya won't click to 100 seconds because the code reads.

Wait for the value stored in ya or 100 seconds before continuing.

In a song, 100 seconds is a really long time.

And right now the lowest value you could use would be one second.

And you might actually want this sound to play for every half second.

You need to do a little math to be able to set the values to smaller numbers.

Take a look at how you could mix the code to try to figure that out.

In this example the say block instructs the program to say whatever is in the ya variable.

Here it says two.

Because the ya is set to two.

You don't have to use the say block in your program.

But it illustrates the change you are about to make in your code.

Go to the operators menu and drag out a divide by block.

And place it inside the wait block.

Put the ya variable into the first value.

And type 10 into the second value.

This now reads wait for ya variable divided by 10.

Which means that instead of waiting for two seconds the code now waits now for .2 second.

Because two divided by 10 is .2 seconds.

This makes the ya sound more frequent.

♪ Ya ya ♪ As the ya variable slider is moved the sprite says numbers like 3.5 instead of 35 like before.

That means the program will pause for only 3.5 seconds.

Not 35 seconds between ya's.

This give 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 a 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 small changes to figured out how to get the answer you want.

You may 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 program to work exactly the way you want.

But the great thing about computer science is that it's easy to try out ideas and 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 first 10 tries.

Some of the best ideas start as mistakes.

Test out the program.

♪ Ya ♪ As a slider is moved the ya sound happens faster or slower.

♪ Ya ♪ (Upbeat music)

♪ Ya ♪ Perfect.

Add a new variable and name it something that makes sense for the sound that you chose.

Double click the variable two times to change it into a slider.

Use the operators menu to build a block then divide your variable by 10.

Then place the hold block inside the wait block.

When you have finished these steps you will have completed the core part of DJ Mixer.

But you can return to this tab to try some fun add-ons by clicking the green arrow.

arrow_backward Back
Next arrow_forward
Instructions
  1. Create a variable to control the speed of your new sample sound.
  2. Convert the variable to a slider bar to make it easier to control the sound.
  3. Use the variable and an "operator" block in the "wait" block that controls the sample sound.