# IO program control

**URL:** <https://forum.arduino.cc/t/io-program-control/1269530>\
**Category:** Programming\
**Created:** [June 8, 2024, 11:09am UTC](https://forum.arduino.cc/t/io-program-control/1269530 "2024-06-08T11:09:45Z")\
**Posts on this page:** 8\
**Page:** 2

<div class="post-metadata">

**Author:** ![GoForSmoke](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/goforsmoke/32/1127097_2.png) [@GoForSmoke](https://forum.arduino.cc/u/GoForSmoke)\
**Post date:** [June 10, 2024, 8:26am UTC](https://forum.arduino.cc/t/io-program-control/1269530/21 "2024-06-10T08:26:54Z")

</div>

With Arduino millis(), 35Hz interval 1000/35 millis (rounded down, integer divides always round down) = 28ms.

If you use Arduino micros(), the resolution is 4, rounded down.  
1000000/35 = 28568 microseconds = 28.568 milliseconds.

Arduino millis() is always +/- 1.

If close matters, use micros!

---

<div class="post-metadata">

**Author:** ![GoForSmoke](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/goforsmoke/32/1127097_2.png) [@GoForSmoke](https://forum.arduino.cc/u/GoForSmoke)\
**Post date:** [June 10, 2024, 8:40am UTC](https://forum.arduino.cc/t/io-program-control/1269530/22 "2024-06-10T08:40:51Z")

</div>

> [@akaf50](#):
>
> Timers like you stated?

Using time to blink lets your code be non-blocking where delay blocks. If you want to blink the led +and+ watch a button at the same time, using delay() means the button can only be sensed when delay() is not running and the button is down! that's what blocking does. Blink and button is 2 things, using time lets your code do more than one thing at a time!

When your code gets bigger/busier, consider that a fast running loop uses a few hundred cpu cycles per loop while a delay() wastes 16000 cycles per ms. Yikes? Timers without delay lets a sketch do wayyy more things per second!

It takes a while to learn well but it is well worth the investment!

---

<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:** [June 10, 2024, 8:53am UTC](https://forum.arduino.cc/t/io-program-control/1269530/23 "2024-06-10T08:53:24Z")

</div>

> [@GoForSmoke](#):
>
> \*uint\_32 is pointer to 32-bit unsigned integer.  
> unsigned long is a 32-bit unsigned integer.

Thanks for that; I dropped the `*` after the `uint32_t` by accident. It was supposed to be in italic; fixed.

---

<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:** [June 10, 2024, 3:16pm UTC](https://forum.arduino.cc/t/io-program-control/1269530/24 "2024-06-10T15:16:46Z")

</div>

> [@GoForSmoke](#):
>
> Oh. I'm a bit deaf there.

Let me try to explain; please don't take this the wrong way.

For my feeling, your code is too dense. What throws me off is all comments that are on the same line as the statements as well as no alignment of comments. For me, it does not improve readability.

As can be seen in my post, I prefer comments on their own line, specifically for conditions but in general as well; I think that it makes the code more readable.

Further I prefer to explain an approach in steps instead of throwing code at people. Your code is sufficiently documented but requires more understanding of your thinking process while the stepwise approach is, in my opinion, more useful to beginners as it demonstrates the (or my) thinking.

Note:  
I learned something new from your sketch; the use of `timer0_millis` for AVR based boards.

---

<div class="post-metadata">

**Author:** ![GoForSmoke](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/goforsmoke/32/1127097_2.png) [@GoForSmoke](https://forum.arduino.cc/u/GoForSmoke)\
**Post date:** [June 10, 2024, 8:08pm UTC](https://forum.arduino.cc/t/io-program-control/1269530/25 "2024-06-10T20:08:15Z")

</div>

The loop counter was added to give the user a real idea of how fast void loop() runs with blink + button + counter. The first time I added a counter to a button and led example it about knocked my socks off!

Downside, the counter adds complication so perhaps I should have posted the simpler example without it. Note that this leaner and meaner counter is the product of a forum thread where better coders than myself chipped in to make it better. It is Forum Code for anyone to use, doesn't need to be understood to add to any non-blocking sketch.  
What it is for is a development tool that can show the impact of sketch changes, did loop() get faster or slower and how much.  
Remove the counter when the sketch is done and it runs faster!

Comments on their own lines... before or after the code line they refer to?

Since 1980 my own feelings have been that the code is the comments after a lot of fixing business code that at least 4 others had danced through (in everything is rush work mode) where too often the comments did not reflect the code.  
But here, we need comments and they need to be accurate.

Also IMO, gold-standard commenting triples the amount of work it takes to write the code and is a real creativity suck!

---

<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:** [June 11, 2024, 4:43am UTC](https://forum.arduino.cc/t/io-program-control/1269530/26 "2024-06-11T04:43:02Z")

</div>

> [@GoForSmoke](#):
>
> Comments on their own lines... before or after the code line they refer to?

Before the statement

```cpp
// if it's time
if(millis() - lastTime >= interval)
{
  ...
  ...
}

```

Comments are important; similar to you, I add a lot of additional comments to code for the forum that will not be in my own code. Real comments, in my opinion, in my code are block comments for a part of the code that is complicated (read: that I battled with to implement correctly) so I later don't have a what-the-heck moment when I look back.

I totally agree that comments need to be accurate. I however don't agree that that they influence my creativity.

---

<div class="post-metadata">

**Author:** ![GoForSmoke](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/goforsmoke/32/1127097_2.png) [@GoForSmoke](https://forum.arduino.cc/u/GoForSmoke)\
**Post date:** [June 11, 2024, 8:14am UTC](https://forum.arduino.cc/t/io-program-control/1269530/27 "2024-06-11T08:14:34Z")

</div>

I feel that explaining logic in English is like trying to explain a joke.

---

<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 8, 2024, 8:14am UTC](https://forum.arduino.cc/t/io-program-control/1269530/28 "2024-12-08T08:14:39Z")

</div>

Closed on December 8, 2024, 8:14am UTC

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

[Previous page](https://forum.arduino.cc/t/io-program-control/1269530.md?page=1)
