Learn to code

ARDUINO & CODES

Previous Post
Making a toggle switch button with Arduino
Next Post
Reliable detecting a switch with Arduino

Creating multiple button states with Arduino

in Arduino Digital / Analog

In this tutorial, we will control 3 LEDs with a single button switch. I have added 2 new LEDs to pins 9 and 8. Every time the button is pressed the switch is closed and sends a HIGH signal. If this happens, the LED will light up in a row. On the 4ht press on the button, the LEDs will turn off and the loop starts over again. To keep track of which LED is active we will count the number of presses with a variable called state.

Parts you will need

Arduino Uno Rev3 Arduino Uno Rev3 × 1
Breadboard 400 point Breadboard 400 point × 1
Dupont Wires Dupont Wires × 8
LED LED × 3
220 ohm resistor 220 ohm resistor × 1
10K Resistor 10K Resistor × 1

Arduinoplatform is a participant in several affiliate programs. This means that I will earn a commision if you buy a product from the affiliated websites through clicking on the links provided above.

BreadBoard Layout

The Code

//  Sketch: Multiple states of the button
//
//  An  example of using a single button switch to set multiple states or conditions
//
//  state holds the current status.
//  0 = all off.
//  1 = green LED
//  2 = yellow LED
//  3 = red LED
// Define the pins being used
int pin_LEDgreen = 10;
int pin_LEDyellow = 9;
int pin_LEDred = 8;
int pin_switch = 2;
// variables to hold the new and old switch states
boolean oldSwitchState = LOW;
boolean newSwitchState = LOW;
byte state = 0;
void setup()
{
  Serial.begin(9600);
  pinMode(pin_LEDgreen, OUTPUT);    digitalWrite(pin_LEDgreen, LOW);
  pinMode(pin_LEDyellow, OUTPUT);   digitalWrite(pin_LEDyellow, LOW);
  pinMode(pin_LEDred, OUTPUT);      digitalWrite(pin_LEDred, LOW);
  pinMode(pin_switch, INPUT);
}
void loop()
{
  newSwitchState = digitalRead(pin_switch);
  if ( newSwitchState != oldSwitchState )
  {
    // has the button switch been closed?
    if ( newSwitchState == HIGH )
    {
      // increase the value of state
      state++;
      if (state > 3) {
        state = 0;
      }
      // turn all LEDs off. Doing it this way means we do not need to care about the individual LEDs
      // simply turn them all off and then turn on the correct one.
      digitalWrite(pin_LEDgreen, LOW);
      digitalWrite(pin_LEDyellow, LOW);
      digitalWrite(pin_LEDred, LOW);
      // Turn on the next LED
      // Because the value of state does not change while we are testing it we don't need to use else if
      if (state == 1) {
        digitalWrite(pin_LEDgreen, HIGH);
      }
      if (state == 2) {
        digitalWrite(pin_LEDyellow, HIGH);
      }
      if (state == 3) {
        digitalWrite(pin_LEDred, HIGH);
      }
    }
    oldSwitchState = newSwitchState;
  }
}

Code Explanation

Since we now have 3 LEDs we need to define the 3 pins being used. You can use any color you would like.

int pin_LEDgreen = 10;
int pin_LEDyellow = 9;
int pin_LEDred = 8;

In the setup() part, we need to make sure that the LEDs are off and are being initialized as output.

pinMode(pin_LEDgreen, OUTPUT);    digitalWrite(pin_LEDgreen, LOW);
  pinMode(pin_LEDyellow, OUTPUT);   digitalWrite(pin_LEDyellow, LOW);
  pinMode(pin_LEDred, OUTPUT);      digitalWrite(pin_LEDred, LOW);
  pinMode(pin_switch, INPUT);

To keep track of where we are, a new variable called state is used. the state is a byte that can have 1 of 4 values (0 to 3):

– state = 0 – all LEDs off
– state = 1 – green LED on
– state = 2 – yellow LED on
– state = 3 – red LED on

Every time the button switch has pressed the value of the state is increased by 1. When the state is greater than 3 then it is reset to 0 and the sketch starts all over again.

state++;
      if (state > 3) {
        state = 0;
      }

I have kept the logic as simple as possible and the LEDs are controlled with a series of if statements.  You could also use a Using Switch Statements

The a upcoming tutorial you will control the blinking of a LED by using a potentiometer and read the analog values of it. You see for yourself: Arduino Tutorial: 4.7 Reading analog values

Leave a Reply

Your email address will not be published. Required fields are marked *

Fill out this field
Fill out this field
Please enter a valid email address.
You need to agree with the terms to proceed

Menu