Hey everyone, have a question regarding functions in arduino. Is there a command similar to openNextFile(), which is in the SD library, that works with functions instead of files? For example a button click in “void loop” would run the next function in line?
you’d need an array full of pointers to those functions to loop through it. maybe this will make sense: http://stackoverflow.com/questions/252748/using-an-array-of-function-pointers
You could also use a simple “switch” statement in some easier cases if you know the list of functions at compile time.
As @Mark_Kriegsman suggested, using a switch statement is fairly easy. That’s what I do with my Xmas window displays. I have each pattern defined as a function, and a running counter that a switch statement uses to figure out which pattern to display.
In it’s simplest form, my program is setup like this:
- running loop() has a 30 seconds timer that increments a counter (in my case, there’s actually a master node that sends a command via I2C to all the slave nodes to change patterns, but it’s the same thing.)
- the loop() calls displayPattern(counter) constantly
displayPattern(counter) then has a switch statement in it that then calls the specific pattern function based on that counter number. So for example, displayPattern(0) is a falling snow effect, displayPattern(1) is a light chase effect, etc., etc.
What’s important to know here is that each of those functions for the effects do not have either delay() or a blocking “for” loop in them. This allows for a counter increment to take effect immediately rather than waiting for a blocking delay or “for” loop to finish.
Thanks everyone, you have given me a couple of great options. One of these will work. Ashley, I have an external interrupter in place in my code which should allow for functions that happen to have delays. It immediately overrides any functions that are currently running.