user85975: So far, a part of this code works fine, but Relay 1 keeps turning on and off instead of staying off.
My purpose is to turn Relay 7 on, wait 3s, turn Relay 8 on, wait 10s, turn Relay 6 on, wait 10s, turn Relay 7 off, wait 10s, turn Relay 5 on, wait 3s, turn Relay 6 off, wait 10s, turn Relay5 on, wait 3s, turn Relay 6 off, wait 10s, turn Relay4 on, wait 3s, turn Relay 5 off, wait 10s, turn Relay 3 on, wait 3s, turn Relay 4 off, wait 10s, turn Relay 2 on, wait 3s, turn Relay 3 off, wait 10s, turn Relay 1 on, wait 10s, turn Relay 1 off, wait 3s, turn Relay 8 off, without looping, only one sequence.
I need some help.
int RelayPompe = 2;
int RelayVanne7 = 3;
int RelayVanne6 = 4;
int RelayVanne5 = 5;
int RelayVanne4 = 6;
int RelayVanne3 = 7;
int RelayVanne2 = 8;
int RelayVanne1 = 9;

const unsigned long eventInterval1 = 10000;
const unsigned long eventInterval2 = 3000;
const unsigned long eventInterval3 = 15000;
const unsigned long eventInterval4 = 20000;
unsigned long previousTime1 = 0;
unsigned long previousTime2 = 0;
unsigned long previousTime3 = 0;
unsigned long previousTime4 = 0;

void setup() {
  Serial.begin(9600);
  pinMode(RelayPompe, OUTPUT);
  pinMode(RelayVanne1, OUTPUT);
  pinMode(RelayVanne2, OUTPUT);
  pinMode(RelayVanne3, OUTPUT);
  pinMode(RelayVanne4, OUTPUT);
  pinMode(RelayVanne5, OUTPUT);
  pinMode(RelayVanne6, OUTPUT);
  pinMode(RelayVanne7, OUTPUT);
  digitalWrite(RelayPompe, HIGH);
  digitalWrite(RelayVanne1, HIGH);
  digitalWrite(RelayVanne2, HIGH);
  digitalWrite(RelayVanne3, HIGH);
  digitalWrite(RelayVanne4, HIGH);
  digitalWrite(RelayVanne5, HIGH);
  digitalWrite(RelayVanne6, HIGH);
  digitalWrite(RelayVanne7, HIGH);
}

void loop() {
  unsigned long currentTime = millis();
  if (currentTime - previousTime1 >= eventInterval1) {
    digitalWrite(RelayPompe, LOW);
    previousTime1 = currentTime;
  }
  if (currentTime - previousTime2 >= eventInterval2) {
    digitalWrite(RelayVanne1, LOW);
    previousTime2 = currentTime;
  }
  if (currentTime - previousTime3 >= eventInterval3) {
    digitalWrite(RelayVanne2, LOW);
    previousTime3 = currentTime;
  }
  if (currentTime - previousTime4 >= eventInterval4) {
    digitalWrite(RelayVanne1, HIGH);
    previousTime4 = currentTime;
  }
}


user4143: For this kind of thing I'd be inclined to create an array (maybe of structs) describing what you want the relays to do, then write a program that steps through that array executing each "instruction" in sequence. That way it separates out what you want to do from how you want it to be done (if that makes sense).
So you could make a simple struct:
typedef struct {
    uint8_t relays;
    uint32_t postDelay;
} RelayState;

Then you can make an array using that struct to describe your relay states. Using a bitmap for your relays (bit 0 is relay 1, bit 1 is relay 2, etc) means you can pack everything into one integer and describe the relays in binary notation:
RelayState states[] = {
    { 0b01000000,  3000 }, // Relay 7 on, wait 3 seconds
    { 0b11000000, 10000 }, // Relay 8 on, wait 10 seconds
    { 0b11100000, 10000 }, // Relay 6 on, wait 10 seconds
    { 0b10100000, 10000 }, // Relay 7 off, wait 10 seconds
    { 0b10110000,  3000 }, // Relay 5 on, wait 3 seconds
    // ... etc ...
    { 0, 0 } // Marker for the end of the array
};

If ever you want to change the sequence you just change the contents of that array with no need to mess with the rest of your program.
It can also be useful to create an array with your relay pins in it:
const uint8_t relayPins[8] = { 9, 8, 7, 6, 5, 4, 3, 2 };

Now you just need to write a small program that only has to deal with the current entry in the array - set the relays according to the first part then delay according to the second part, and step to the next entry.  Something that could look like:
void setup() {
    // Set the relay pins to outputs
    for (int i = 0; i < 8; i++) {
        pinMode(relayPins[i], OUTPUT);
    }

    // Set the initial state from the first entry in the array
    setRelays(states[0].relays);
}

void loop() {
    static uint32_t ts = millis(); // Storage for the time
    static uint8_t pos = 0; 

    if ((millis() - ts) >= states[pos].postDelay) { // Our time is up
        ts = millis(); // Record the time
        pos++; // Move to the next entry
        if ((states[pos].relays == 0) && (states[pos].postDelay == 0)) { // Last entry marker
            pos = 0; // Start from the beginning again
        }
        setRelays(states[pos].relays);
    }
}

// Small helper function to set the relays according to a bitmap
void setRelays(uint8_t bm) {
    for (int i = 0; i < 8; i++) {
        digitalWrite(relayPins[i], bm & (1 << i));
    }
}

Note: this code has not been tested, just written "off the cuff", and may not even compile. But regardless, it should give you a rough idea of the methodology.

The format of the bitmaps is very simple. They're just a binary representation of a number where each bit is one relay. Turn that bit on and the corresponding relay turns on.

Relay 1: 0b00000001
Relay 2: 0b00000010
Relay 3: 0b00000100

etc.  Just set a 1 for a relay to be on and a 0 for a relay to be off at that point in time.
So turn relay 1 on is 0b00000001, then turn on relay 4 at the same time is 0b00001001, and then turn relay 1 off again is 0b00001000 (relay 4 is still on).

85975: <p>So far, a part of this code works fine, but Relay 1 keeps turning on and off instead of staying off.</p>
<p>My purpose is to turn Relay 7 on, wait 3s, turn Relay 8 on, wait 10s, turn Relay 6 on, wait 10s, turn Relay 7 off, wait 10s, turn Relay 5 on, wait 3s, turn Relay 6 off, wait 10s, turn Relay5 on, wait 3s, turn Relay 6 off, wait 10s, turn Relay4 on, wait 3s, turn Relay 5 off, wait 10s, turn Relay 3 on, wait 3s, turn Relay 4 off, wait 10s, turn Relay 2 on, wait 3s, turn Relay 3 off, wait 10s, turn Relay 1 on, wait 10s, turn Relay 1 off, wait 3s, turn Relay 8 off, without looping, only one sequence.</p>
<p>I need some help.</p>
<pre class="lang-cpp prettyprint-override"><code>int RelayPompe = 2;
int RelayVanne7 = 3;
int RelayVanne6 = 4;
int RelayVanne5 = 5;
int RelayVanne4 = 6;
int RelayVanne3 = 7;
int RelayVanne2 = 8;
int RelayVanne1 = 9;

const unsigned long eventInterval1 = 10000;
const unsigned long eventInterval2 = 3000;
const unsigned long eventInterval3 = 15000;
const unsigned long eventInterval4 = 20000;
unsigned long previousTime1 = 0;
unsigned long previousTime2 = 0;
unsigned long previousTime3 = 0;
unsigned long previousTime4 = 0;

void setup() {
  Serial.begin(9600);
  pinMode(RelayPompe, OUTPUT);
  pinMode(RelayVanne1, OUTPUT);
  pinMode(RelayVanne2, OUTPUT);
  pinMode(RelayVanne3, OUTPUT);
  pinMode(RelayVanne4, OUTPUT);
  pinMode(RelayVanne5, OUTPUT);
  pinMode(RelayVanne6, OUTPUT);
  pinMode(RelayVanne7, OUTPUT);
  digitalWrite(RelayPompe, HIGH);
  digitalWrite(RelayVanne1, HIGH);
  digitalWrite(RelayVanne2, HIGH);
  digitalWrite(RelayVanne3, HIGH);
  digitalWrite(RelayVanne4, HIGH);
  digitalWrite(RelayVanne5, HIGH);
  digitalWrite(RelayVanne6, HIGH);
  digitalWrite(RelayVanne7, HIGH);
}

void loop() {
  unsigned long currentTime = millis();
  if (currentTime - previousTime1 &gt;= eventInterval1) {
    digitalWrite(RelayPompe, LOW);
    previousTime1 = currentTime;
  }
  if (currentTime - previousTime2 &gt;= eventInterval2) {
    digitalWrite(RelayVanne1, LOW);
    previousTime2 = currentTime;
  }
  if (currentTime - previousTime3 &gt;= eventInterval3) {
    digitalWrite(RelayVanne2, LOW);
    previousTime3 = currentTime;
  }
  if (currentTime - previousTime4 &gt;= eventInterval4) {
    digitalWrite(RelayVanne1, HIGH);
    previousTime4 = currentTime;
  }
}
</code></pre>
 4143: <p>For this kind of thing I'd be inclined to create an array (maybe of structs) describing what you want the relays to do, then write a program that steps through that array executing each &quot;instruction&quot; in sequence. That way it separates out what you want to do from how you want it to be done (if that makes sense).</p>
<p>So you could make a simple struct:</p>
<pre class="lang-cpp prettyprint-override"><code>typedef struct {
    uint8_t relays;
    uint32_t postDelay;
} RelayState;
</code></pre>
<p>Then you can make an array using that struct to describe your relay states. Using a bitmap for your relays (bit 0 is relay 1, bit 1 is relay 2, etc) means you can pack everything into one integer and describe the relays in binary notation:</p>
<pre class="lang-cpp prettyprint-override"><code>RelayState states[] = {
    { 0b01000000,  3000 }, // Relay 7 on, wait 3 seconds
    { 0b11000000, 10000 }, // Relay 8 on, wait 10 seconds
    { 0b11100000, 10000 }, // Relay 6 on, wait 10 seconds
    { 0b10100000, 10000 }, // Relay 7 off, wait 10 seconds
    { 0b10110000,  3000 }, // Relay 5 on, wait 3 seconds
    // ... etc ...
    { 0, 0 } // Marker for the end of the array
};
</code></pre>
<p>If ever you want to change the sequence you just change the contents of that array with no need to mess with the rest of your program.</p>
<p>It can also be useful to create an array with your relay pins in it:</p>
<pre class="lang-cpp prettyprint-override"><code>const uint8_t relayPins[8] = { 9, 8, 7, 6, 5, 4, 3, 2 };
</code></pre>
<p>Now you just need to write a small program that only has to deal with the current entry in the array - set the relays according to the first part then delay according to the second part, and step to the next entry.  Something that could look like:</p>
<pre class="lang-cpp prettyprint-override"><code>void setup() {
    // Set the relay pins to outputs
    for (int i = 0; i &lt; 8; i++) {
        pinMode(relayPins[i], OUTPUT);
    }

    // Set the initial state from the first entry in the array
    setRelays(states[0].relays);
}

void loop() {
    static uint32_t ts = millis(); // Storage for the time
    static uint8_t pos = 0; 

    if ((millis() - ts) &gt;= states[pos].postDelay) { // Our time is up
        ts = millis(); // Record the time
        pos++; // Move to the next entry
        if ((states[pos].relays == 0) &amp;&amp; (states[pos].postDelay == 0)) { // Last entry marker
            pos = 0; // Start from the beginning again
        }
        setRelays(states[pos].relays);
    }
}

// Small helper function to set the relays according to a bitmap
void setRelays(uint8_t bm) {
    for (int i = 0; i &lt; 8; i++) {
        digitalWrite(relayPins[i], bm &amp; (1 &lt;&lt; i));
    }
}
</code></pre>
<p>Note: this code has not been tested, just written &quot;off the cuff&quot;, and may not even compile. But regardless, it should give you a rough idea of the methodology.</p>
<hr />
<p>The format of the bitmaps is very simple. They're just a binary representation of a number where each bit is one relay. Turn that bit on and the corresponding relay turns on.</p>
<ul>
<li>Relay 1: 0b00000001</li>
<li>Relay 2: 0b00000010</li>
<li>Relay 3: 0b00000100</li>
</ul>
<p>etc.  Just set a 1 for a relay to be on and a 0 for a relay to be off at that point in time.</p>
<p>So turn relay 1 on is 0b00000001, then turn on relay 4 at the same time is 0b00001001, and then turn relay 1 off again is 0b00001000 (relay 4 is still on).</p>
