// Copyright 2007, Sandeep Gangadharan // For more free scripts go to http://www.sivamdesign.com/scripts/
 
Blink Hookup: 1 LED in pin 13. Remember shorter leg of LED is ground.
 

Take your horn beeps to the next level with more tones and tunes! Beep the horn on Code Car automatically in song sequences.

Code

The code below already works and is ready to upload! Upload the code to Code Car and see what happens. Then, you'll 'take apart' the code to learn what each piece of the program does.

/* * Play a tune on the horn of Code Car */ void setup() { pinMode(2,OUTPUT); //set the speaker to OUTPUT } void loop() { tone(2,250); //send a tone of 250 hertz to pin 2 delay(1000); //wait 1 second before running the next line of code tone(2,500); //send a tone of 500 hertz to pin 2 delay(1000); tone(2,1000); //send a tone of 1000 hertz to pin 2 delay(1000); noTone(2); //stop all tones on pin 2 delay(1000); //wait 1 second before the code starts the loop again } //this brace ends the void loop // (c) 2017 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

How many can you complete? Change the code according to the challenges below. Upload your code to see the effect when you're finished. Complete a challenge? Check it off the list!

Use the internet to find a table that converts hertz to musical notes. Can you make some of the following songs?



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: Repetition and Patterns (Again)

The challenges on this lesson are very open-ended, and they can get long. Some students have written hundreds of lines of code to create their favorite song from a movie or video game. Most songs, like most code, is made up of patterns that repeat or at least use the same tools (notes and pauses, for example) to create something unique. So while it may seem intimidating to look at a huge block of code, remember to try to pick out the patterns and deconstruct the program into its pieces.

 

New Concept: Styling

The longer your programs become, the more important styling becomes. There is more than one way to style code, but your goal should be to make your code readable to yourself and to anyone else who reads it. Looking at the coding example above, you can see that everything inside the void loop() is indented one line. That's not so the computer can read it, it's so programmers can easily see where the loop ends (it's the closing curly brace that's not indented). Also notice the blank lines between each tone+delay pair. That doesn't affect how the code runs, but it breaks the program into 'notes' and 'rests' that are easier for humans to read.


Quiz

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

If you type:
tone(2,500);
noTone(2);
how long will you hear the tone of 500 hertz?





What styling tool(s) can you use to make your code more readable?