Ascend to Charge Part Two: Variable Delays

Replace the note delay with a variable so you can easily speed up or slow down the pace of your song as you ‘tune it in’.

Code

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

/*'Ascend to Charge', a popular song during baseball games C,E,G,A,E,G are the notes in order Using variables instead of numbers makes it easier to change the song */ int note = 300; //a variable named note to replace the delay values void setup(){ pinMode(10,OUTPUT); //Set the Speaker as an OUTPUT } void loop(){ tone(10,523); //C delay(note); //Play the note for 150 milliseconds tone(10,659); //E delay(note); tone(10,784); //G delay(note); tone(10,880); //A delay(note*2); //double delay on this note tone(10,659); //E delay(note); tone(10,784); //G delay(note*4); //quadruple delay on the last note noTone(10); delay(5000); //wait 5 seconds before replaying the song } // (c) 2018 Let's Start Coding. License: www.letsstartcoding.com/bsdlicense
 

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 challenge? Change the code in your code editor above. Upload your code to see the effect when you're finished. Complete a challenge? Check it off the list!


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: Reducing repetition

Whenever a programmer finds themselves repeating something over and over again, it’s time to look for a new way of doing things. If you’re trying to write a song and you have to change every single delay each time you have a ‘test listen’ to see if the song sounds right, it’s time to use variables. Here, the variable allows you to very easily change the speed of the song and reduces the amount of typing you have to do as you tweak your code.

Quiz

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

When creating a variable, you have to decide 3 'qualities' of the variable in order. What's the order?