LED Flashlight

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.

Code

/* * Use a button to turn a light on and off */ int pressCount = 0; // count how many presses on the button void setup(){ pinMode(12,OUTPUT); //LED pinMode(A5,INPUT_PULLUP); //pushbutton } void loop(){ //if the button is pressed, increase the value of pressCount by one if(digitalRead(A5) == LOW){ pressCount++; //++ is shorthand for pressCount = pressCount + 1 delay(250); //This delay keeps one press from counting many times } if(pressCount == 1){ //if pressCount is 1, turn on the LED. digitalWrite(12,HIGH); } else { //otherwise (pressCount not equal to 1), turn off the LED digitalWrite(12,LOW); pressCount = 0; //reset the count to 0 to start over. } } // (c) 2017 Let's Start Coding. License: www.letsstartcoding.com/bsdlicense
 

Challenges

How many can you complete? Change the code and hardware according to the challenges below. Upload your code to see the effect when you're finished. Complete a challenge? Check it off the list!

Videos

Read the Code Walkthrough Text (click to open)

Concepts

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.

Quiz

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