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!
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?