Goal: Add sound and multi-colors as a spiral grows.
to spiral :length :pitch :whatcolor
if :length > howbig [stop]
fd :length rt angle
carefully [note :pitch 3] [ ]
spiral :length + howtight :pitch - 1 :whatcolor + 1
We can add sound to the project with the note
- 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.
- A procedure can receive more than one input, if we define it that way. These inputs can have different values in the recursive call (when the procedure name is used again within the procedure).
- In this project, we add an input 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 an input 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 understands that we are talking about the whatcolor variable and not the whatcolor slider. The recursive call 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 instruction.
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, pitch 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 is the highest pitch and the lowest pitch you can hear? (In the command center, write different note commands, such as note 120 5.)
Alter the button and the last line of the procedure so that the notes get higher instead of lower.
On Your Own - Project Ideas
Can you add a fourth input to the definition of the procedure and adjust the procedure itself and button to use it? (Perhaps the turtle could grow in size as it draws, or the angle could gradually increase or decrease?)