Project 3.02 AND Logic

In this project you’ll learn about using AND logic with buttons to turn on LED1 only when both buttons are pressed.

What if you wanted to only turn on an LED when SW1 and SW2 were pressed at the same time? This is where AND logic comes into play. Our if-statements need to get a little more complicated to make this happen!

Project Code:

///////////////////////////////////////////////////////
// 3.02 – AND Logic

byte SW1 = 1;
byte SW2 = 0;
bool pressed = 0;

byte LED1 = 13;

void setup() {
  pinMode(SW1, INPUT);
  pinMode(SW2, INPUT);

  pinMode(LED1, OUTPUT);
}

void loop() {
  bool SW1State = digitalRead(SW1);
  bool SW2State = digitalRead(SW2);

  if (SW1State == pressed && SW2State == pressed) {
    digitalWrite(LED1, HIGH);
  }
  else {
    digitalWrite(LED1, LOW);
  }

}
///////////////////////////////////////////////////////

*If you’re copying and pasting the code, or typing from scratch, delete everything out of a new Arduino sketch and paste / type in the above text.

We know that we want to have an LED turn on when both buttons are pressed but how do we do it? Breaking down a problem into smaller, more manageable chunks is a priceless skill in programming. That being said, let’s break this down.

First, we need to create the variables to represent our button pins, a variable to represent the pressed state, and a variable to represent our LED:

byte SW1 = 1;
byte SW2 = 0;
bool pressed = 0;

byte LED1 = 13;

Next, we need to set the correct pinModes for the pins we plan to use. Buttons are inputs and LEDs are outputs:

void setup() {
  pinMode(SW1, INPUT);
  pinMode(SW2, INPUT);

  pinMode(LED1, OUTPUT);
}

Now we need to create the loop() functions code. The first thing we need to know is if the buttons are being pressed. To do that we digitalRead the button pins and store them in bool type variables for later use:

void loop() {
  bool SW1State = digitalRead(SW1);
  bool SW2State = digitalRead(SW2);

Next, we need to make a decision. How do we make a decision? We need to use an if-statement. The decision we need to make is if SW1 AND SW2 are pressed.

  if (SW1State == pressed && SW2State == pressed) {

As promised, the conditional got a little more complicated. The “&&” symbols in the conditional are new. This is the programmatic way to say AND. The conditional will evaluate to be true if what is on the left side of the “&&” symbols and what is on the right side of the “&&” symbols are both true. The left AND right sides need to be true for the code to execute.

 If true, it turns LED1 on:  

    digitalWrite(LED1, HIGH);
  }

Else, if it is not true, it turns LED1 off:

  else {
    digitalWrite(LED1, LOW);
  }

AND actually has a very strict definition in programming. It can be represented by what is known as a “Truth Table”. It shows all the possible outputs based on the inputs and logic being applied. Here is the AND Logic table:

Input A Input B Output
0 0 0
0 1 0
1 0 0
1 1 1

As you can see, the output is only 1 if both inputs are 1. In other words, if input A AND input B are 1, the output is a 1.

For a more familiar example let’s make the table based on our program’s inputs:

SW1State == pressed SW2State == pressed Output
0 0 0
0 1 0
1 0 0
1 1 1

As you can see, the output is only a 1 if SW1State and SW2State equal 1; I.e., both buttons are pressed.

Previous
Previous

Project 3.03 OR Logic

Next
Next

Project 3.01 Blink an LED with a button