Arduino Tutorial 4: If Statements

In this project we will be creating a device which does the following:

  • Contains 2 LED lights. One Red, the other Green
  • The device also contains a button.
  • Normally, the red light is on and the green light is off.
  • But if the button is pushed, the green light turns on and the red light turns off.

The end result will look like this: (gif may take time to load)

Components Needed

  1. Arduino Uno
  2. Breadboard
  3. Various Wires
  4. 2x 220Ω Resistor (Red, Red, Brown, Gold colored bands)
  5. A Green LED Light
  6. A Red LED Light
  7. A Pushbutton
  8. 1x 10,000Ω Resistor (Brown, Blue, Orange, Gold)

Pull Down Resistor and Digital Inputs

This project will involve something you’ve not been introduced to yet.

We’ve seen the line of code “digitalRead(pin)” before… But what does it do?

Well, when you call it, it results in either 0 (Which also means false, or LOW) and 1 (or true and HIGH). It is 0 if there is no voltage coming through the pin and conversely, it is 1 if there is voltage coming into the pin.

However, it’s not quite this simple.

Small amounts of electricity and static in the air cause the input to sometimes report voltage. Even when we don’t want it to. This is referred to as the pin FLOATING.

In order to prevent this we need to make sure the pin is only HIGH when we intend it to be (Which in our case is when the button gets pushed). We do this with a PULL DOWN RESISTOR.

PULL DOWN RESISTOR is a very powerful resistor (We use the 10,000Ω resistor) connected to ground on one end and the input source on another. This grounds the excess voltage, and only allows voltage through if it is powerful enough to pass the resistor (Such as the 5v from the Arduino)

Confused? No worries! Understanding this specific topic is not critical to your success.

Image below shows a Floating pin. If you called digitalRead(8); in code, sometimes it would be HIGH even when the button is not pushed.
Image below shows a pull down resistor fixing a floating pin! Now digitalRead(8) will only be HIGH when the button is pushed.

HINTS

Please use hints only as you need to! Strive to figure as much out yourself as possible. (To use a hint, simply tap on the boxes below)

WHERE DO I EVEN START? AAAAHHHH!

Whoa hold on there! Let’s take this one step at a time. First we should start by making the circuit for the device. Once we think we have a circuit that should work, then lets move on to the programming portion.

[collapse]

 

The Circuit

What am I trying to do again?

You are trying to build a circuit that has two LED lights and a PushButton that are each connected to different digital pins.

[collapse]
Where should I start with the circuit?

For now, forget about the button and the pull down resistor that goes with it. Just focus on hooking up two LED lights, each controlled from a different digital output pin.

[collapse]
How do I wire an LED light again?
Okay, so where does the button come in?

The purpose of the Pushbutton is that when the button is pushed, voltage should flow to an input pin (So we can tell it was pushed) The Pushbutton should be attached to a digital input pin on one connector, and a constant supply of voltage on another. You can use the “5v” pin on the Arduino to provide a constant voltage to something. Ask for help to determine which of the Pushbutton pins become connected when the button is pushed.

[collapse]
Do I need a Pull Down Resistor?

The pulldown resistor as explained above is necessary here. The reason for this, is because we need to know when the button is pushed. And if any little bit of static in the air is going to make the arduino THINK the button is pushed… Well thats no good. Use the diagrams above as a reference. It should be placed after the push button but before the wire going to the input.

[collapse]
I give up! Just show me the circuit already.

 

The Program

I am solidly overwhelmed. What am I doing?

We need to make a program that checks to see if the button was pushed. If it was, we need to turn on a green light. Otherwise, the red light stays on.

[collapse]
Okay... So where do I start?

Same place we usually start!

I would begin by creating variables at the top of your sketch that act as shortcut names for the pins that you used.

For example:
int greenLED = 12;

But remember, the pin number will be whatever you have wired your green LED light to.

After that, I would setup our inputs and outputs using the pinMode statement.

[collapse]
How do I check if the button is pushed?

This concept is new, so no worries if you couldn’t figure it out on your own!

We actually want to save the state of the button into a variable.

Start by creating a new variable at the start of the Loop function like so:
bool buttonIsPressed;

Note that it’s a bool. This means that it holds a value of either true or false. Remember how we mentioned the digitalRead function? It also results in true and false.

So after we create our variable enter this line…

buttonIsPressed = digitalRead(buttonPin);
Where buttonPin is the pin number you connected the PushButton to.

This will make buttonIsPressed hold the state of the button. If the button was pushed, it will be true. Otherwise, it will be false.

If you wanted to, you could create and set this variable in the same line of code! Can you figure out how?

[collapse]
How do I turn on a light only if the button is pushed?

Enter the IF statement!

Remember how we said that an If statement will only execute the lines of code inside it if the statement within the parenthesis is true?

We already have a variable that is true or false depending on if the button is pushed.

All we need to do is use that in an If statement.

The syntax for an If statement is as follows:
if(condition){

}

[collapse]
I need more direction about If statements!

No problem! In the specific case of this program, you’ll need to write the following:

if(buttonIsPressed){
// Any lines of code in here only get run if the button was pressed…
}else{
// Otherwise, any lines of code in here get run.
}

[collapse]
Both lights are turning on! What did I do wrong?

Whenever you turn one light on, remember to turn the other off.

[collapse]
Can I PLEASE see the code?

I SUPPOSE! Remember to right click and choose “Save Link As” Arduino Lesson 4 Code Here

[collapse]