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:

State_change.ino:

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.

  • balamsoto

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

    thanks

  • 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.

  • Johnny

    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?

  • Johnny

    just saw the above…sorry

  • Emil

    What does the button state change code do ?

  • victoria secret

    Hi, that is impossible .

  • See the latest code block…
    stateNum++;
    if(stateNum>2) stateNum=0;

  • Roel Veldhuyzen

    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?

  • 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…

  • Roel

    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 :)

  • But did you use a resistor when you connected a button? With pullup you use the internal resistor of the UNO (which is fine).

  • Roel

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

  • Vilnis Andersons

    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.

  • 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.

  • Vilnis Andersons

    Thanks for reply. Will do :)