Go Down

Topic: Class to simplify "blink without delay" (Read 1 time) previous topic - next topic

Nick Gammon

The original uses 4 bytes for previousMillis anyway, so I don't know that you need to worry about that too much.

Nick Gammon


How did you ever track down the solution to an error message like "__cxa_guard_acquire"?


Google was my friend there. Someone on one of the AVR forums worked out how to do it.

PaulS

Nice job there, and a good explanation.

But...

Class names typically start with an upper case letter - Serial, Ethernet, Client, Server, LiquidCrystal, etc. Yours should, too.

Why did you declare the global instances static?

Nick Gammon


The name "blink without delay" itself is confusing. How can you blink with delaying? You will be always on!!


It is really "how to blink without using delay ()".

Some sort of delay is required to make things blink, but the idea I'm trying to convey is "use a timer interval, not the delay() function".

Nick Gammon

#9
May 11, 2011, 12:15 pm Last Edit: May 11, 2011, 01:05 pm by Nick Gammon Reason: 1

Nice job there, and a good explanation.

But...

Class names typically start with an upper case letter - Serial, Ethernet, Client, Server, LiquidCrystal, etc. Yours should, too.


I have modified the original post to incorporate your suggestion (ie. changed elapsed to Elapsed). Also the downloadable library has been amended. May as well be consistent, eh?

Quote
Why did you declare the global instances static?


I know it's not necessary, but does no harm. The real reason was that if you copied and pasted it into a function, you retain the "static" part. So if you copy it into "loop" it won't really work unless it is static.

I'm inclined to prefer this version:

Code: [Select]
void loop() {
static Elapsed t2, t3;
...


The reason is that the scope of the variable is inside loop, where it belongs. But the lifetime is global, so it continues to accrue elapsed time, even after you leave loop and go back into it. But without the static keyword, it won't behave as expected.

Go Up