Go Down

Topic: A handy hint? or not. Blink without delay 3.0. (Read 2 times) previous topic - next topic

WizenedEE

Code: [Select]
static uint16_t _lasttime, uint16_t _thistime

is illegal. You might be able to do some strange things with a struct though:

Code: [Select]

#define runAfter(n) for (struct { static uint16_t last; uint16_t now; } _rA = {0, millis()}; _rA.now - _rA.last >= (n); _rA.last += (n))


That doesn't work either, though: static means something different there.


I also think that runEvery is a better name because it reads "Run every 500" (ms) which is exactly what it does. runAfter imples it only runs once, and has a built in delay.

_thistime is unnecessary if you do the += (n) trick, since then millis() wil only be called once.

so this is what I'm thinking of:
Code: [Select]

#define runEvery(t) for (static uint16_t _lasttime; millis() - _lasttime > (t); _lasttime += (t))


which is tested and works on my uno.



Thanks for checking it out though --- hopefully a macro like this one will be useful to others.

pYro_65

#11
Sep 28, 2012, 07:56 am Last Edit: Sep 28, 2012, 08:02 am by pYro_65 Reason: 1

Code: [Select]
static uint16_t _lasttime, uint16_t _thistime

is illegal. You might be able to do some strange things with a struct though:


According to the standard. 6.5.3 - 3

Quote
If the for-init-statement is a declaration, the scope of the name(s) declared extends to the end of the for-statement.


But I see the static now, :( . hmmm

westfw

Both the original (random) and my hack (bitwise and) don't use extra ram...
Whether they're suitable or not depends on unspecified requirements and behaviors.

sbright33

Nice Macro!  Very useful.  I wish there was a way for beginners to find it easily while Googling.
If you fall... I'll be there for you!
-Floor

Skype Brighteyes3333
(262) 696-9619

Coding Badly

I wish there was a way for beginners to find it easily while Googling.


You can give Google a helping hand.  If you or anyone you know has a blog, add a paragraph or two about the macro and include a link to this topic.

Go Up