Ein Beispiel für Timer / millis

Ich bin ein Anfänger in der Arduino Welt,
könnte sein das jemand mein Beispiel gebrauchen kann.

Würde mich über Verbesserungsvorschläge freuen.

benutzen:
struct DELAY MyTimer1 = run(ON TIME, OFF TIME);

Beispiel 1
TIMER 1 / ON 10 sec / OFF 2 sec

int count0 = 0;
int count1 = 0;
unsigned long currentMillis = millis();

struct DELAY                  // Datentyp für DELAY
{
  unsigned long previousMillis;
  unsigned long intervalOn;
  unsigned long intervalOff;
  bool TRIGGER;
};



void setup() {
  Serial.begin(9600);
  Serial.println("Serial initialized!");
}



void loop() {
  currentMillis = millis();
  struct DELAY MyTimer1 = run(10000, 2000);  // SET TIMER 1 /  ON 10 sec / OFF 2 sec            

                                    
  count0 += 1;
  Serial.print(MyTimer1.TRIGGER ? ">> MyTimer1: ON  : " : "<< MyTimer1: OFF :");
  Serial.println(count0);
}



struct DELAY run(long _on, long _off)     //Funktion 
{
  struct DELAY MyTIMER;

  MyTIMER.intervalOn = _on;
  MyTIMER.intervalOff = _off;

  if (MyTIMER.TRIGGER == HIGH && currentMillis - MyTIMER.previousMillis > MyTIMER.intervalOn) // ON
  {
    MyTIMER.previousMillis = currentMillis;
    MyTIMER.TRIGGER = LOW;
  }


  if (MyTIMER.TRIGGER == LOW && currentMillis - MyTIMER.previousMillis > MyTIMER.intervalOff) // OFF
  {
    MyTIMER.previousMillis = currentMillis;
    MyTIMER.TRIGGER = HIGH;
  }
  
  return MyTIMER;
}

Beispiel 2
TIMER 1 / ON 10 sec / OFF 2 sec
TIMER 2 / ON 5 sec / OFF 3 sec
TIMER 3 / ON 15 sec / OFF 15 sec

int count0 = 0;
int count1 = 0;
int count2 = 0;
int count3 = 0;
int count4 = 0;
int count5 = 0;

unsigned long currentMillis = millis();

bool STATE1 = false;
bool STATE2 = false;
bool STATE3 = false;

struct DELAY                  // Datentyp für DELAY
{
  unsigned long previousMillis;
  unsigned long intervalOn;
  unsigned long intervalOff;
  bool TRIGGER;
};



void setup() {
  Serial.begin(9600);
  Serial.println("Serial initialized!");
}



void loop() {
  currentMillis = millis();
  struct DELAY MyTimer1 = run(10000, 2000), // SET TIMER 1 /  ON 10 sec / OFF 2 sec
         MyTimer2 = run(5000, 3000),  // SET TIMER 2 /  ON 5 sec / OFF 3 sec 
         MyTimer3 = run(15000, 15000);  // SET TIMER 3 /  ON 15 sec / OFF 15 sec



  if (MyTimer1.TRIGGER != STATE1)
  {
    if (MyTimer1.TRIGGER == HIGH)
    {
      count0 += 1;
      Serial.print(">> MyTimer1: ON  : ");
      Serial.println(count0);
    }

    else
    {
      count1 += 1;
      Serial.print("<< MyTimer1: OFF : ");
      Serial.println(count1);
    }
  }
  STATE1 = MyTimer1.TRIGGER;
  


  if (MyTimer2.TRIGGER != STATE2)
  {
    if (MyTimer2.TRIGGER == HIGH)
    {
      count2 += 1;
      Serial.print(">> MyTimer2: ON  : ");
      Serial.println(count2);
    }

    else
    {
      count3 += 1;
      Serial.print("<< MyTimer2: OFF : ");
      Serial.println(count3);
    }
  }
  STATE2 = MyTimer2.TRIGGER;



  if (MyTimer3.TRIGGER != STATE3)
  {
    if (MyTimer3.TRIGGER == HIGH)
    {
      count4 += 1;
      Serial.print(">> MyTimer3: ON  : ");
      Serial.println(count4);
    }

    else
    {
      count5 += 1;
      Serial.print("<< MyTimer3: OFF : ");
      Serial.println(count5);
    }
  }
  STATE3 = MyTimer3.TRIGGER;
}



struct DELAY run(long _on, long _off)     //Funktion 
{
  struct DELAY MyTIMER;

  MyTIMER.intervalOn = _on;
  MyTIMER.intervalOff = _off;

  if (MyTIMER.TRIGGER == HIGH && currentMillis - MyTIMER.previousMillis > MyTIMER.intervalOn) // ON
  {
    MyTIMER.previousMillis = currentMillis;
    MyTIMER.TRIGGER = LOW;
  }


  if (MyTIMER.TRIGGER == LOW && currentMillis - MyTIMER.previousMillis > MyTIMER.intervalOff) // OFF
  {
    MyTIMER.previousMillis = currentMillis;
    MyTIMER.TRIGGER = HIGH;
  }

  return MyTIMER;
}

Mach eine richtige Klasse draus mit run() Methode in der Klasse. Statt ein Objekt mit den Daten außen herumzureichen

Und Namen in nur Großbuchstaben verwendet man normaler nur für Konstanten