Hold Button for LED

This code ties two conditions together- the button’s condition is directly related to the LED’s condition. Because each of them is using only digital logic, there are only two condition (HIGH or LOW) for the button and for the LED.


/* * Button held on pin A5 turns LED attached to pin 11 on */ void setup(){ pinMode(11, OUTPUT); //LED pin pinMode(A5, INPUT_PULLUP); //pushbutton pin } void loop(){ //if the pushbutton is pressed, hold the LED on if (digitalRead(A5) == LOW){ digitalWrite(11, HIGH); } else { digitalWrite(11,LOW); //otherwise, turn LED off } } // (c) 2018 Let's Start Coding. License: www.letsstartcoding.com/bsdlicense


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!


Read the Code Walkthrough Text (click to open)


New Concept: If-Else Statement

Most code is designed to interact with something outside of itself. Code may receive signals from another program, input from a human, or data may come into a program from the natural environment. 

In order to have code that interacts with the world around it, the code needs to 'have options' or be able to 'make decisions'. The if-else statement is one way to provide options to a program.

When you are creating a condition for code to act on, you must be very specific. When do you want the code to make a decision? What is that decision based on? And what action should occur after the decision is made? What are the alternatives to the decision?

Those are the questions that the if-else statement answers. First, look at the structure of an 'if' statement. This is just the pattern for the code, not actual code:
if(condition here is true){

   run these actions;


This code answers the question "What is the decision based on?" in the parentheses. You will usually put a comparative statement in those parentheses. For example, if(temperature > 90) means that the actions will only run if the temperature is greater than 90. 

The content inside the curly braces is what should happen if the condition is true. This can be any valid code statement, including more if statements or loops. 

The alternatives to an if statement's condition can be determined multiple ways. Here, you'll just learn about the else statement. 

As the code reads through, it will check the condition inside the if statement's parentheses. If that statement is not true, it skips the content inside the curly braces. Typically, there will be an else statement after an if statement. 

Keep in mind that the else is only valid code immediately after an if statement. You cannot write an else statement without first writing an if statement. You also cannot put any code between the closing curly brace of the if actions and the word 'else' or the code will not compile. 

The else just means "In all other cases....". It doesn't require a condition to be true, it just requires the if statement to be false. The else provides a single alternative to the if statement.

Finally, keep in mind where you place an if-else statement. Using the example above, you would probably want to place the if-else statement below some code that checks the temperature. Otherwise, you'll be making a decision based on old readings. 


If you're having trouble, try to run a program that can help you find the answer.

1. Which symbols (called comparators) check to see if two things are exactly equal to each other? How did you check to see if the digitalRead was equal to LOW?

2. How many arguments does the digitalRead function require?

3. True or False: You must have an 'if' statement in order to use an 'else' statement.