To create an LED flashlight, you need to set up a button that 'holds its state', meaning it can clicked on and then clicked off.
New Concept: Variables
A variable is a word that's used in place of a number. They are an important concept in all programming languages.
Think about any thermometer: it can display many temperatures, but only one temperature at a time. The number that the thermometer is showing at any time is called 'temperature'. More precisely, it's "temperature right here, right now". Temperature is constantly changing: it's variable.
The thermometer doesn't 'know' how warm it is, but when you look at the thermometer, you can see what value temperature is right then. Variables in code are the same way- they can change throughout the program and you can use them to measure things, count things, or keep track of things that change over time.
New Concept: = versus ==
This small difference is very important. A single equals sign means "is equal to." as a statement. A double equals sign asks "are these two things equal?" as a question.
When you compare two things, you use the double equals sign. This will be common in if statements and while statements.
When you're setting the value of a variable, you'll use the single equals sign.
New Concept: Scope
Scope refers to which parts of a program can work together. This concept applies mostly to variables, their names, and where they are declared. A variable declared inside the curly braces of void setup() can't be used outside of void setup(). That variable is limited to the scope of void setup().
In general, programs contain functions- like void setup and void loop- and functions contain statements like the if statement. A variable that is created in a statement can be used only inside that statement.
If you want to use a variable in all parts of your program, you declare it at the program level- called the global scope. Now all functions and statements can interact with this variable at any time.
Keep in mind that when you create a new statement or function, you're opening a new, smaller scope. When you close that statement or function, you close that scope. The variables inside that scope are only available inside that scope.
QuizIf you're having trouble, try to run a program that can help you find the answer.
1. Because the pressCount variable is outside of any function (above void setup() and void loop()), you can refer to it as a ____________ variable.
2. Which of these is the best definition of variable?
3. What goes between the parentheses of an 'if' statement?