Help with 4 way relay code

Hello there I'm looking for some help to get relay 4 to turn on 4 secs after the first 3 have turned. I scoured the internet to try and figure it out but not getting anywhere. Here's the code I'm using.

int relay_1 = 4;
int relay_2 = 7;
int relay_3 = 8;
int relay_4 = 12;

void setup() {
// put your setup code here, to run once:
Serial.begin(9600);

pinMode(relay_1, OUTPUT);
pinMode(relay_2, OUTPUT);
pinMode(relay_3, OUTPUT);
pinMode(relay_4, OUTPUT);

}

void loop() {

digitalWrite(relay_1, HIGH);
digitalWrite(relay_2, HIGH);
digitalWrite(relay_3, HIGH);
digitalWrite(relay_4, HIGH);

Serial.println("All relays ON");

delay(10000);

digitalWrite(relay_1, LOW);
digitalWrite(relay_2, LOW);
digitalWrite(relay_3, LOW);
digitalWrite(relay_4, LOW);

Serial.println("All relays OFF");

delay(10000);
}

Take some time and describe the desired function of the sketch in simple words and this very simple.

How to properly post code using code tags.

int relay_1 = 4;
int relay_2 = 7;
int relay_3 = 8;
int relay_4 = 12;

void setup() {
// put your setup code here, to run once:
Serial.begin(9600);

pinMode(relay_1, OUTPUT);
pinMode(relay_2, OUTPUT);
pinMode(relay_3, OUTPUT);
pinMode(relay_4, OUTPUT);

}

void loop() {

digitalWrite(relay_1, HIGH);
digitalWrite(relay_2, HIGH);
digitalWrite(relay_3, HIGH);
digitalWrite(relay_4, HIGH);

Serial.println("All relays ON");

delay(10000);

digitalWrite(relay_1, LOW);
digitalWrite(relay_2, LOW);
digitalWrite(relay_3, LOW);
digitalWrite(relay_4, LOW);

Serial.println("All relays OFF");

delay(10000);
}

You are welcome.

int relay_1 = 4;
int relay_2 = 7;
int relay_3 = 8;
int relay_4 = 12;

void setup() {
// put your setup code here, to run once:
Serial.begin(9600);

pinMode(relay_1, OUTPUT);
pinMode(relay_2, OUTPUT);
pinMode(relay_3, OUTPUT);
pinMode(relay_4, OUTPUT);

}

void loop() {

digitalWrite(relay_1, HIGH);
digitalWrite(relay_2, HIGH);
digitalWrite(relay_3, HIGH);

delay(4000); <<<<<<<<<<<<<<<<<<<<<<<<<<<<add a 4 second delay
digitalWrite(relay_4, HIGH);

Serial.println("All relays ON");

delay(10000); might want to change this to 6000

digitalWrite(relay_1, LOW);
digitalWrite(relay_2, LOW);
digitalWrite(relay_3, LOW);
digitalWrite(relay_4, LOW);

Serial.println("All relays OFF");

delay(10000);
}

add a delay before turning on relay # 4

Thank you! I've tried adding a delay like this but it turns off the other relays. I want the first 3 to turn on, stay on and then relay 4 to turn on 4 secs later and then all turn off together. So relay 1,2,3 on, for secs later relay 4 turns on. 10 secs in total 5hen all turn off. I'm new to this if you hadn't guessed. But thanks for replying so quickly

Show us your attempt.

What´s the task of sketch?

Try this:

#include <Arduino.h>

#define NUM_RELAYS 4
#define INTERVAL 5 // Time in seconds
#define DELAY_INTERVAL 4 // Time in seconds
const byte relayPin[NUM_RELAYS] = {4, 7, 8, 12};

bool currentStatus[NUM_RELAYS] = {false};
unsigned long prevMillis = 0;
unsigned long delayMillis = 0;

byte relaysChanged = 0;

void setup()
{
  Serial.begin(9600);

  for (byte i = 0; i < NUM_RELAYS; i++)
  {
    pinMode(relayPin[i], OUTPUT);

    digitalWrite(relayPin[i], currentStatus[i]);
    Serial.print("Relay ");
    Serial.print(i);
    Serial.println(currentStatus[i] ? ": ON" : ": OFF");
  }
  Serial.println();
  prevMillis = millis();
}

void loop()
{
  if ((millis() - prevMillis) >= (INTERVAL * 1000UL))
  {
    for (byte i = 0; i < NUM_RELAYS; i++)
    {
      if (i != (NUM_RELAYS - 1))
      {
        currentStatus[i] = !currentStatus[i];
        digitalWrite(relayPin[i], currentStatus[i]);
        if (currentStatus[i] == true)
        {
          relaysChanged++;
        }
        Serial.print("Relay ");
        Serial.print(i);
        Serial.println(currentStatus[i] ? ": ON" : ": OFF");
      }
    }

    if (relaysChanged == 0)
    {
      currentStatus[NUM_RELAYS - 1] = false;
      digitalWrite(relayPin[NUM_RELAYS - 1], currentStatus[NUM_RELAYS - 1]);

      Serial.print("Relay ");
      Serial.print(NUM_RELAYS - 1);
      Serial.println(": OFF\n");
    }
    prevMillis = millis();
    delayMillis = millis();
  }

  if (relaysChanged == (NUM_RELAYS - 1))
  {
    if ((millis() - delayMillis) >= (DELAY_INTERVAL * 1000UL))
    {
      currentStatus[NUM_RELAYS - 1] = true;
      digitalWrite(relayPin[NUM_RELAYS - 1], currentStatus[NUM_RELAYS - 1]);

      Serial.print("Relay ");
      Serial.print(NUM_RELAYS - 1);
      Serial.println(": ON\n");
      relaysChanged = 0;
    }
  }
}

2 Likes

Hi @blackheart69,

there seems to be something wrong with your hardware if the three relays switch off during a delay.

As @LarryD already mentioned a description of your hardware setup would be more than helpful.

Whether delays() are a reasonable solution to your task can only be evaluated if you would describe the objective of your application ...

Regards
ec2021

P.S.: @FernandoGarcia has already posted a generally better solution ... But if the relays change state during a delay there seems to be some other trouble ...

1 Like

Thank you that's great I will try it next week when back at work and we share the update with you.thanks again

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.