Ascend to Charge Part Three: Variable Notes

Convert your piano notes to hertz values one time, then use your variables throughout the code to quickly create whatever song you’d like!

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 int C = 523; int E = 659; int G = 784; int A = 880; //C,E,G,A,E,G int note = 150; //how long each note plays void setup(){ pinMode(10,OUTPUT); //Set the Speaker as an OUTPUT } void loop(){ tone(10,C); delay(note); tone(10,E); delay(note); tone(10,G); delay(note); tone(10,A); delay(note*2); //This note is held for twice the value of the variable tone(10,E); delay(note); tone(10,G); delay(note*4); //Hold the end note for four times the value of the variable. 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: Variables- one step further.

Here, you’ve taken variables to the next level, using them in place of any number that is likely to change as you modify your song or create a new song. Now that you have some of the common notes mapped out, you could change the void loop() very quickly to create different tunes. As programs grow more complex, you may see fewer and fewer ‘hard-coded’ values and more variable values that replace those numbers.

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. When creating variables in this program, 'int' is short for what?