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

Create an alarm that flashes the brake lights and beeps a tone when you press the 'reverse' button on Code Car, letting everyone in your path know to look out!


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.

/* * Use a single button to flash the brake lights and beep the horn */ void setup(){ pinMode(11,INPUT_PULLUP); //button to activate flashing and beeping pinMode(7,OUTPUT); //brake light pinMode(2,OUTPUT); //speaker / horn } void loop(){ if(digitalRead(11)==LOW){ digitalWrite(7,HIGH); //turn on LED 7 (tail light) tone(2,500); //play a tone of 500 hertz delay(300); //wait 300 milliseconds digitalWrite(7,LOW); //turn off LED 7 noTone(2); //silence the tone on pin 2 delay(300); //wait 300 milliseconds (before checking 'if' condition again) } else{ //otherwise, if the button is not pressed... digitalWrite(7,LOW); //ensure the LED is off noTone(2); //silence the tone on pin 2 } }//This curly brace closes 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!


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: Even Bigger Conditionals ('if' Statements)

In this example, you're including all of the output code commands you've learned up to this point - digitalWrite, delay, and tone, inside an 'if' statement. Any action that you can do inside void loop(), you can do inside an 'if' statement. 

You may have noticed in this program that the button isn't as responsive as some of the previous programs. In past programs, releasing the button press has immediately allowed the code to run the 'else' statement. This is a reminder of how fast code runs without any delays- in those programs, the instant you released the button, the code ran through the rest of the loop, back to the top, and hit the 'else' statement. When you have delays inside the 'if' statement, the code can't run through the loop as quickly, so the response of releasing the button is slower.

New Concept: Precision Placement

If you tried the challenges in this lesson, you may have had to experiment a few times with how to get the LED light to turn on while the button wasn't pressed. You may have even needed to use your visualization and imagination skills to think about how the new digitalWrite() commands would interact with the existing code! When you start modifying code, where you insert commands can be just as important as which commands you insert.


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

Which of the numbers in the code could you replace with a variable?

To reverse the way this program works- have the backup alarm on by default and only off when the button is pressed, what would you do?