// Copyright 2007, Sandeep Gangadharan // For more free scripts go to http://www.sivamdesign.com/scripts/

Beep the Horn on Code Car

 
Blink Hookup: 1 LED in pin 13. Remember shorter leg of LED is ground.
 

Start making noise with code! Beep the horn on Code Car automatically.

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.

/* * Beep the horn on 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 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!



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: Comments as Experiments

Coding is a series of experiments to get a final result. You may come up with an idea, test it, observe the result, and then modify your idea, test it, and so on. When you're new to coding, the typing of code can be a challenge, so you can use comments to remove code from your program without deleting it. Remember, the computer doesn't read your comments, so if you place two forward slashes (//) in front of a line of code, it's the same as deleting it (to the computer). But if you want to get that code back into your program, just delete the two slashes and the code is back in action! 

 

New Concept: Code Won't Stop Without a Command

You saw this concept in action if you completed the challenges of this lesson. By 'commenting out' noTone() , you removed the only thing telling Code Car's horn to stop blaring! This might have been a reminder that your program doesn't "know" what you want it to do. You're in control, and the program simply runs your commands.

Quiz

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

To 'comment out' a line of code, you __________________.



What arguments (pieces of information) does the tone() function require? Make sure they're in the right order!