The only obvious thing is that you have a status flag for which you can increase the value, but you do not test it in the first if. It would be better to structure your code a bit more. You can also track more than one value if you have more than one output, unless they all disappear and exit at the same time. In this case, you will be better off with an array of struct with parameters for each pine.
One way to use delay with multiple tasks is to have each task run for the time that has elapsed since the last cycle and set the delay at the end of the cycle to the time it takes to complete the tasks. If your loop looks something like this:
static unsigned long last_time = 0; // set to millis() in setup static unsigned long refresh_period = 20; // 50Hz void loop() { unsigned long time = millis(); unsigned long delta = time - last_time; doTask1( delta ); doTask2( delta ); doTask3( delta ); doTask4( delta ); // as tasks may have taken some millis, adjust delay unsigned long post_time = millis(); if ( post_time - last_time < refresh_period ) delay( refresh_period - ( post_time - last_time ) ); last_time = time; }
Then each task will be executed approximately every 20 ms and will be transmitted 20 or so how many milliseconds to update their status. This way you get some delay, but everything has a chance to update.
source share