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: Inputs vs. Outputs
Up to this point, after you hit "Upload" for your code, all you could do is observe what it does. With the addition of buttons to your program, you now have the ability to interact with your code and change what Code Car does after you've uploaded code. This is also the point when you could show your Code Car to someone else and they could interact with it without being involved with the coding process. In computer science terms, the button is an 'interface' to your program, so that a simple action (pressing it) makes something complicated happen in the background, and then an outcome occurs (the light turns on!).
It's useful to think of inputs as things that send information into a program. Then the program can do something with that information. The do something is often a process that leads to an output.
New Concept: Decision Making
This program is the first time that your Code Car has 'made a decision' about what to do. To put it a different way, each time the void loop of the program runs, two different things could happen: the brake light could be signaled to turn on or it could be signaled to turn off. Almost any time there is an input to a program, there is a 'decision' being made. Imagine logging off of your computer. You go and find the 'log out' button and click it. Somewhere in the computer code, there must be a 'decision' made by the code so that "if 'log off' is clicked, run the log off process."
HOC: Decision Making
In this program, your code with 'make a decision' about whether or not to turn on the LED brake light. The decision is based on whether or not a button is pressed. In code, you express decisions with 'if' statements, a piece of code that runs only under certain conditions. You surely make hundreds of decisions each day, and each of those could be simplified down into an 'if' statement!
The rest of the lessons in this hour will be based on the different ways you can use 'if' statements to make multiple things happen, or how you can combine 'if' statements to work under many different conditions.
The if-else statements represent the importance of styling. The if statement is inside the void loop and the digitalWrite() statement is inside the if statement! The else statement is inside the void loop, but it's not inside the if statement. The example code is styled so that if you see a closing curly brace, you should be able to trace an invisible line straight upward to it's starting point. The closing curly brace of void loop, for example, is directly below the words 'void loop.'
Indentation also comes into play. When you see that something is indented, you can think of it as 'inside' something else. For example, the digitalWrite(7,HIGH); command is inside the 'if' statement, which is inside the void loop function. That may sound confusing, but that's all the more reason to style your code in a way that makes it more readable and clear.