# Button implementation within a long delay loop

**URL:** <https://forum.arduino.cc/t/button-implementation-within-a-long-delay-loop/1412116>\
**Category:** Programming\
**Created:** [November 3, 2025, 10:47pm UTC](https://forum.arduino.cc/t/button-implementation-within-a-long-delay-loop/1412116 "2025-11-03T22:47:22Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![mcu\_noob](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/mcu_noob/32/1131985_2.png) [@mcu\_noob](https://forum.arduino.cc/u/mcu_noob)\
**Post date:** [November 3, 2025, 10:47pm UTC](https://forum.arduino.cc/t/button-implementation-within-a-long-delay-loop/1412116/1 "2025-11-03T22:47:22Z")

</div>

Hello, I am building an air quality sensor using an arduino uno r3, an scd40 co2 sensor and an i2c oled screen.  
I am using the default code from the sensor manufacturer which polls the sensor every 5 seconds to save power.

```arduino

void loop() { 

	// Slow down the sampling to 0.2Hz.
    	//
    	delay(5000);
	.
	.
	sensor.readMeasurement(co2Concentration)
	.
	.
	display.print(co2Concentration);
	.
	.
}

```

So far so good, everything works fine. Now I would like to implement a button that upon pressing changes the brightness.  
After reading about button state changes and debouncing, I understand that the 5 seconds delay in the main loop might be problematic.  
How could I implement the above given that both in and out button state changes could very well take place within the 5 seconds of the main loop delay and be missed?

---

<div class="post-metadata">

**Author:** ![LarryD](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/larryd/32/215550_2.png) [@LarryD](https://forum.arduino.cc/u/LarryD)\
**Post date:** [November 3, 2025, 11:01pm UTC](https://forum.arduino.cc/t/button-implementation-within-a-long-delay-loop/1412116/2 "2025-11-03T23:01:16Z")

</div>

> [@mcu\_noob](#):
>
> I understand that the 5 seconds delay in the main loop might be problematic.

- delay(. . .) stops normal code execution for that delay time.
- Look at the examples in the IDE for making non blocking delay TIMERs.

[https://docs.arduino.cc/built-in-examples/digital/BlinkWithoutDelay](https://docs.arduino.cc/built-in-examples/digital/BlinkWithoutDelay)

---

<div class="post-metadata">

**Author:** ![gcjr](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/gcjr/32/314015_2.png) [@gcjr](https://forum.arduino.cc/u/gcjr)\
**Post date:** [November 3, 2025, 11:02pm UTC](https://forum.arduino.cc/t/button-implementation-within-a-long-delay-loop/1412116/3 "2025-11-03T23:02:17Z")

</div>

use a timer to invoke that code every 5 seconds instead of usign delay().

---

<div class="post-metadata">

**Author:** ![sonofcy](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/sonofcy/32/1283617_2.png) [@sonofcy](https://forum.arduino.cc/u/sonofcy)\
**Post date:** [November 3, 2025, 11:05pm UTC](https://forum.arduino.cc/t/button-implementation-within-a-long-delay-loop/1412116/4 "2025-11-03T23:05:15Z")

</div>

Use a button library to handle the button and see post 2 and 3.

---

<div class="post-metadata">

**Author:** ![xfpd](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/xfpd/32/1120236_2.png) [@xfpd](https://forum.arduino.cc/u/xfpd)\
**Post date:** [November 4, 2025, 12:16am UTC](https://forum.arduino.cc/t/button-implementation-within-a-long-delay-loop/1412116/5 "2025-11-04T00:16:55Z")

</div>

After you complete the timed event, you might want a clean way to update the new data. Clearing the display (`display.clearDisplay()`) requires updating all the information (even static data, like an outline, heading or image) on the display, which may cause "flickering."

What I like to use is... before the next sensor reading, position the cursor, change the text color to black, and write the old data.

```arduino
    // erase old data
    display.setCursor(20, 10); // position cursor
    display.setTextColor(BLACK);
    display.print(co2Concentration); // left over from previous reading
    display.display(); // display the buffer

```

Then take a new sensor reading...

```arduino
    // read new data
    sensor.readMeasurement(co2Concentration)

```

Then position the cursor, change text color to white, and print it.

```arduino
    // display new data
    display.setCursor(20, 10); // position cursor
    display.setTextColor(WHITE);
    display.print(co2Concentration);
    display.display();

```

---

<div class="post-metadata">

**Author:** ![JCA34F](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/jca34f/32/340148_2.png) [@JCA34F](https://forum.arduino.cc/u/JCA34F)\
**Post date:** [November 4, 2025, 2:39am UTC](https://forum.arduino.cc/t/button-implementation-within-a-long-delay-loop/1412116/6 "2025-11-04T02:39:11Z")

</div>

This millis timer works for me.

```arduino
void loop()
{ // start loop
  static unsigned long timer = millis();
  const unsigned long interval = 5000;
  
  if(millis() - timer > interval) // timer expired
  { // start timed display
    timer += interval; // restart timer
    sensor.readMeasurement(co2Concentration);
    display.print(co2Concentration);
  } // end timed display
  
  // button and brightnes code here
  //
  //
  //
  //
  //
  //
   
} // end loop

```

---

<div class="post-metadata">

**Author:** ![xfpd](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/xfpd/32/1120236_2.png) [@xfpd](https://forum.arduino.cc/u/xfpd)\
**Post date:** [November 4, 2025, 6:00am UTC](https://forum.arduino.cc/t/button-implementation-within-a-long-delay-loop/1412116/7 "2025-11-04T06:00:05Z")

</div>

> [@JCA34F](#):
>
> ```arduino
> if(millis() - timer > interval) // timer expired
> { // start timed display
> timer += interval; // restart timer 
> 
> ```

I have seen that way, with adding the interval to timer. I use a similar method... without addition...

```arduino
unsigned long timer, samplerate = 5000; // ms for timer
void setup() {
  // setup stuff
}

void loop() {
  // loop stuff
  if (millis() - timer > samplerate) { // has samplerate time expired?
    timer = millis(); // reset timer
.
.
}

```

---

<div class="post-metadata">

**Author:** ![LarryD](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/larryd/32/215550_2.png) [@LarryD](https://forum.arduino.cc/u/LarryD)\
**Post date:** [November 4, 2025, 6:14am UTC](https://forum.arduino.cc/t/button-implementation-within-a-long-delay-loop/1412116/8 "2025-11-04T06:14:48Z")

</div>

> [@JCA34F](#):
>
> `timer += interval; // restart timer`

## Versus

> [@xfpd](#):
>
> `timer = millis(); // reset timer`

## Versus

```
while ( millis() - timer > interval )
{
  timer = timer + interval;
}  

```

* * *
 
* * *
 
* * *

This is an interesting discussion:

> [@Blink Without Delay problem. Warning.](https://forum.arduino.cc/t/blink-without-delay-problem-warning/382135):
>
> Warning to new people. I just wanted to point out a potential (major) problem with the BWD (Blink Without Delay) technique. There are two (four, see post #4) ways that the BWD timing variable can updated: Method one updates the timing variable by adding the delay time to the timing variable itself. There is an argument that this will prevent slippage in timing. millisOne = millisOne + delayTime; Method two updates the timing variable by setting it equal to the current value of millis()…

---

<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:** [May 3, 2026, 6:15am UTC](https://forum.arduino.cc/t/button-implementation-within-a-long-delay-loop/1412116/9 "2026-05-03T06:15:46Z")

</div>

Closed on May 3, 2026, 6:15am UTC

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