8 Relay Module timing issue

Hi,
I'm new to working with Arduino, I'm working on a project that basically fires 8 relays at specific times to activate and cancel a 3rd party system in our office. I have based this on the "Several things at the same time" code. It works perfectly providing that I keep my interval times below 30000, as soon as I go over this relay then stops working. Ideally I'd want these times to range from 2 minutes to 10 minutes.
Edit
I'm using a Mega 2560 but plan to run this on a NANO once I can get the code working.

Any advice or anyone that can point me in the right direction where I've went wrong would be appreciated.

Full Code below, (relayTwo - does not work in this version of the sketch)

//Outputs
const int relayOne = 4;
const int relayTwo = 5;
const int relayThree = 6;
const int relayFour = 7;
const int relayFive = 8;
const int relaySix = 9;
const int relaySeven = 10;
const int relayEight = 11;

//Timers
const int intervalOne = 10000;
const int intervalTwo = 31000;
const int intervalThree = 29000;
const int intervalFour = 19000;
const int intervalFive = 28500;
const int intervalSix = 26000;
const int intervalSeven = 18000;
const int intervalEight = 24000;

const int relayDuration = 500;

//States
byte relayOneState = HIGH;
byte relayTwoState = HIGH;
byte relayThreeState = HIGH;
byte relayFourState = HIGH;
byte relayFiveState = HIGH;
byte relaySixState = HIGH;
byte relaySevenState = HIGH;
byte relayEightState = HIGH;

//Timers
unsigned long currentMillis = 0;
unsigned long previousRelayOneMillis = 0;
unsigned long previousRelayTwoMillis = 0;
unsigned long previousRelayThreeMillis = 0;
unsigned long previousRelayFourMillis = 0;
unsigned long previousRelayFiveMillis = 0;
unsigned long previousRelaySixMillis = 0;
unsigned long previousRelaySevenMillis = 0;
unsigned long previousRelayEightMillis = 0;

void setup() {
  // put your setup code here, to run once:
Serial.begin(9600);
Serial.println("Relay Modules Start:::");
  
pinMode(relayOne, OUTPUT);
pinMode(relayTwo, OUTPUT);
pinMode(relayThree, OUTPUT);
pinMode(relayFour, OUTPUT);
pinMode(relayFive, OUTPUT);
pinMode(relaySix, OUTPUT);
pinMode(relaySeven, OUTPUT);
pinMode(relayEight, OUTPUT);

}

void loop() {
  // put your main code here, to run repeatedly:
currentMillis = millis();


updateRelayOne();
updateRelayTwo();
updateRelayThree();
updateRelayFour();
updateRelayFive();
updateRelaySix();
updateRelaySeven();
updateRelayEight();
switchRelays();

}

//===========================

void updateRelayOne (){
  if (relayOneState == HIGH) {
    if (currentMillis - previousRelayOneMillis >= intervalOne) {
      relayOneState = LOW;
      previousRelayOneMillis += intervalOne;
      Serial.println("Call Point 1 --- Call");
    }
  }
  else {
    if (currentMillis - previousRelayOneMillis >= relayDuration) {
      relayOneState = HIGH;
      previousRelayOneMillis += relayDuration;
    }
  }  
}

//===========================

void updateRelayTwo (){
if (relayTwoState == HIGH) {
    if (currentMillis - previousRelayTwoMillis >= intervalTwo) {
      relayTwoState = LOW;
      previousRelayTwoMillis += intervalTwo;
      Serial.println("Call Point 1 --- Reset");
    }
  }
  else {
    if (currentMillis - previousRelayTwoMillis >= relayDuration) {
      relayTwoState = HIGH;
      previousRelayTwoMillis += relayDuration;
    }
  }
}


//===========================

void updateRelayThree (){
if (relayThreeState == HIGH) {
    if (currentMillis - previousRelayThreeMillis >= intervalThree) {
      relayThreeState = LOW;
      previousRelayThreeMillis += intervalThree;
      Serial.println("Call Point 2 --- Call");
    }
  }
  else {
    if (currentMillis - previousRelayThreeMillis >= relayDuration) {
      relayThreeState = HIGH;
      previousRelayThreeMillis += relayDuration;
    }
  }    
}


//===========================

void updateRelayFour (){
if (relayFourState == HIGH) {
    if (currentMillis - previousRelayFourMillis >= intervalFour) {
      relayFourState = LOW;
      previousRelayFourMillis += intervalFour;
      Serial.println("Call Point 2 --- Reset");
    }
  }
  else {
    if (currentMillis - previousRelayFourMillis >= relayDuration) {
      relayFourState = HIGH;
      previousRelayFourMillis += relayDuration;
    }
  }      
}


//===========================

void updateRelayFive (){
  if (relayFiveState == HIGH) {
    if (currentMillis - previousRelayFiveMillis >= intervalOne) {
      relayOneState = LOW;
      previousRelayFiveMillis += intervalOne;
      Serial.println("Call Point 3 --- Call");
    }
  }
  else {
    if (currentMillis - previousRelayFiveMillis >= relayDuration) {
      relayFiveState = HIGH;
      previousRelayFiveMillis += relayDuration;
    }
  }    
}

//===========================

void updateRelaySix (){
if (relaySixState == HIGH) {
    if (currentMillis - previousRelaySixMillis >= intervalSix) {
      relaySixState = LOW;
      previousRelaySixMillis += intervalSix;
      Serial.println("Call Point 3 --- Reset");
    }
  }
  else {
    if (currentMillis - previousRelaySixMillis >= relayDuration) {
      relaySixState = HIGH;
      previousRelaySixMillis += relayDuration;
    }
  }      
}


//===========================

void updateRelaySeven (){
if (relaySevenState == HIGH) {
    if (currentMillis - previousRelaySevenMillis >= intervalSeven) {
      relaySevenState = LOW;
      previousRelaySevenMillis += intervalSeven;
      Serial.println("Call Point 4 --- Call");
    }
  }
  else {
    if (currentMillis - previousRelaySevenMillis >= relayDuration) {
      relaySevenState = HIGH;
      previousRelaySevenMillis += relayDuration;
    }
  }    
}


//===========================

void updateRelayEight (){
if (relayEightState == HIGH) {
    if (currentMillis - previousRelayEightMillis >= intervalEight) {
      relayEightState = LOW;
      previousRelayEightMillis += intervalEight;
      Serial.println("Call Point 4 --- Reset");
    }
  }
  else {
    if (currentMillis - previousRelayEightMillis >= relayDuration) {
      relayEightState = HIGH;
      previousRelayEightMillis += relayDuration;
    }
  }      
}

//===========================

void switchRelays(){
  digitalWrite(relayOne, relayOneState);
  digitalWrite(relayTwo, relayTwoState);
  digitalWrite(relayThree, relayThreeState);
  digitalWrite(relayFour, relayFourState);
  digitalWrite(relayFive, relayFiveState);
  digitalWrite(relaySix, relaySixState);
  digitalWrite(relaySeven, relaySevenState);
  digitalWrite(relayEight, relayEightState);
}

Cheers
M

Your topic has been moved here as this forum section is more appropriate than where it was originally posted.

A signed int varaible, as used for your interval variable, can only hold a positive value up to 32,767 on a 328 chip

See int - Arduino Reference

Start by changing them to unsigned int to allow a value up to 65,535

Overall, I think you’ll gain a lot if you study structs and arrays.
They will allow you to manage the relays in a much more intuitive (and shorter) program.

Looking good, now to develop your skills.

Thanks for the quick replies, I'll change that tonight and see how it goes. I suppose I'd best start looking at a DUE if I'm wanting longer times.

M

No need to use a DUE for longer times, use unsigned longs for all of the timing variables and you can time up to 49 days on a Nano

Is that long enough :grinning:

Hello
I have quickly "soldered" a sketch from my sketch box. I have summarized your information in a struct{}. What is missing now for the correct initialization are the pin numbers, and the respective switching times for the leds and the output text.
If you have any questions, please let me know.

#define ProjectName "8 Relay Module timing issue"
const int heartBeat {
  LED_BUILTIN
};
struct RELAISTIMINGTEXT {
  byte pin;
  byte state;
  unsigned long stamp;
  unsigned long duration;
  unsigned long durationLedOn;
  unsigned long durationLedOff;
  String text;
} relais[] {
  { 2, 1, 0, 5000, 5000, 1000, "test"},
};
void processing() {
  unsigned long now = millis();
  for (auto &Relais : relais) {
    if (now - Relais.stamp >= Relais.duration) {
      Relais.stamp = now;
      if (digitalRead(Relais.pin)) {
        Relais.state = LOW;
        Relais.duration = Relais.durationLedOff;
        Serial.println(Relais.text);
      } else {
        Relais.state = HIGH;
        Relais.duration = Relais.durationLedOn;
      }
    }
  }
}
void output() {
  for (auto &Relais : relais) digitalWrite(Relais.pin, Relais.state);
}
void setup() {  // put your setup code here, to run once:
  Serial.begin(9600);
  pinMode (heartBeat, OUTPUT);
  Serial.print(F(".\nlet´s go! ")); 
  for (auto &Relais : relais) pinMode(Relais.pin, OUTPUT), digitalWrite(Relais.pin, Relais.state);
}
void loop() { // put your main code here, to run repeatedly:
  digitalWrite(heartBeat, millis() / 500 % 2);
  processing();
  output();
}

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