Hello bismarckfunf
Try this small sketch using an array containing all relevant data.
A time handler based on the milli() function takes care about the led pattern timing.
/* BLOCK COMMENT
ATTENTION: This Sketch contains elements of C++.
https://www.learncpp.com/cpp-tutorial/
Many thanks to LarryD
https://europe1.discourse-cdn.com/arduino/original/4X/7/e/0/7e0ee1e51f1df32e30893550c85f0dd33244fb0e.jpeg
https://forum.arduino.cc/t/how-to-use-arduino-uno-millis-function/969287
Tested with Arduino: Mega[ ] - UNO [X] - Nano [ ]
*/
#define ProjectName "How to use arduino uno millis function()"
// HARDWARE AND TIMER SETTINGS
// YOU MAY NEED TO CHANGE THESE CONSTANTS TO YOUR HARDWARE AND NEEDS
constexpr byte LedPins[] {9, 10, 11, 12}; // portPin o---|220|---|LED|---GND
// VARIABLE DECLARATION AND DEFINITION
unsigned long currentTime;
struct TIMER { // has the following members
unsigned long duration; // memory for interval time
unsigned long stamp; // memory for actual time
bool onOff; // control for start/stop
};
// make array for led pattern and timing information
struct LedDelay {
TIMER wait;
byte ledPattern[sizeof(LedPins)];
};
LedDelay ledDelays[] {
{1000, 0, true, {false, false, false, false}},
{1000, 0, false, {true, false, false, false}},
{1000, 0, false, {false, true, false, false}},
{1000, 0, false, {false, false, true, false}},
{1000, 0, false, {false, false, false, true}},
{500, 0, false, {false, false, false, false}},
{500, 0, false, {false, false, false, true}},
{500, 0, false, {false, false, true, false}},
{500, 0, false, {false, true, false, false}},
{500, 0, false, {true, false, false, false}},
};
// time handler
bool timerEvent (TIMER &timer) {
return (currentTime - timer.stamp >= timer.duration && timer.onOff);
}
// -------------------------------------------------------------------
void setup() {
Serial.begin(9600);
Serial.println(F("."));
Serial.print(F("File : ")), Serial.println(__FILE__);
Serial.print(F("Date : ")), Serial.println(__DATE__);
Serial.print(F("Project: ")), Serial.println(ProjectName);
pinMode (LED_BUILTIN, OUTPUT); // used as heartbeat indicator
// https://www.learncpp.com/cpp-tutorial/for-each-loops/
for (auto Output_ : LedPins) pinMode(Output_, OUTPUT);
}
void loop () {
currentTime = millis();
digitalWrite(LED_BUILTIN, (currentTime / 500) % 2);
// get access to the ledDelay array
for (auto &ledDelay : ledDelays) {
// it´s time for action?
if (timerEvent(ledDelay.wait)) {
// yes, switch off current timer
ledDelay.wait.onOff = false;
// make local variable
static int patternCounter = 0;
// did we get all members of the array?
patternCounter = (patternCounter + 1) % (sizeof(ledDelays) / sizeof(ledDelays[0]));
// set time duration for pattern
ledDelays[patternCounter].wait.stamp = currentTime;
// switch timer ON
ledDelays[patternCounter].wait.onOff = true;
// make local variable
int element = 0;
// and copy led pattern to leds
for (auto ledPin : LedPins) digitalWrite(ledPin, ledDelays[patternCounter].ledPattern[element++]);
}
}
}
Have a nice day and enjoy coding in C++.