Arduino Programming – State change

How to program a state change functionality. ‘State change detection’ is a method to see when a button is pressed or released. You can use it to fire a different action each press of a button.
In this tutorial the basic code structure ( setup, loop ) of Arduino script is also explained.

I explain you how to program a state change functionality. ‘State change detection’ is a method to see when a button is pressed or released. You can use it to fire a different action each press of a button.

For example the “Play/Pause” button on a cd-player behaves like that. The first press is play, the next press is pause, the next play again etc. Depending the state the action is different ( play > pause, pause > play ).

In this tutorial I explain also the basic code structure ( setup, loop ) of Arduino script.

Level: beginner with Arduino. ( Basic knowledge of programming principles like if/else and variables ).

OnOff.ino:

/*
  LED on when you press a button.
  LED off when you release a button.  
  
  created 01-12-2009 by kasperkamperman.com
*/

const int buttonPin = 2;      // the pin that the pushbutton is attached to
const int ledPin    = 13;     // the pin that the LED is attached to

int buttonState     = 0;      // current state of the button

void setup() {
  pinMode(buttonPin, INPUT);  // initialize the button pin as a input
  pinMode(ledPin, OUTPUT);    // initialize the button pin as a output
}

void loop() {
  // read the pushbutton input pin
  buttonState = digitalRead(buttonPin);

  // turns LED on if the buttonState=HIGH or off if the buttonState=LOW
  digitalWrite(ledPin, buttonState);
}

State_change.ino:

/*
  Set a state of a variable when you press a pushbutton ( the 
  button went from off to on ). 
  
  created 01-12-2009 by kasperkamperman.com
  based on example 'State change detection' by Tom Igoe
*/

const int buttonPin  = 2;     // the pin that the pushbutton is attached to
const int ledPin     = 13;    // the pin that the LED is attached to

int buttonState      = 0;     // current state of the button
int lastButtonState  = 0;     // previous state of the button
int ledState         = 0;     // remember current led state

void setup() {
  pinMode(buttonPin, INPUT);  // initialize the button pin as a input
  pinMode(ledPin, OUTPUT);    // initialize the button pin as a output
}

void loop() {
  // read the pushbutton input pin
  buttonState = digitalRead(buttonPin);

  // check if the button is pressed or released
  // by comparing the buttonState to its previous state 
  if (buttonState != lastButtonState) {
    
    // change the state of the led when someone pressed the button
    if (buttonState == 1) { 
      if(ledState==1) ledState=0;
      else            ledState=1;         
    }
    
    // remember the current state of the button
    lastButtonState = buttonState;
  }
  
  // turns LED on if the ledState=1 or off if the ledState=0
  digitalWrite(ledPin, ledState);
  
  // adding a small delay prevents reading the buttonState to fast
  // ( debouncing )
  delay(20);
}

If you like to work with more than two states, do something like below. And use a switch or if statement later to perform some action on a certain stateNum value.

if (buttonState == 1) { 
      stateNum++; 
      if(stateNum>2) stateNum=0;      
}
Buy Me a Coffee at ko-fi.comWas this article useful to you? Buy me a coffee!

17 thoughts on “Arduino Programming – State change”

  1. i
    HI
    how would you add more states ( lets say 3 states ) in the code

    thanks

  2. Em, I explained that: If you like to work with more than two states, do something like below. And use a switch or if statement later to perform some action on a certain stateNum value.

  3. Just what I was looking for! I’m interested in toggling between 2 different LEDs based on the button state. Can you help with example code for that?

  4. If I implement this code on my UNO, there seems to be an 8 second window for interaction.
    If I run the code and do nothing, the LED turns on for 8 seconds, then off for 8 seconds, and so on.
    During the first 8 seconds it doesn’t respond to button pushes, in the second 8 seconds it does respond and I can turn the LED on and off as much as I like, then I get another 8 seconds where I can’t do anything. The next 8 seconds it will respond again.
    Any idea why this is the case and how I can fix that?

  5. No idea. Did you put a delay somewhere? In my example there is a small delay of 20 milliseconds to prevent bouncing. Are you running other code? Try to go back to the barebone example of me to test it. Otherwise it might be a hardware, cabling problem…

  6. That’s the first thing I did, went to a new file and only worked with your code, kept having the weird 8 second increment thing. But I somehow seem to have fixed it.

    I changed “pinMode(buttonPin, INPUT);” to “pinMode(buttonPin, INPUT_PULLUP);”.
    Now it works like a charm. I have no idea what caused the weird 8 second thing, or why this change fixed it, to be honest. But it works!
    Thanks :)

  7. Well, there is a step I forgot. Would that cause the weird 8 second pattern I got?

  8. How would you write a code to execute a function when button is pressed and stop it immediately if button is pressed again? My function is a very basic sequence of 4 leds turning on and off.

    void blinker(){

    digitalWrite(ledA, HIGH);
    delay(delayOn);
    digitalWrite(ledB, HIGH);
    delay(delayOn);
    digitalWrite(ledC, HIGH);
    delay(delayOn);
    digitalWrite(ledD, HIGH);
    delay(delayOn);
    digitalWrite(ledA, LOW);
    delay(delayOff);
    digitalWrite(ledB, LOW);
    delay(delayOff);
    digitalWrite(ledC, LOW);
    delay(delayOff);
    digitalWrite(ledD, LOW);
    delay(delayOff);
    }

    I could get it started with your code – stopping is the problem.

  9. First program the blinking without using the delay function. Search for blink without delay. Then use a variable. Start reading a geting started book of Arduino first, so you get an understanding how things work.

  10. hey if I wanted to change this code so it sent a message over the serial port only when the button was pushed down or released

    how would I go about it ?

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.