// 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.

Honk your horn with a button on Code Car. What sounds will you make?


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.

/* * Press button 9 to honk the horn on Code Car */ void setup(){ pinMode(9,INPUT_PULLUP); //Button to activate the horn pinMode(2,OUTPUT); //Horn on the Code Car } void loop(){ //if the pushbutton is pressed, start playing a tone if (digitalRead(9) ==LOW) { //if the button is pressed tone(2,250); //the code between the braces is the 'action' } else{ // an else statement runs when the 'if' condition isn't true noTone(2); //otherwise, silence the horn. } } // (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!


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!


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: Continuing with 'if' statements

The 'if' statement is worth spending another lesson on, since it changes the entire way you can think about programming. Code Car only has one type of physical input- the button, but you can trigger any sort of output with it.

To put it another way, anything that you can run inside a void loop, you can also run inside an 'if' statement. It could be one line of code or it could be 20 lines of code - there is no limit. When you do that, the code inside the 'if' statement only runs when a certain condition is true. If you didn't get enough challenge out of this lesson, think about some of the code in previous lessons, like songs, that you could now trigger with an 'if' statement.


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

What types of code commands can run as an 'action' inside an 'if' statement?

An 'if' statement always has two basic parts. What are they?