# Stop Blocking on Button Press

**URL:** <https://forum.arduino.cc/t/stop-blocking-on-button-press/1099294>\
**Category:** Programming\
**Created:** [March 8, 2023, 10:41am UTC](https://forum.arduino.cc/t/stop-blocking-on-button-press/1099294 "2023-03-08T10:41:27Z")\
**Posts on this page:** 15\
**Page:** 1

<div class="post-metadata">

**Author:** ![mrshiney](https://avatars.discourse-cdn.com/v4/letter/m/cc9497/32.png) [@mrshiney](https://forum.arduino.cc/u/mrshiney)\
**Post date:** [March 8, 2023, 10:41am UTC](https://forum.arduino.cc/t/stop-blocking-on-button-press/1099294/1 "2023-03-08T10:41:27Z")

</div>

Hi  
Im very new to all this and green.  
At the moment the delay is blocking the second button press.  
I would like to be able to press both buttons at any time instead of wait.  
I looked up **millis()** instead for delay but don't understand how to write it.  
could someone help me write the code below to have the same moment and wait without blocking.  
many thanks  
Jason

```cpp
#include <ezButton.h>
#include <Servo.h> 
ezButton buttonleft(7);
ezButton buttonright(8); // create ezButton object that attach to pin 7;
Servo servoleft;  
Servo servoright;  
int dt=50; 
int pos = 50;  
void setup() {
    servoleft.attach(9);
    servoright.attach(10);
  Serial.begin(9600);
  servoleft.write(87);
  servoright.write(90);

}

void loop() {
  buttonleft.loop(); 
   buttonright.loop();// MUST call the loop() function first

 delay(dt);

if(buttonright.isPressed()){
   Serial.println("The button left is pressed");
     // Delay just means WAIT! Remember every 
                  // 100 = 0.1 second!
 delay(100); 
servoleft.write(130); // Move servo to 180 degrees!
delay(1000); // Wait 1 second
 for (pos = 130; pos >= 87; pos -= 1) {/* goes from 0 to 70
                                          degrees in steps
                                          of 1 degree */
    
   servoleft.write(pos); // go to position 'pos'
    
    delay(5); // waits 5ms
                                       

 }

}
if(buttonleft.isPressed()){
   Serial.println("The button right is pressed ");
     // Delay just means WAIT! Remember every 
                  // 100 = 0.1 second!
 delay(100); 
servoright.write(40); // Move servo to 180 degrees!
delay(500); // Wait 1 second
for (pos = 40; pos <= 90; pos += 1) { /* goes from 0 to 70
                                          degrees in steps
                                          of 1 degree */
    
   servoright.write(pos); // go to position 'pos'
    
    delay(5); /* waits 5ms 

}
}
}

```

---

<div class="post-metadata">

**Author:** ![UKHeliBob](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/ukhelibob/32/215663_2.png) [@UKHeliBob](https://forum.arduino.cc/u/UKHeliBob)\
**Post date:** [March 8, 2023, 11:49am UTC](https://forum.arduino.cc/t/stop-blocking-on-button-press/1099294/2 "2023-03-08T11:49:45Z")

</div>

Perhaps start by correcting the mistakes in the current sketch

```cpp
  if (buttonright.isPressed())
  {
    Serial.println("The button left is pressed");

```

```cpp
  if (buttonleft.isPressed())
  {
    Serial.println("The button right is pressed ");

```

---

<div class="post-metadata">

**Author:** ![mrshiney](https://avatars.discourse-cdn.com/v4/letter/m/cc9497/32.png) [@mrshiney](https://forum.arduino.cc/u/mrshiney)\
**Post date:** [March 8, 2023, 1:01pm UTC](https://forum.arduino.cc/t/stop-blocking-on-button-press/1099294/3 "2023-03-08T13:01:45Z")

</div>

😂  
So helpful  
Thanks  
That was just for testing to seeing if each button was working did not matter if they matched up.  
But I did say I was new lol

---

<div class="post-metadata">

**Author:** ![FernandoGarcia](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/fernandogarcia/32/214937_2.png) [@FernandoGarcia](https://forum.arduino.cc/u/FernandoGarcia)\
**Post date:** [March 8, 2023, 1:26pm UTC](https://forum.arduino.cc/t/stop-blocking-on-button-press/1099294/4 "2023-03-08T13:26:08Z")

</div>

Try understand this example and apply to your project.

```cpp
#include <Arduino.h>
#include <Bounce2.h> // https://github.com/thomasfredericks/Bounce2/archive/refs/heads/master.zip
#include <Servo.h>

Bounce2::Button button = Bounce2::Button();

const int buttonPin = 4;
const int servo1Pin = 9;
const int servo2Pin = 10;
const int ledPin = LED_BUILTIN;

byte step = 1;
const int courseTime = 10;
const int time = (courseTime * 300) / 180;

Servo myservo1;
Servo myservo2;

void setup()
{
  Serial.begin(9600);

  button.attach(buttonPin, INPUT_PULLUP);
  button.interval(5); // interval im ms
  button.setPressedState(LOW);

  myservo1.write(0); //starts motor at 0 degress
  myservo2.write(0);
  myservo1.attach(servo1Pin);
  myservo2.attach(servo2Pin);
}

void loop()
{
  button.update();

  if (button.pressed())
  {
    if (step == 0)
    {
      step = 1;
      moveServo();
    }
    else
    {
      step = 0;
      moveServo();
    }
  }
}

void moveServo()
{
  Serial.print("step = ");
  Serial.println(step);

  switch (step)
  {
    case 0:
      for (int pos = 0; pos < 120; pos++)
      {
        myservo1.write(pos);
        myservo2.write(pos);
      }
      break;
    case 1:
      for (int pos = 120; pos > 0; pos--)
      {
        myservo1.write(pos);
        myservo2.write(pos);
      }
      break;
    default:
      break;
  }
}

```

> **[Button and servo - Wokwi Arduino and ESP32 Simulator](https://wokwi.com/projects/357742242318009345)**
>
> Run IoT and embedded projects in your browser: ESP32, Arduino, Pi Pico, and more. No installation required!

---

<div class="post-metadata">

**Author:** ![mrshiney](https://avatars.discourse-cdn.com/v4/letter/m/cc9497/32.png) [@mrshiney](https://forum.arduino.cc/u/mrshiney)\
**Post date:** [March 8, 2023, 1:27pm UTC](https://forum.arduino.cc/t/stop-blocking-on-button-press/1099294/5 "2023-03-08T13:27:38Z")

</div>

Thanks for your help  
I will try to get my head round it

---

<div class="post-metadata">

**Author:** ![UKHeliBob](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/ukhelibob/32/215663_2.png) [@UKHeliBob](https://forum.arduino.cc/u/UKHeliBob)\
**Post date:** [March 8, 2023, 1:28pm UTC](https://forum.arduino.cc/t/stop-blocking-on-button-press/1099294/6 "2023-03-08T13:28:35Z")

</div>

That's the easy stuff to change but changing to non blocking timing using millis() is going to need bigger changes. All of the delay()s need to go, even assuming that they were needed in the first place

I assume that the delay()sin the servo movement for loops are to slow down the servo movement but what about the others ?

I feel that I should point out that your code as posted does not compile

---

<div class="post-metadata">

**Author:** ![mrshiney](https://avatars.discourse-cdn.com/v4/letter/m/cc9497/32.png) [@mrshiney](https://forum.arduino.cc/u/mrshiney)\
**Post date:** [March 8, 2023, 1:35pm UTC](https://forum.arduino.cc/t/stop-blocking-on-button-press/1099294/7 "2023-03-08T13:35:56Z")

</div>

Thanks  
I copied and pasted bits so probably missed something.  
I’m a magician and have been using it to make a sign fall in my show (left side then the right). It works but would like it to drop both together as a finale.  
I think I could make it works with number of buttons presses which might be easier and then would only need 1 button I think?

Thanks again for your help

---

<div class="post-metadata">

**Author:** ![FernandoGarcia](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/fernandogarcia/32/214937_2.png) [@FernandoGarcia](https://forum.arduino.cc/u/FernandoGarcia)\
**Post date:** [March 8, 2023, 1:38pm UTC](https://forum.arduino.cc/t/stop-blocking-on-button-press/1099294/8 "2023-03-08T13:38:54Z")

</div>

> [@mrshiney](#):
>
> I think I could make it works with number of buttons presses which might be easier and then would only need 1 button I think?

It's exactly what the code above does.

---

<div class="post-metadata">

**Author:** ![UKHeliBob](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/ukhelibob/32/215663_2.png) [@UKHeliBob](https://forum.arduino.cc/u/UKHeliBob)\
**Post date:** [March 8, 2023, 1:41pm UTC](https://forum.arduino.cc/t/stop-blocking-on-button-press/1099294/9 "2023-03-08T13:41:32Z")

</div>

You could certainly make it work using a single button

- first press, one side drops
- second press, other side drops (does the first side need to raise ?)
- third press, both sides drop

You could then continue to use delay()s in the code, which is going to be easier than changing to millis() timing

---

<div class="post-metadata">

**Author:** ![mrshiney](https://avatars.discourse-cdn.com/v4/letter/m/cc9497/32.png) [@mrshiney](https://forum.arduino.cc/u/mrshiney)\
**Post date:** [March 8, 2023, 3:30pm UTC](https://forum.arduino.cc/t/stop-blocking-on-button-press/1099294/10 "2023-03-08T15:30:08Z")

</div>

It’s first side drops  
I put it back  
Then first side drops again  
This time I put it back and hold it.  
Then second side drops  
I put it back walk away  
Then they both drop.

The buttons are link to a remote with 2 channels

But at the moment I can’t get them both to drop at the same time. Because of the delay

So will try to figure out the button number count and do it that way.

---

<div class="post-metadata">

**Author:** ![UKHeliBob](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/ukhelibob/32/215663_2.png) [@UKHeliBob](https://forum.arduino.cc/u/UKHeliBob)\
**Post date:** [March 8, 2023, 3:59pm UTC](https://forum.arduino.cc/t/stop-blocking-on-button-press/1099294/11 "2023-03-08T15:59:16Z")

</div>

> [@mrshiney](#):
>
> the button number count

Take a look at the StateChangeDetection example in the IDE

---

<div class="post-metadata">

**Author:** ![FernandoGarcia](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/fernandogarcia/32/214937_2.png) [@FernandoGarcia](https://forum.arduino.cc/u/FernandoGarcia)\
**Post date:** [March 8, 2023, 5:28pm UTC](https://forum.arduino.cc/t/stop-blocking-on-button-press/1099294/12 "2023-03-08T17:28:41Z")

</div>

Look for this:

```cpp
#include <Arduino.h>
#include <Servo.h>
#include <IRremote.h>

const int receiverPin = 2;
const int servoLeftPin = 9;
const int servoRightPin = 10;

byte step = 0;
bool nextStep = false;
bool commandReceived = false;

unsigned long prevMillis = 0;

#define DROP_INTERVAL 2 // Time in seconds

Servo leftSide;
Servo rightSide;

IRrecv receiver(receiverPin);

void translateIR();
void moveServo(byte move);

void setup()
{
  Serial.begin(9600);

  leftSide.write(90);
  rightSide.write(90);
  leftSide.attach(servoLeftPin);
  rightSide.attach(servoRightPin);

  receiver.enableIRIn();
}

void loop()
{
  // Checks received an IR signal
  if (receiver.decode())
  {
    translateIR();
    receiver.resume(); // Receive the next value
  }

  if (step == 4)
  {
    if ((millis() - prevMillis) >= (DROP_INTERVAL * 1000UL))
    {
      Serial.print("step = ");
      Serial.println(step);
      moveServo(4);
      step = 0;
    }
  }

  switch (step)
  {
    case 0:
      if (commandReceived == true)
      {
        if (nextStep == false)
        {
          nextStep = true;
          moveServo(0);
          prevMillis = millis();
        }
        else
        {
          if ((millis() - prevMillis) >= (DROP_INTERVAL * 1000UL))
          {
            nextStep = false;
            commandReceived = false;
            moveServo(1);
          }
        }
      }
      break;
    case 1:
      if (commandReceived == true)
      {
        if (nextStep == false)
        {
          nextStep = true;
          moveServo(2);
          prevMillis = millis();
        }
        else
        {
          if ((millis() - prevMillis) >= (DROP_INTERVAL * 1000UL))
          {
            nextStep = false;
            commandReceived = false;
            step = 4;
            moveServo(3);
            prevMillis = millis();
          }
        }
      }
      break;
    default:
      break;
  }
}

void translateIR()
{
  // Takes command based on IR code received
  switch (receiver.decodedIRData.command)
  {
    case 48:
      Serial.println("Button pressed: 1");
      step = 0;
      commandReceived = true;
      break;
    case 24:
      Serial.println("Button pressed: 2");
      commandReceived = true;
      step = 1;
      break;
    default:
      Serial.print("Other button: ");
      Serial.println(receiver.decodedIRData.command);
  }
}

void moveServo(byte move)
{
  Serial.print("move = ");
  Serial.println(move);

  switch (move)
  {
    case 0:
      leftSide.write(180);
      break;
    case 1:
      leftSide.write(90);
      break;
    case 2:
      rightSide.write(0);
      break;
    case 3:
      rightSide.write(90);
      break;
    case 4:
      leftSide.write(180);
      rightSide.write(0);
      break;
    default:
      break;
  }
}

```

I don't know how do you pretend to simulate put the side back in place.

Maybe you should dettach the servo at `move = 0`, then move the servo manually to right position, after attach the servo again to keep it in place.

Remember to always write the position before attach a servo.

Play here:

Button 1 will drop the left side and 2 the right side. Both sides will drop after `DROP_INTERVAL` when `step = 4`.

> **[Servo remote control - Wokwi Arduino and ESP32 Simulator](https://wokwi.com/projects/358646366040153089)**
>
> Run IoT and embedded projects in your browser: ESP32, Arduino, Pi Pico, and more. No installation required!

---

<div class="post-metadata">

**Author:** ![mrshiney](https://avatars.discourse-cdn.com/v4/letter/m/cc9497/32.png) [@mrshiney](https://forum.arduino.cc/u/mrshiney)\
**Post date:** [March 8, 2023, 5:54pm UTC](https://forum.arduino.cc/t/stop-blocking-on-button-press/1099294/13 "2023-03-08T17:54:58Z")

</div>

Got It all working with 1 button with EZButton thanks for the help.  
Ive keeped the other button for now incase I want something else later.

Im trying to rest the count to 0 after the 4th press.  
I tried countleft = 0; at the end  
but does not work.  
anyone know an easy way to reset it.  
thanks

```cpp
#include <ezButton.h>
#include <Servo.h> 
ezButton buttonleft(7);
ezButton buttonright(8); // create ezButton object that attach to pin 7;
Servo servoleft;  
Servo servoright; 
// Tracks the button state. 
int dt=50; 
int pos = 50;

void setup() {
    servoleft.attach(9);
    servoright.attach(10);
  Serial.begin(9600);
  servoleft.write(87);
  servoright.write(77);
  buttonleft.setCountMode(COUNT_FALLING);
  buttonright.setCountMode(COUNT_FALLING);

}

void loop() {
  buttonleft.loop(); 
   buttonright.loop();// MUST call the loop() function first
   unsigned long countleft = buttonleft.getCount();
   unsigned long countright = buttonright.getCount();
   

 delay(dt);
if(buttonleft.isPressed() && countleft == 1){
   Serial.println("The button right is pressed");
     // Delay just means WAIT! Remember every 
                  // 100 = 0.1 second!

servoright.write(40); // Move servo to 180 degrees!
delay(500); // Wait 1 second
for (pos = 40; pos <= 77; pos += 1) { /* goes from 0 to 70
                                          degrees in steps
                                          of 1 degree */
                                       
servoright.write(pos); 
    delay(5);  
 }
 

}
if(buttonleft.isPressed() && countleft == 2){
   Serial.println("The button right is pressed");
     // Delay just means WAIT! Remember every 
                  // 100 = 0.1 second!

servoright.write(40); // Move servo to 180 degrees!
delay(500); // Wait 1 second
for (pos = 40; pos <= 77; pos += 1) { /* goes from 0 to 70
                                          degrees in steps
                                          of 1 degree */
                                       
servoright.write(pos); 
    delay(5);  
 }
 

}

if(buttonleft.isPressed() && countleft == 3){
   Serial.println("The button right is pressed");
     // Delay just means WAIT! Remember every 
                  // 100 = 0.1 second!

servoleft.write(130); // Move servo to 180 degrees!
delay(500); // Wait 1 second
 for (pos = 130; pos >= 87; pos -= 1) {/* goes from 0 to 70
                                          degrees in steps
                                          of 1 degree */
    
                 // go to position 'pos'
    
servoleft.write(pos); 
    delay(5);  
                                       

 }
 

}
if(buttonleft.isPressed() && countleft == 4){
   Serial.println("The button right is pressed");
     // Delay just means WAIT! Remember every 
                  // 100 = 0.1 second!
servoright.write(40); 
servoleft.write(130); // Move servo to 180 degrees!
delay(500); // Wait 1 second

servoright.write(77); 
servoleft.write(87);                                    
countleft = 0;
 }
 

}

```

---

<div class="post-metadata">

**Author:** ![mrshiney](https://avatars.discourse-cdn.com/v4/letter/m/cc9497/32.png) [@mrshiney](https://forum.arduino.cc/u/mrshiney)\
**Post date:** [March 8, 2023, 6:03pm UTC](https://forum.arduino.cc/t/stop-blocking-on-button-press/1099294/14 "2023-03-08T18:03:57Z")

</div>

Found it  
buttonleft.resetCount();

thanks again for all the help

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [September 4, 2023, 6:04pm UTC](https://forum.arduino.cc/t/stop-blocking-on-button-press/1099294/15 "2023-09-04T18:04:50Z")

</div>

Closed on September 4, 2023, 6:04pm UTC

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.
