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

Backup Alarm on Code Car

 
 

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!

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.

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

Quiz

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?