# Puss button does not respond if a delay is used

**URL:** <https://forum.arduino.cc/t/puss-button-does-not-respond-if-a-delay-is-used/899819>\
**Category:** Programming\
**Created:** [August 27, 2021, 2:19pm UTC](https://forum.arduino.cc/t/puss-button-does-not-respond-if-a-delay-is-used/899819 "2021-08-27T14:19:28Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![cevepe](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/cevepe/32/1013044_2.png) [@cevepe](https://forum.arduino.cc/u/cevepe)\
**Post date:** [August 27, 2021, 2:19pm UTC](https://forum.arduino.cc/t/puss-button-does-not-respond-if-a-delay-is-used/899819/1 "2021-08-27T14:19:28Z")

</div>

In loop (), I read the data from the sensors every minute and print it out on the LCD. I have a delay between measuring different sensors so that the text on the display stays visible longer. The problem is that when using delay and therefore minute data, pressing the HIGH button does not work, because in loop () it is at the beginning and responds best outside the minute data when the sensors do not measure.

Can't the reaction of pressing the button be solved outside the loop, so that the pressing works without what happens in the loop? To work right away without any circumstances?

---

<div class="post-metadata">

**Author:** ![blh64](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/blh64/32/605986_2.png) [@blh64](https://forum.arduino.cc/u/blh64)\
**Post date:** [August 27, 2021, 2:22pm UTC](https://forum.arduino.cc/t/puss-button-does-not-respond-if-a-delay-is-used/899819/2 "2021-08-27T14:22:02Z")

</div>

The solution is to remove delay() and instead track elapsed time using millis() so the rest of loop() can keep doing useful things. It is a common pitfall. There is even an example in the IDE - BlinkWithoutDelay (File-\>examples-\>02.digital-\>BlinkWithoutDelay)

There is also a nice tutorial about how to break your code up to do small chunks at a time: [several things at the same time](https://forum.arduino.cc/index.php?topic=223286.0)

---

<div class="post-metadata">

**Author:** ![alto777](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/alto777/32/317385_2.png) [@alto777](https://forum.arduino.cc/u/alto777)\
**Post date:** [August 27, 2021, 2:33pm UTC](https://forum.arduino.cc/t/puss-button-does-not-respond-if-a-delay-is-used/899819/3 "2021-08-27T14:33:30Z")

</div>

Agree, it is a simple enough matter.

Please post your code, or a smaller similar example showing how your current concept is interfering with the desired behaviour.

Because there's as many ways to do it wrong as there are ways to do it differently, so give us a lot at what you've managed to do sofar.

a7

---

<div class="post-metadata">

**Author:** ![groundFungus](https://avatars.discourse-cdn.com/v4/letter/g/73ab20/32.png) [@groundFungus](https://forum.arduino.cc/u/groundFungus)\
**Post date:** [August 27, 2021, 2:35pm UTC](https://forum.arduino.cc/t/puss-button-does-not-respond-if-a-delay-is-used/899819/4 "2021-08-27T14:35:13Z")

</div>

Using millis() for timing tutorials:  
[Several things at a time.](https://forum.arduino.cc/index.php?topic=223286.0)  
[Beginner's guide to millis().](https://forum.arduino.cc/index.php?topic=503368.0)  
[Blink without delay().](https://www.arduino.cc/en/tutorial/BlinkWithoutDelay)

---

<div class="post-metadata">

**Author:** ![cevepe](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/cevepe/32/1013044_2.png) [@cevepe](https://forum.arduino.cc/u/cevepe)\
**Post date:** [August 27, 2021, 2:41pm UTC](https://forum.arduino.cc/t/puss-button-does-not-respond-if-a-delay-is-used/899819/5 "2021-08-27T14:41:57Z")

</div>

I use Delay mainly for LCD (1602) to display text long enough. But then the push button doesn't work ☹

```cpp
void loop() {
Reload = digitalRead(RELOAD_DATA_PIN);
if (Reload == HIGH)
{
    // something will be done 
} 

// every 1 minutes
if (millis() - timeMinute >= 60000 || timeMinute == 0) {
timeMinute = millis();

Pressure = bme280.getPressure(); 
lcd.clear();
lcd.print("BME280 pressure:");
lcd.setCursor(0, 1);
lcd.print(Pressure);
delay(3000);

Temp = sht.getTemperature();
lcd.clear();
lcd.print("SHT3x temp:");
lcd.setCursor(0, 1);
lcd.print(Temp);
delay(3000);
}

} // loop

```

---

<div class="post-metadata">

**Author:** ![groundFungus](https://avatars.discourse-cdn.com/v4/letter/g/73ab20/32.png) [@groundFungus](https://forum.arduino.cc/u/groundFungus)\
**Post date:** [August 27, 2021, 3:22pm UTC](https://forum.arduino.cc/t/puss-button-does-not-respond-if-a-delay-is-used/899819/6 "2021-08-27T15:22:04Z")

</div>

Look in the several things at a time tutorial to see how different things can be done at their own times. Those delay() calls mess up the millis() timing. Get rid of delays completely.

When you post code, post the whole code.

---

<div class="post-metadata">

**Author:** ![ruilviana](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/ruilviana/32/722429_2.png) [@ruilviana](https://forum.arduino.cc/u/ruilviana)\
**Post date:** [August 28, 2021, 12:53pm UTC](https://forum.arduino.cc/t/puss-button-does-not-respond-if-a-delay-is-used/899819/7 "2021-08-28T12:53:14Z")

</div>

Hi @cevepe  
You can use external interrupt to detect button press.  
Interrupt remain available even during delays().

RV mineirin

---

<div class="post-metadata">

**Author:** ![sterretje](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/sterretje/32/391294_2.png) [@sterretje](https://forum.arduino.cc/u/sterretje)\
**Post date:** [August 28, 2021, 1:25pm UTC](https://forum.arduino.cc/t/puss-button-does-not-respond-if-a-delay-is-used/899819/8 "2021-08-28T13:25:56Z")

</div>

@ruilviana, if that will work highly depends on what needs to happen when the button is pressed.

---

<div class="post-metadata">

**Author:** ![GolamMostafa](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/golammostafa/32/239011_2.png) [@GolamMostafa](https://forum.arduino.cc/u/GolamMostafa)\
**Post date:** [August 28, 2021, 5:45pm UTC](https://forum.arduino.cc/t/puss-button-does-not-respond-if-a-delay-is-used/899819/9 "2021-08-28T17:45:06Z")

</div>

> [@cevepe](#):
>
> between measuring different sensors so that the text on the display stays visible longer.

LCD has its own scanning circuitry and display controller. The display will be showing the users data all the time without any interruption. So, there is no need to insert any time delay at all.

---

<div class="post-metadata">

**Author:** ![noiasca](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/noiasca/32/1126910_2.png) [@noiasca](https://forum.arduino.cc/u/noiasca)\
**Post date:** [August 28, 2021, 6:04pm UTC](https://forum.arduino.cc/t/puss-button-does-not-respond-if-a-delay-is-used/899819/10 "2021-08-28T18:04:29Z")

</div>

> [@cevepe](#):
>
> I use Delay mainly for LCD

you don't need delay.

just switch the contet of the display .

This switches between to LCD contents:

```arduino
void updateLCD()
{
  const uint16_t interval = 60000; // update Intervall in milliseconds
  static uint32_t previousMillis = 0; // timestamp last update
  static byte state = 0; // which "screen" is to display
  uint32_t currentMillis = millis();   

  if (currentMillis - previousMillis >= interval)
  {
    previousMillis = currentMillis;
    switch (state)
    {
      case 0: // show screen 0
        Pressure = bme280.getPressure();
        lcd.clear();
        lcd.print("BME280 pressure:");
        lcd.setCursor(0, 1);
        lcd.print(Pressure);
        state = 1; // show screen 1 next time
        break;
      case 1: // show screen 1
        Temp = sht.getTemperature();
        lcd.clear();
        lcd.print("SHT3x temp:");
        lcd.setCursor(0, 1);
        lcd.print(Temp);
        state = 0;
        break;
    }
  }
}
```

and call

```arduino
updateLCD();
```

in your loop().

---

<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:** [December 26, 2021, 6:04pm UTC](https://forum.arduino.cc/t/puss-button-does-not-respond-if-a-delay-is-used/899819/11 "2021-12-26T18:04:44Z")

</div>

Closed on December 26, 2021, 6:04pm UTC

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