Goal: Add sound and multi-colors as a spiral grows.
Vocabulary: note
to spiral :length :pitch :whatcolor
pd
if :length > howbig [stop]
setc :whatcolor
setpensize howthick
fd :length rt angle
carefully [note :pitch 3] [ ]
spiral :length + howtight :pitch - 1 :whatcolor + 1
end
to reset
cg
end
Notes We can add sound to the project with the note command.
- Note takes two inputs. The first sets the pitch and the second sets the duration of the note (in tenths of a second). The sounds are rather soft, so you may need to turn up the volume on your speakers. If the pitch is very high or very low, the human ear cannot hear it.
- We can place two or more variables in the naming line of a procedure. These variables can all change value in the recursive call (when the procedure name is used again within the procedure).
- In this project, we add a variable named pitch and use it as the first input to note to control the pitch of the note. (We could name it anything, but naming it pitch reminds us of its purpose.)
- We also add a variable named whatcolor. Notice that we still have a whatcolor slider, and we use its name in the spiral button. This sets the starting color to the value of the whatcolor slider.
But in the procedure itself, when we place a colon before whatcolor throughout the procedure, the computer ignores the whatcolor slider. Instead, it increases the value of whatcolor by 1 each time spiral is called. (You might find it less confusing to rename either the whatcolor slider or the whatcolor variable, but we've kept them the same here to demonstrate that it is possible!)
- Spiral needs three inputs in this project. We must include three inputs on the spiral button.
The first input sets the length of the first line.
The second input sets the pitch of the first note.
The third input sets the starting color.
- We must also be sure to include three inputs on the last line when spiral is called.
In the recursive call, note decreases in value. The notes get lower as the spiral gets bigger.
- But we need to be careful! The pitch cannot go lower than 1 or we will get an error message. So we place the note command inside of a carefully command. (See Spirals 2.) If the value of pitch becomes less than 1, carefully runs the second input. In this case, the second input is simply empty brackets [ ]. That means that nothing will happen! But this allows the spiral to continue to grow in silence without producing an error message.
If you create large spirals with this project, you may notice that the notes get too low to hear, and then after the spiral continues to grow slowly, suddenly it speeds up. When that happens, we know that the value of pitch has fallen below 1, so the note command has stopped running without producing an error and the spiral now grows more quickly as it no longer needs to pause for the duration of each note.
What If?
What is the highest pitch and the lowest pitch you can hear? (In the command center, write different note commands, such as note 120 5.)
Challenge
Alter the button and the last line of the procedure so that the notes get higher instead of lower.
|