Code :


#define RELAY1_PIN 2
#define RELAY2_PIN 3
#define RELAY3_PIN 4
#define RELAY4_PIN 5

#include <DHT.h>;

#define DHTPIN 6
#define DHTTYPE DHT22

int chk;
float hum;
float temp;

void setup()
    Serial.println("SENSOR RUNNING");

    pinMode(RELAY1_PIN, OUTPUT);
    pinMode(RELAY2_PIN, OUTPUT);
    pinMode(RELAY3_PIN, OUTPUT);
    pinMode(RELAY4_PIN, OUTPUT);

void loop()
    float h = dht.readHumidity();
    float t = dht.readTemperature();
    if (isnan(h) || isnan(t))
        Serial.println("CONNECTION ERROR");
    Serial.print("Humidity: ");
    Serial.print(" %\t");
    Serial.print("Temperature: ");
    Serial.print(" *C ");
    Serial.println(" ");
        // FOR RELAY
        digitalWrite(RELAY1_PIN, HIGH);
        delay(2*1000); // wait for 2 second ON
        digitalWrite(RELAY2_PIN, HIGH);
        delay(3*1000); // wait for 3 second ON
        digitalWrite(RELAY3_PIN, HIGH);
        delay(4*1000); // wait for 4 second ON
        digitalWrite(RELAY4_PIN, HIGH);
        delay(5*1000); // wait for 5 second ON

        digitalWrite(RELAY1_PIN, LOW);
        digitalWrite(RELAY2_PIN, LOW);
        digitalWrite(RELAY3_PIN, LOW);
        digitalWrite(RELAY4_PIN, LOW);

This code doesn't do what I want. How do I make them independent?

closed as unclear what you're asking by MichaelT, MatsK, Dmitry Grigoryev, Juraj, Glorfindel Jul 15 at 16:38

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

  • 3
    Please post an actual question and not code. What do you want to actually achieve? I and many others will not bother to look at your code until we know what we need to answer. Clearly explaining your problem then asking a specific question will result likely in help. – morbo Jul 8 at 23:24
  • 3
    Indent your code properly. As it is, there's no way it'll compile, and as you should be able to see, it's exceptionally difficult to follow the flow. – stevieb Jul 8 at 23:40
  • 1
    think about your title ... not in sequence requirement makes no sense ... what if in intervals happens to be in sequence ... do you want to prevent relay activation if that happens? – jsotola Jul 8 at 23:42
  • 2
    Although the question is far from clear, it looks like the answer may be for you to learn how to Blink Without Delay. – Edgar Bonet Jul 9 at 11:04

Here is some code that might help you; leaving out the temperature and humidity stuff:

// constants
// number or relays
const int relays = 4;

// pins for the relays
const int relayPins[] = {2, 3, 4, 5};

// relay cycle times
const unsigned long relayTimes[] = {
     5*60*1000, // 5 min
     60*60*1000, // 1 Hour
     2*60*60*1000, // 2 hours
     12*60*60*1000 // 12 hours

// relay on durations
const unsigned long relayOntimes[] = {
    1000, // 1 second
    10*1000, // 10 seconds
    60*1000, // 1 minute
    60*60*1000 // 1 hour

// timing for the relays
// storing the last time
unsigned long relayElapsed[] = {0, 0, 0, 0};

// state of each relay
int relaystate[] = {0, 0, 0, 0};

// helper function to process each relay
void relay_helper(unsigned long t, unsigned long &elapsed, int pin, int &state, unsigned long delta, unsigned long ontime)
  // depending on the state we do different things
  switch (state)
  case 0: // state 0 is off, wait until time has passed
    if ((t-elapsed) > delta)
    {  // then
       state = 1; // set the state to on
       elapsed = t; // reset the time
       digitalWrite(pin, HIGH); // turn on
  case 1: // state 1 is on, wait until time has passed
    if ((t-elapsed) > ontime)
    {  // then
       state = 0; // set the state to off
       elapsed = t; // reset the time
       digitalWrite(pin, LOW); // turn off

void setup()
  unsigned long start = millis();
  for(int i = 0; i < relays; i++) // for each pin
    pinMode(relayPins[i], OUTPUT); // set it to output
    digitalWrite(relayPins[i], LOW); // and turn it off
    relayElapsed[i] = start;

void loop()
  unsigned long tm = millis();
  for(int i = 0; i < relays; i++)
    relay_helper(tm, relayElapsed[i], relayPins[i], relaystate[i], relayTimes[i], relayOnTimes[i]);
  // do other loop stuff

The basic idea is that the only way to do thing simultaneously is to do things in sequence really fast, we can't use delay() because it is a busy loop, nothing else can happen during delay(), so we keep track of the time and when it is time to turn on the relay we turn it on, and when it is time to turn it off we turn if off.

Another option would be to write a class per relay:

class Relay
  Relay(int pin, unsigned long delta, unsigned long ontime);

  process(unsigned long t);

  unsigned long delta;
  unsigned long ontime;
  unsigned long last; // elapsed
  int pin;      

Then one can create an array of these:

Relay[] relays = {
    Relay(2, 5*60000, 1000),
    Relay(3, 60*60000, 10000),
    Relay(4, 2*60*60000, 60000),
    Relay(5, 12*60*60000, 60*60000)

put the code from setup into the constuctor, and the code from the helper into process, and finally call it in a loop in loop.

The first way shown is how one would do it without C++ classes, and the second way is with them!

Not the answer you're looking for? Browse other questions tagged or ask your own question.