# Easy to use non-blocking timing for periodic-delayed code-execution

**URL:** <https://forum.arduino.cc/t/easy-to-use-non-blocking-timing-for-periodic-delayed-code-execution/1073967>\
**Category:** Tutorials\
**Created:** [January 6, 2023, 10:53am UTC](https://forum.arduino.cc/t/easy-to-use-non-blocking-timing-for-periodic-delayed-code-execution/1073967 "2023-01-06T10:53:58Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![StefanL38](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/stefanl38/32/713548_2.png) [@StefanL38](https://forum.arduino.cc/u/StefanL38)\
**Post date:** [January 6, 2023, 10:53am UTC](https://forum.arduino.cc/t/easy-to-use-non-blocking-timing-for-periodic-delayed-code-execution/1073967/1 "2023-01-06T10:53:58Z")

</div>

Hi Everybody,

I'm using a self-written function for non-blocking periodic-delayed code-execution.  
The most simplest example is switching an LED **ON** / off. Which could be called toggle the LED

If LED is off =\> switch LED **ON**  
.  
If LED is **ON** =\> switch LED off

```cpp
void toggleLED_OnOff(byte myLED_Pin) {
  if ( digitalRead(myLED_Pin) == LOW) { // if LED is off
    digitalWrite(myLED_Pin,HIGH); // switch LED ON
  }
  else { // if LED is ON
    digitalWrite(myLED_Pin,LOW); // switch LED off    
  }
}

```

example to switch on / off a LED periodically and continiously once every 5 seconds (5000 milliseconds)

as a time-table

```cpp
actual| 
time | state of LED
00:00 | switch LED ON
00:05 | switch LED off
00:10 | switch LED ON
00:15 | switch LED off
00:20 | switch LED ON
00:25 | switch LED off
00:30 | switch LED ON
00:35 | switch LED off
...

```

**most basic example:**

```cpp

const byte LED_Pin = 13;
unsigned long StartTime;

void toggleLED_OnOff(byte myLED_Pin) {
  if ( digitalRead(myLED_Pin) == LOW) { // if LED is off
    digitalWrite(myLED_Pin,HIGH); // switch LED ON
  }
  else { // if LED is ON
    digitalWrite(myLED_Pin,LOW); // switch LED off    
  }
}

void setup() {
  pinMode(LED_Pin,OUTPUT);
  digitalWrite(LED_Pin,HIGH); // switch ON LED
  StartTime = millis(); // initialise variable StartTime with actual value of function millis()
}

void loop() {
  // code that shall be executed all the time

  
  // check if more or less then 5000 milliseconds have passed by
  if ( TimePeriodIsOver(StartTime,5000) ) {
    // if 5000 milliseconds HAVE passed by automatically update variable StartTime
    toggleLED_OnOff(LED_Pin); // toggle LED 
  }
}

// easy to use helper-function for non-blocking timing
boolean TimePeriodIsOver (unsigned long &startOfPeriod, unsigned long TimePeriod) {
  unsigned long currentMillis = millis();
  if ( currentMillis - startOfPeriod >= TimePeriod ) {
    // more time than TimePeriod has elapsed since last time if-condition was true
    startOfPeriod = currentMillis; // a new period starts right here so set new starttime
    return true;
  }
  else return false; // actual TimePeriod is NOT yet over
}
```

to demonstrate the **non** -blocking character of this timing-function the code-version below uses the serial monitor in a special way similar to a LC-Display  
The effect will be that you see just one single line with text in the serial monitor that shows a fast upcounting number and the state of the LED beeing ON or off

you see 5 seconds long

```cpp
counting fast 2524740 LED is switched ON

```

then 5 seconds long

```cpp
counting fast 2558332 LED is switched off

```

and so on

the fast counting up number shows how many times per second loop is iterating the lines of code

```cpp

const byte LED_Pin = 13;
unsigned long StartTime;
unsigned long myCounter;
unsigned long PrintTimer;

void toggleLED_OnOff(byte myLED_Pin) {
  if ( digitalRead(myLED_Pin) == LOW) { // if LED is off
    digitalWrite(myLED_Pin, HIGH); // switch LED ON
  }
  else { // if LED is ON
    digitalWrite(myLED_Pin, LOW); // switch LED off
  }
}

void setup() {
  Serial.begin(115200);
  pinMode(LED_Pin, OUTPUT);
  digitalWrite(LED_Pin, HIGH); // switch ON LED
  StartTime = millis(); // initialise variable StartTime with actual value of function millis()
  myCounter = 0;
}

void loop() {
  // code that shall be executed all the time
  myCounter++; // increment variable myCounter by 1 with each iteration of loop()
  PrintCounter_and_LEDState();

  // check if more or less then 5000 milliseconds have passed by
  if ( TimePeriodIsOver(StartTime, 5000) ) {
    // if 5000 milliseconds HAVE passed by automatically update variable StartTime
    toggleLED_OnOff(LED_Pin); // toggle LED
  }
}

// easy to use helper-function for non-blocking timing
boolean TimePeriodIsOver (unsigned long &startOfPeriod, unsigned long TimePeriod) {
  unsigned long currentMillis = millis();
  if ( currentMillis - startOfPeriod >= TimePeriod ) {
    // more time than TimePeriod has elapsed since last time if-condition was true
    startOfPeriod = currentMillis; // a new period starts right here so set new starttime
    return true;
  }
  else return false; // actual TimePeriod is NOT yet over
}

void ClearSerialMonitor() {
  for (byte i = 0; i < 40; i++) {
    Serial.println();
  }
}

void PrintCounter_and_LEDState() {

  // to create a steady-looking text print 40 empty lines
  // that scroll the last printed text out of sight
  if ( TimePeriodIsOver(PrintTimer, 200) ) { // once every 200 milliseconds
    ClearSerialMonitor(); 

    Serial.print("counting fast ");
    Serial.print(myCounter);
    Serial.print(" LED is switched ");
    if ( digitalRead(LED_Pin) == HIGH) {
      Serial.println("ON");
    }
    else {
      Serial.println("off");
    }
  }
}
```

---

<div class="post-metadata">

**Author:** ![StefanL38](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/stefanl38/32/713548_2.png) [@StefanL38](https://forum.arduino.cc/u/StefanL38)\
**Post date:** [January 6, 2023, 10:58am UTC](https://forum.arduino.cc/t/easy-to-use-non-blocking-timing-for-periodic-delayed-code-execution/1073967/2 "2023-01-06T10:58:38Z")

</div>

This is a more graphical explanation how the function TimePeriodIsOver() works

 ![image](https://europe1.discourse-cdn.com/arduino/original/4X/a/5/7/a575edf7728861c9880eb1dc148cf96513ffcfde.png)

In continioulsy periodic use it looks like this

 ![image](https://europe1.discourse-cdn.com/arduino/original/4X/0/a/a/0aa0987cca2275ae83a17a1a86ed1f241b47aaa3.jpeg)

---

<div class="post-metadata">

**Author:** ![buckfast\_beekeeper](https://avatars.discourse-cdn.com/v4/letter/b/74df32/32.png) [@buckfast\_beekeeper](https://forum.arduino.cc/u/buckfast_beekeeper)\
**Post date:** [January 6, 2023, 11:26am UTC](https://forum.arduino.cc/t/easy-to-use-non-blocking-timing-for-periodic-delayed-code-execution/1073967/3 "2023-01-06T11:26:57Z")

</div>

> [@StefanL38](#):
>
> ```arduino
> void toggleLED_OnOff(byte myLED_Pin) {
> if ( digitalRead(myLED_Pin) == LOW) { // if LED is off
> digitalWrite(myLED_Pin,HIGH); // switch LED ON
> }
> else { // if LED is ON
> digitalWrite(myLED_Pin,LOW); // switch LED off    
> }
> }
> 
> ```

Can much easier

```arduino
void toggleLED_OnOff(byte myLED_Pin) {
  digitalWrite(myLED_Pin, !digitalRead(myLED_Pin));
}

```

---

<div class="post-metadata">

**Author:** ![StefanL38](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/stefanl38/32/713548_2.png) [@StefanL38](https://forum.arduino.cc/u/StefanL38)\
**Post date:** [January 6, 2023, 11:30am UTC](https://forum.arduino.cc/t/easy-to-use-non-blocking-timing-for-periodic-delayed-code-execution/1073967/4 "2023-01-06T11:30:27Z")

</div>

> [@buckfast\_beekeeper](#):
>
> Can much easier
> 
> ```arduino
> void toggleLED_OnOff(byte myLED_Pin) {
> digitalWrite(myLED_Pin, !digitalRead(myLED_Pin));
> 
> ```

Yes I know this version. But presenting this version without explaining it throurougly leaves beginners with scratching their head: Huh what wizardness is this??!

---

<div class="post-metadata">

**Author:** ![buckfast\_beekeeper](https://avatars.discourse-cdn.com/v4/letter/b/74df32/32.png) [@buckfast\_beekeeper](https://forum.arduino.cc/u/buckfast_beekeeper)\
**Post date:** [January 6, 2023, 11:36am UTC](https://forum.arduino.cc/t/easy-to-use-non-blocking-timing-for-periodic-delayed-code-execution/1073967/5 "2023-01-06T11:36:04Z")

</div>

Programming without the knowledge of || == && != \<= \>= ... that isn't possible.

---

<div class="post-metadata">

**Author:** ![StefanL38](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/stefanl38/32/713548_2.png) [@StefanL38](https://forum.arduino.cc/u/StefanL38)\
**Post date:** [January 6, 2023, 11:41am UTC](https://forum.arduino.cc/t/easy-to-use-non-blocking-timing-for-periodic-delayed-code-execution/1073967/6 "2023-01-06T11:41:06Z")

</div>

Write a turial about them include the single not-operator !

---

<div class="post-metadata">

**Author:** ![paulpaulson](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/paulpaulson/32/363643_2.png) [@paulpaulson](https://forum.arduino.cc/u/paulpaulson)\
**Post date:** [January 6, 2023, 1:40pm UTC](https://forum.arduino.cc/t/easy-to-use-non-blocking-timing-for-periodic-delayed-code-execution/1073967/7 "2023-01-06T13:40:52Z")

</div>

Why ?

[https://www.learncpp.com/cpp-tutorial/logical-operators/](https://www.learncpp.com/cpp-tutorial/logical-operators/)

Read - Understand - Make !

---

<div class="post-metadata">

**Author:** ![J-M-L](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/j-m-l/32/766245_2.png) [@J-M-L](https://forum.arduino.cc/u/J-M-L)\
**Post date:** [January 6, 2023, 4:48pm UTC](https://forum.arduino.cc/t/easy-to-use-non-blocking-timing-for-periodic-delayed-code-execution/1073967/8 "2023-01-06T16:48:09Z")

</div>

this is poor coding. You use boolean operator and boolean promotion and the "undocumented" knowledge (unless you go check the source code) that HIGH is 1 and LOW is 0 and are of integer type and not a class type that does not promote to an integral type or back from an integral type...

> [@buckfast\_beekeeper](#):
>
> Can much easier
> 
> ```arduino
> void toggleLED_OnOff(byte myLED_Pin) {
> digitalWrite(myLED_Pin, !digitalRead(myLED_Pin));
> }
> 
> ```

if you want to do short and clean use the ternary operator. This way you respect the API and types

```arduino
void toggleLED_OnOff(byte myLED_Pin) {
  digitalWrite(myLED_Pin, (digitalRead(myLED_Pin) == HIGH) ? LOW : HIGH);
}

```

see this to know what could happen if Arduino decided to change the way HIGH and LOW are defined... it's not unheard of...

> <https://github.com/arduino/ArduinoCore-API/issues/25>
>
> This project changes \`LOW\`, \`HIGH\`, \`INPUT\`, \`INPUT\_PULLUP\`, and \`OUTPUT\` from m…acros to enums:
> https://github.com/arduino/ArduinoCore-API/blob/e1eb8de126786b7701b211332dda3f09aa400f35/api/Common.h#L10-L23
> 
> I'm concerned that this will cause breakage of a significant amount of existing code.
> 
> An example is the popular \[Keypad library\](https://playground.arduino.cc/code/keypad). Compilation of both the \[original\](https://playground.arduino.cc/uploads/Code/keypad.zip) library and the \[version in Library Manager\](https://github.com/Chris--A/Keypad) fails once this change is made.
> \`\`\`
> In file included from E:\\electronics\\arduino\\libraries\\Keypad-master\\examples\\HelloKeypad\\HelloKeypad.ino:10:0:
> 
> E:\\electronics\\arduino\\libraries\\Keypad-master\\src/Keypad.h: In member function 'virtual void Keypad::pin\_write(byte, boolean)':
> 
> E:\\electronics\\arduino\\libraries\\Keypad-master\\src/Keypad.h:81:81: error: cannot convert 'boolean {aka bool}' to 'PinStatus' for argument '2' to 'void digitalWrite(pin\_size\_t, PinStatus)'
> 
> virtual void pin\_write(byte pinNum, boolean level) { digitalWrite(pinNum, level); }
> \`\`\`
> This commonly used code will also now fail:
> \`\`\`c++
> digitalWrite(13, !digitalRead(13)); // toggle pin 13
> \`\`\`
> \`\`\`
> toggle:2:36: error: cannot convert 'bool' to 'PinStatus' for argument '2' to 'void digitalWrite(pin\_size\_t, PinStatus)'
> 
> digitalWrite(13, !digitalRead(13)); // toggle pin 13
> \`\`\`
> I understand that the root cause of these errors is bad code and that any code which followed best practices will have no problems with this change. However, I fear there is a lot of bad code in widespread use that currently works fine. In the case of the Keypad library, it is unlikely it can even be fixed since Chris--A has gone AWOL. I'm sure there are other such abandoned projects.
> 
> I do like the spirit of this change (though lumping \`CHANGE\`, \`FALLING\`, and \`RISING\` into \`PinStatus\` is questionable). I'm open to being convinced that it's worth the breakage and, if so, I'm willing to help ease the transition by providing user support and submitting PRs to fix broken code. I just think this warrants some consideration before ArduinoCore-API goes into more widespread use.
> 
> Some previous discussion on the topic:
> \- http://forum.arduino.cc/index.php?topic=455579 (from \[here\](http://forum.arduino.cc/index.php?topic=455579.msg4001965#msg4001965) onward)
> \- https://forum.arduino.cc/index.php?topic=584322
> \- http://forum.arduino.cc/index.php?topic=602250
> \- https://forum.arduino.cc/index.php?topic=621429
> \- https://forum.arduino.cc/index.php?topic=627883.msg4319254#msg4319254
> \- https://forum.arduino.cc/index.php?topic=659624
> \- https://github.com/arduino/ArduinoCore-megaavr/issues/68

---

<div class="post-metadata">

**Author:** ![jfjlaros](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/jfjlaros/32/716915_2.png) [@jfjlaros](https://forum.arduino.cc/u/jfjlaros)\
**Post date:** [January 6, 2023, 6:00pm UTC](https://forum.arduino.cc/t/easy-to-use-non-blocking-timing-for-periodic-delayed-code-execution/1073967/9 "2023-01-06T18:00:13Z")

</div>

> [@StefanL38](#):
>
> ` else return false; // actual TimePeriod is NOT yet over`

The `else` is [redundant](https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#f56-avoid-unnecessary-condition-nesting) here.

---

<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:** [January 7, 2023, 7:57am UTC](https://forum.arduino.cc/t/easy-to-use-non-blocking-timing-for-periodic-delayed-code-execution/1073967/10 "2023-01-07T07:57:33Z")

</div>

@StefanL38  
please, before your write any more "content" ... please read and apply:

> **[Arduino Style Guide for Writing Content | Arduino Documentation](https://docs.arduino.cc/learn/contributions/arduino-writing-style-guide)**
>
> Learn how to write clear Arduino examples that can be read by beginners and advanced users alike.

and

> **[Arduino Style Guide for Creating Libraries | Arduino Documentation](https://docs.arduino.cc/learn/contributions/arduino-library-style-guide)**
>
> Learn how to write library APIs in an Arduino style.

just an example: you were told regarding camelCase over and over again, so please don't post any more of your "content" as long as you don't get that right! If you need help on that, first post in programming and let others correct your code before you put it into tutorials!

---

<div class="post-metadata">

**Author:** ![StefanL38](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/stefanl38/32/713548_2.png) [@StefanL38](https://forum.arduino.cc/u/StefanL38)\
**Post date:** [January 7, 2023, 8:12am UTC](https://forum.arduino.cc/t/easy-to-use-non-blocking-timing-for-periodic-delayed-code-execution/1073967/11 "2023-01-07T08:12:58Z")

</div>

> [@noiasca](#):
>
> just an example: you were told regarding camelCase over and over again, so please don't post any more of your "content" as long as you don't get that right! If you need help on that, first post in programming and let others correct your code before you put it into tutorials!

I see this as a **totally minor** problem. The Arduino-Team seems to see writing in camelCase as the almost only thing that is important about example-codes that the Arduino-Team is delivering with their examples.

As long as the Arduino-Team does not change to a much more descriptive style in the example-codes, as long as most of the users here don't apply the recommend tutorial-rules in their answers I won't care about that. I claim that my tutorials are easier to understand as most others.

If you can point to specific things and explain **why** the way I have written it is **confusing** beginners I will think about it.

---

<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:** [January 7, 2023, 8:21am UTC](https://forum.arduino.cc/t/easy-to-use-non-blocking-timing-for-periodic-delayed-code-execution/1073967/12 "2023-01-07T08:21:04Z")

</div>

I don't discuss with you why or why not camelCase. It's described, it's common practice. No more to add. Just use it.

Another thing which confuses

```cpp
  StartTime = millis(); // initialise variable StartTime with actual value of function millis()

```

You could explain why you want to have this variable the value of millis() at the end of setup. at this point millis() will have lit. "no" progress (tests show it is 0). This obscure line can be omitted without any negative effect during runtime. StartTime is a global so it will be initialized with 0 anyway. if you want to make it clear that it is 0, do that together with the declaration

Very similar:

```cpp
myCounter = 0;

```

why? And if myCounter = 0 would have been important, why have you left out PrintTimer?

People might have different styles to write code, but at least they should be consistent within their code. Your code isn't consistent and can confuse others.

If I can't accept feedback on my work I would not publish code.

---

<div class="post-metadata">

**Author:** ![paulpaulson](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/paulpaulson/32/363643_2.png) [@paulpaulson](https://forum.arduino.cc/u/paulpaulson)\
**Post date:** [January 7, 2023, 8:39am UTC](https://forum.arduino.cc/t/easy-to-use-non-blocking-timing-for-periodic-delayed-code-execution/1073967/13 "2023-01-07T08:39:59Z")

</div>

Hello [noiasca](https://forum.arduino.cc/u/noiasca)

Many thanks for the links.

Perfect documentations to make a good job to design useful tutorials for this forum. 👍

Have a nice day and enjoy coding in C++.

---

<div class="post-metadata">

**Author:** ![StefanL38](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/stefanl38/32/713548_2.png) [@StefanL38](https://forum.arduino.cc/u/StefanL38)\
**Post date:** [January 7, 2023, 8:55am UTC](https://forum.arduino.cc/t/easy-to-use-non-blocking-timing-for-periodic-delayed-code-execution/1073967/14 "2023-01-07T08:55:53Z")

</div>

> [@noiasca](#):
>
> explain why you want to have this variable the value of millis() at the end of setup.

In bigger code where some actions do start at an unpredictable time f.e. caused by a button-pressthe initialisation is required to make it work. So I introduce to this in the very first example.

But I will think about if I add some explanation about this or adding a follow-up example with a button-press

---

<div class="post-metadata">

**Author:** ![J-M-L](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/j-m-l/32/766245_2.png) [@J-M-L](https://forum.arduino.cc/u/J-M-L)\
**Post date:** [January 7, 2023, 10:03am UTC](https://forum.arduino.cc/t/easy-to-use-non-blocking-timing-for-periodic-delayed-code-execution/1073967/15 "2023-01-07T10:03:27Z")

</div>

> [@StefanL38](#):
>
> As long as the Arduino-Team does not change to a much more descriptive style in the example-codes, as long as most of the users here don't apply the recommend tutorial-rules in their answers I won't care about that. I claim that my tutorials are easier to understand as most others.

For what’s it’s worth I actually find your documentation style totally cumbersome and your variables naming is hurting my eyes

by adding tons of unrelated stuff (debug macros, async blinking etc) and relatively useless comments you loose sight of the primary goals of your tutorials and confuse beginners. That’s the main reason I would not recommend them.

But it’s a free world so do as you want until mods get unhappy. This is arduino space at the end of the day

---

<div class="post-metadata">

**Author:** ![jfjlaros](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/jfjlaros/32/716915_2.png) [@jfjlaros](https://forum.arduino.cc/u/jfjlaros)\
**Post date:** [January 7, 2023, 10:40am UTC](https://forum.arduino.cc/t/easy-to-use-non-blocking-timing-for-periodic-delayed-code-execution/1073967/16 "2023-01-07T10:40:10Z")

</div>

The style guide could use a few touch ups.

> [@](#):
>
> ```arduino
> if (somethingIsTrue == TRUE) {
> 
> ```

P.S. I could not find the source, otherwise I would have contacted the maintainers.

[edit]

Update: I found the repo, the issue is [fixed](https://github.com/arduino/docs-content/pull/759) now.

---

<div class="post-metadata">

**Author:** ![J-M-L](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/j-m-l/32/766245_2.png) [@J-M-L](https://forum.arduino.cc/u/J-M-L)\
**Post date:** [January 7, 2023, 10:46am UTC](https://forum.arduino.cc/t/easy-to-use-non-blocking-timing-for-periodic-delayed-code-execution/1073967/17 "2023-01-07T10:46:42Z")

</div>

agreed, recommending == TRUE is really poor. The notion of a truth value is very well known as in natural language you would not say

```plaintext
if it is raining is true then
  open your umbrella 

```

you say

```plaintext
if it is raining then
  open your umbrella 

```

---

<div class="post-metadata">

**Author:** ![jfjlaros](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/jfjlaros/32/716915_2.png) [@jfjlaros](https://forum.arduino.cc/u/jfjlaros)\
**Post date:** [January 7, 2023, 10:49am UTC](https://forum.arduino.cc/t/easy-to-use-non-blocking-timing-for-periodic-delayed-code-execution/1073967/18 "2023-01-07T10:49:29Z")

</div>

Moreover, `TRUE` (uppercase) is not defined.

---

<div class="post-metadata">

**Author:** ![StefanL38](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/stefanl38/32/713548_2.png) [@StefanL38](https://forum.arduino.cc/u/StefanL38)\
**Post date:** [January 7, 2023, 10:55am UTC](https://forum.arduino.cc/t/easy-to-use-non-blocking-timing-for-periodic-delayed-code-execution/1073967/19 "2023-01-07T10:55:52Z")

</div>

> [@jfjlaros](#):
>
> The style guide could use a few touch ups.
> 
> > [@](#):
> >
> > ```arduino
> > if (somethingIsTrue == TRUE) {
> > 
> > ```

Well if the variablename contains the word "true" I agree.  
But in most cases it does not explicitly contain the word "true"  
and then I would recommend to show both versions.

```arduino
if (characterReceived == true) {
  // process character
}

```

as variable characterReceived is of type boolean **itself** this can be written shorter as

```arduino
if (characterReceived) {
  // process character
}

```

And add a link to the explanation how in C++ the if-statement treats value zero and non-zero values

Of course this adds text. And some experienced users might be annoyed by reading additional text. Well my opinion is:  
As an expert it is a **peace of cake** to jump over additional text or redundant comments  
because an expert knows where to look at despite to beginners

---

<div class="post-metadata">

**Author:** ![J-M-L](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/j-m-l/32/766245_2.png) [@J-M-L](https://forum.arduino.cc/u/J-M-L)\
**Post date:** [January 7, 2023, 11:06am UTC](https://forum.arduino.cc/t/easy-to-use-non-blocking-timing-for-periodic-delayed-code-execution/1073967/20 "2023-01-07T11:06:41Z")

</div>

> [@StefanL38](#):
>
> And add a link to the explanation how in C++ the if-statement treats value zero and non-zero values

why ? the concept of truth value is represented by the boolean type in the first place. So if the user was smart enough to make something a truth value, then he should stick with this.

```arduino
bool itIsRaining = (digitalRead(rainSensorPin) == HIGH); // will be true when the sensor detects the rain.
...
if (itIsRaining) { // is it raining ?
  digitalWrite(umbrellaPin, HIGH); // yes, open the umbrella
}

```

otherwise stick to non boolean values

```arduino
int rainSensorStatus = digitalRead(rainSensorPin); // will be HIGH when the sensor detects the rain.
...
if (rainSensorStatus == HIGH) { // is it raining?
  digitalWrite(umbrellaPin, HIGH); // yes, open the umbrella
}

```

[Next page](https://forum.arduino.cc/t/easy-to-use-non-blocking-timing-for-periodic-delayed-code-execution/1073967.md?page=2)
