Advanced Blaster Sound Effects

Take your laser blaster sound effects to the next level using your latest and greatest coding skills.

Code

The code in the editor below already works Just plug in your Code Rocket and press upload to see what it does! Tinker with it and make changes to see what each line of code controls. 

/* Fire laser 7 when you press button 11. * Fire laser 8 when you press button 12. * Use the speaker to add a tone when the laser fires. */ void setup() { pinMode(2,OUTPUT); //speaker pinMode(7,OUTPUT); //blaster LED 7 pinMode(8,OUTPUT); //blaster LED 8 pinMode(11,INPUT_PULLUP); //button 11 pinMode(12,INPUT_PULLUP); //button 12 } void loop() { if(digitalRead(11)==LOW){ //if button 11 is pressed digitalWrite(7,HIGH); //turn on LED 7 //A fast falling tone makes a better laser sound (like the laser 'bullet' is getting further away) than a single tone does. for(int i=800;i>500;i--){ //Starting at 800 and decreasing to 500 tone(2,i); //play the tone with pitch equal to variable 'i' delay(1); //delay 1 millisecond before changing the pitch and playing again } } else if(digitalRead(12)==LOW){ //if button 12 is pressed digitalWrite(8,HIGH); // turn on LED 8 // This tone falls from 1000 to 700, playing a different tone every 1 millsecond for(int i=1000;i>700;i--){ // Starting at 1000 and decreasing to 700 tone(2,i); //play the tone with pitch equal to variable 'i' delay(1); //delay 1 millsecond } } else{ //Otherwise if no buttons are pressed digitalWrite(7,LOW); //turn off the LEDs digitalWrite(8,LOW); noTone(2); //stop the tone on the speaker } }
 

Walkthrough Videos

Watch the videos for line-by-line explanation of how the example program works. Then you'll be ready to make some changes of your own!

Challenges

Can you complete the challenges? Change the code in the editor above, then upload it to your Code Rocket and see if you got the result. Don't stop here, keep experimenting with the code!

Concepts

These are the new code concepts covered in this example program. To become a great coder, read through these concepts to learn new vocabulary.

New Concept: Nesting

You’ve been using this concept in nearly every program so far, but it’s time to give it a name. Nesting is about organizing your code, both visually (to make it easier for humans to understand) and logically. In this program, you have a void loop. Inside that, there is an ‘if’ statement. Inside that, there is a ‘for’ loop. Inside the ‘for’ loop are tone and delay commands. To help display that logic clearly, programmers will usually indent each ‘layer’ of the nested statements deeper than the last. That is why you’ll sometimes see multiple closing curly braces ( } ) on one line after another. Each curly brace is ending a different ‘layer’ of the nested statements above.

Now let’s think about why you need to nest. Working backward, if you wrote out the intention of the first ‘blaster’ sound effect, you might write:

“Play a tone of ‘i’ on pin 2. Only do that if the ‘for’ loop condition is true. Only check the ‘for’ loop condition when if the ‘if’ statement is true.”

In this case, you have two conditions that both have to be true in order for your tone to play. The layers of nesting are what allow you to check all those conditions in sequence.

Quiz

If you're having trouble, try to run an experimental program or look at the example code to help you find the answer.

1. How many milliseconds does it take for one of the 'for' loops to run in the example code? Hint: Take the number of times the loop runs and multiply it by any delays inside the for loop.