I'm looking for some feedback on my proposed strategy for flashing LEDs on an R4 Uno Minima.
And this is beyond the basic examples using delay and millis.
The essential point is that the application code will be doing other things which may, or may not, affect how the flashing happens.
A simple scenario. I'm using the built in CAN bus controller to connect into an existing automation network which can send out a command instructing my connected node (or other nodes) to control an LED:
- on ,
- off ,
- flash at around 1 Hz 50% duty cycle,
- flash faster at around 2Hz, or
- flash very fast at around 4Hz.
Inside my loop I'm monitoring button presses, reading DS1820 style temp sensors, doing some floating point maths (potentially slow even with the Minima's FPU) etc and then sending messages back onto the CAN bus. So all of these are non trivial operations and can affect LED timing.
I have written a C++ class to encapsulate the node.
My approach is to put a public method to update the elapsed time, and a public class member to read the state of the LED. Psuedo code:
class CanbusNode
{
public:
bool feedbackLED = false;
void updateLEDTime(unsigned long ticks);
....
I'd then call this function as the last statement in my loop procedure and set the physical LED based on the boolean class member
node CanbusNode;
// initialisation etc elsewhere
void loop() {
// do all the other time consuming stuff
node.updateLEDTime(millis());
digitalWrite(node->feedbackLED, HIGH);
}
Inside of the updateLEDTime function I will compare the incoming ticks parameter to a class level variable and if sufficient time has elapsed and the mode is one of the flashing types, then toggle the boolean value from true to false etc.
Note that the mode is set by a different incoming Canbus message (off,on,flash slow, flash fast, flash very fast) .
Does this sound like a sensible strategy?
Are there any suggestions for improvements ?