Using a variable, set the 'mode' of the headlight to on or off with the press of a button, so you don't have to hold a button to keep your headlights on all the time!
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!
New Concept: Variables to Set "Modes"
If you spell out the logic of this program, the press of your button sets the value of the variable pressCount, then the value of pressCount determines whether or not the LED is lit. Because the program can 'remember' the value of the variable, when you use the button to change the variable, you're changing the 'mode' of that LED. You have probably flipped on a light switch today or maybe turned a key in a lock. Those are physical representations of 'on/off' or 'locked/unlocked' like what you're recreating in code. Unlike the key or the lock, though, your code variables can have any number of 'modes' or values, so that pressCount = 1 does something different than pressCount = 2, 3, 4, or so on.
QuizIf you're having trouble, try to run an experimental program or look at the example code to help you find the answer.
Why is it important to set pressCount=0 in the 'else' statement?
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?