Combinations of relay having delay

Hello All
I am facing a problem while working on Relay project, I want to operate 2 relay when i press the button. 2 relays having 50msec delay in it. can anybody solve this. I am using this following code. I am using 8 relay module, all relays are working fine, but when they are in the combinations they didn't work.

i want to combinations like this (S=switch R= Relay)
S1 - R1 (50ms delay) R2
S2 - R2 (50ms delay) R3
S3 - R1 (50ms delay) R3
S4 - R5 (50ms delay) R6
S5 - R2 (50ms delay) R4


int Relay1 = 7;
int Relay2 = 8;
int Relay3 = 9;
int Relay4 = 10;
int Relay5 = 11;
int Relay6 = 12;

int Button1 = 2;
int BV1 = 0;

int Button2 = 3;
int BV2 = 0;

int Button3 = 4;
int BV3 = 0;

int Button4 = 5;
int BV4 = 0;

int Button5 = 6;
int BV5 = 0;


void setup() 
{  
pinMode(Relay1,OUTPUT);
pinMode(Relay2,OUTPUT);
pinMode(Relay3,OUTPUT);
pinMode(Relay4,OUTPUT);
pinMode(Relay5,OUTPUT);
pinMode(Relay6,OUTPUT);

pinMode(Button1,INPUT_PULLUP);
pinMode(Button2,INPUT_PULLUP);
pinMode(Button3,INPUT_PULLUP);
pinMode(Button4,INPUT_PULLUP);
pinMode(Button5,INPUT_PULLUP);

}
void loop() 
{
  BV1 = digitalRead(Button1);
  BV2 = digitalRead(Button2);
  BV3 = digitalRead(Button3);
  BV4 = digitalRead(Button4);
  BV5 = digitalRead(Button5);


if(BV1 == LOW)
{
  digitalWrite(Relay1,LOW);
  delay(50);
  digitalWrite(Relay2,LOW); 
}

else 
{
  digitalWrite(Relay1,HIGH);
  digitalWrite(Relay2,HIGH); 
}


if(BV2 == LOW)
{
  digitalWrite(Relay2,LOW);
  delay(50);
  digitalWrite(Relay3,LOW);
}
else
{
  digitalWrite(Relay2,HIGH); 
  digitalWrite(Relay3,HIGH);
}

if(BV3 == LOW)
{
  digitalWrite(Relay1,LOW);
  delay(50);
  digitalWrite(Relay3,LOW); 
}

else 
{
  digitalWrite(Relay1,HIGH);
  digitalWrite(Relay3,HIGH); 
}

if(BV4 == LOW)
{
  digitalWrite(Relay5,LOW);
  delay(50);
  digitalWrite(Relay6,LOW);
}

else 
{
  digitalWrite(Relay5,HIGH);
  digitalWrite(Relay6,HIGH); 
}

if(BV5 == LOW)
{
  digitalWrite(Relay2,LOW);
  delay(50);
  digitalWrite(Relay4,LOW);
}

else 
{
  digitalWrite(Relay2,HIGH);
  digitalWrite(Relay4,HIGH); 
}
}

delay() does exactly what it says. It stops everything until the delay is finished.
That makes button presses and other relays slow/unresponsive.
Learn to code without delay() calls. Use millis) timing instead.
Study the BlinkWithoutDelay sketch, found in the examples of the IDE.
Leo..

OK sir...Thank You

Just 1 switch?

As a favor
Go yo your post.
Hit the pencil at the bottom.
Then highlight all your code.
Then hit the </>

more than a favour, a requirement if OP is respectful of the forum...

@rushi534 please read How to get the best out of this forum

Hello,
you may try this quick and home brewed solution but you have to do some mods wrt to you needs.

//BLOCK COMMENT
//https://forum.arduino.cc/t/combinations-of-relay-having-delay/890612/2
#define ProjectName "Combinations of relay having delay"
// CONSTANT DEFINITION
// you may need to change these constants to your hardware
const byte Input_[] {A0};
const byte Output_[] {2, 3};
enum {One, Two};
// VARIABLE DECLARATION
enum {Pressed, Released};
struct BLOCK {
  byte buttonPin;
  bool buttonState;
  byte relay1Pin;
  byte relay2Pin;
  unsigned long stamp;
  unsigned long duration;
  bool timerState;
} blocks[] {
  {Input_[One], 1, Output_[One], Output_[Two], 0, 1000, 0},
};
// FUNCTIONS
void setup() {
  Serial.begin(9600);
  Serial.println(F("."));
  Serial.print(F("File   : ")), Serial.println(__FILE__);
  Serial.print(F("Date   : ")), Serial.println(__DATE__);
  Serial.print(F("Project: ")), Serial.println(ProjectName);
  pinMode (LED_BUILTIN, OUTPUT);
  for (auto Input : Input_) pinMode(Input, INPUT_PULLUP);
  for (auto Output : Output_) pinMode(Output, OUTPUT);
  // check outputs
  for (auto Output : Output_) digitalWrite(Output, HIGH);
  delay(1000);
  for (auto Output : Output_) digitalWrite(Output, LOW);
}
void loop () {
  unsigned long currentTime = millis();
  digitalWrite(LED_BUILTIN, (currentTime / 500) % 2);
  static unsigned long myScan;
  if (currentTime - myScan >= 20) {
    for (auto &block : blocks) {
      bool stateNew = digitalRead(block.buttonPin);
      if (block.buttonState != stateNew) {
        block.buttonState = stateNew;
        switch (stateNew) {
          case Pressed:
            digitalWrite(block.relay1Pin, HIGH);
            block.stamp = currentTime;
            block.timerState = true;
            break;
          case Released:
            digitalWrite(block.relay1Pin, LOW);
            digitalWrite(block.relay2Pin, LOW);
            break;
        }
      }
    }
  }
  for (auto &block : blocks) {
    if (currentTime - block.stamp > block.duration && block.timerState) {
      block.timerState = false;
      digitalWrite(block.relay2Pin, HIGH);
    }
  }
}

Have fun and enjoy the weekend

Thank You sir. I am changing the code as per requirement it doesn't work, my humble request is will you write code for me that fulfill my requirements.

It does something. What does "it didn't work" mean?

Think what happens when BV1 is LOW? You will activate two relays. Next you test BV2 and it's HIGH so you switch your relays off again.

On top of that, loop() loops so the next time that BV1 is checked it might be HIGH and both relays will be off again. Or it is still LOW and you will activate the first relay again.

What needs to happen? Do the relays have to stay in the last state? Till the button is released again? If so, look at the state change detection example that comes with the IDE; you're not interested in the fact that the button is pressed but when it becomes pressed. Same for the release.

When i hold the button 1,1st relay gets energized and then after 50ms 2nd relay gets energized as soon as i lift off my finger both relay should get de-energized at the same time.
when i press the button 3 and 4, the 3rd relay didn't gets energized but 4th relay gets energized. My power supply is proper in connection. I am using millis function,its not working please anybody correct it.

int Relay1 = 2;
int Relay2 = 3;
const unsigned int interval = 50;
unsigned long previousTime = 0;

int Button1 = 7;
int BV1 = 0;

void setup() 
{  
pinMode(Relay1,OUTPUT);
pinMode(Relay2,OUTPUT);

pinMode(Button1,INPUT_PULLUP);
}

void loop() 
{
  unsigned long currentTime = millis();
  
  BV1 = digitalRead(Button1);
  


if(BV1 == LOW)
{
  digitalWrite(Relay1,LOW);
  if(currentTime - previousTime >= interval) 
{
  digitalWrite(Relay2,LOW);
}
   previousTime = currentTime;
}

else 
{
  digitalWrite(Relay1,HIGH);
  digitalWrite(Relay2,HIGH); 
}
}

you stated clearly 'switch' and not 'button'

"i want to combinations like this (S=switch R= Relay)
S1 - R1 (50ms delay) R2
S2 - R2 (50ms delay) R3
S3 - R1 (50ms delay) R3
S4 - R5 (50ms delay) R6"

two totally different operations and are handled totally differently in code.

you did later call them buttons. so, there was some confusion.
this just points out how a single word can alter the entire understanding of a project.

Look at the 'button' example in your IDE and also 'debounce'

learn how to toggle a flag in software when a button press occurs.

it is a noob mistake, and one you will learn and grow from, as have us all.

Button1 = digitalRead(Button1);
if(Button1 == LOW)
{
BV1 = !BV1 ; // toggles BV1 to the opposite state with each button press
// WARNING, your must release button before the loop re-scans
// delay(100) is required and your button press has to be less than 100mS
}

if(BV1 == LOW)
{
digitalWrite(Relay1,LOW);
if(currentTime - previousTime >= interval)
{
digitalWrite(Relay2,LOW);
}

change only the digital read and add a new if()

and if you find the button has ANY bounce, you can slow the whole sketch down, or add debounce.

if you slow the whole sketch down you only need to cover the debounce problem.

delay(100) ; // put at the end of loop() and your loop will only cycle 10 times a second and not thousands of times.

although we look down on delay() there are times when it works well for the application, and when it is the correct thing to use.

if you want, you can test your reflexes
write a sketch that counts how many times a button makes contact

and you can write a sketch that shows how long the button was depressed. that way you can see how long you hold it down when you press.

Hi, @rushi534
Welcome to the forum.

What is the application that requires relays to operate with less than 50mS precision?
What relays are you using?
How are you measuring the 50mS delays?

Thanks.. Tom.. :smiley: :+1: :coffee: :australia:

here is an example using debounce as the main format.

as you can see the practice of separating the sketch into 3 parts,
gather data / analyze and perform logic / output
have been used.

/*     Debounce
  Each time the input pin goes from LOW to HIGH (e.g. because of a push-button
  press), the output pin is toggled from LOW to HIGH or HIGH to LOW. There's a
  minimum delay between toggles to debounce the circuit (i.e. to ignore noise).
  http://www.arduino.cc/en/Tutorial/Debounce
*/

int Relay1 = 2;
int Relay2 = 3;
const unsigned int interval = 50;
unsigned long previousTime = 0;

// constants won't change. They're used here to set pin numbers:
const int Button1 = 7;
//  const int buttonPin = 2;    // the number of the pushbutton pin
const int ledPin = 13;      // the number of the LED pin

// Variables will change:
int ledState = HIGH;         // the current state of the output pin
int buttonState;             // the current reading from the input pin
int lastButtonState = LOW;   // the previous reading from the input pin

// the following variables are unsigned longs because the time, measured in
// milliseconds, will quickly become a bigger number than can be stored in an int.
unsigned long lastDebounceTime = 0;  // the last time the output pin was toggled
unsigned long debounceDelay = 50;    // the debounce time; increase if the output flickers

void setup() {
  pinMode(Button1, INPUT);
  pinMode(ledPin, OUTPUT);

  // set initial LED state
  digitalWrite(ledPin, ledState);
}

unsigned long currentTime ;
// int lastButtonState ;

void loop() {
  // read the state of the switch into a local variable:
  int reading = digitalRead(Button1);

  // check to see if you just pressed the button
  // (i.e. the input went from LOW to HIGH), and you've waited long enough
  // since the last press to ignore any noise:

  // If the switch changed, due to noise or pressing:
  if (reading != lastButtonState) {
    // reset the debouncing timer
    lastDebounceTime = millis();
  }

  if ((millis() - lastDebounceTime) > debounceDelay) {
    // whatever the reading is at, it's been there for longer than the debounce
    // delay, so take it as the actual current state:

    // if the button state has changed:
    if (reading != buttonState) {
      buttonState = reading;

      // only toggle the LED if the new button state is HIGH
      // this only occurs while the button is being pressed
      if (buttonState == HIGH) {
        ledState = !ledState;
      }
    }
  } // end if debounce

  // now the values get passed to the output

  // set the LED:
  digitalWrite(ledPin, ledState);

  if ( ledState == HIGH ) {

    //  if(BV1 == LOW)  from the OP's sketch
    {
      digitalWrite(Relay1, LOW);
      if (currentTime - previousTime >= interval)
      {
        digitalWrite(Relay2, LOW);
      }
      previousTime = currentTime;
    }
  }
  else
  {
    digitalWrite(Relay1, HIGH);
    digitalWrite(Relay2, HIGH);
  }

// this is housekeeping to give the next loop a reference
// save the reading. Next time through the loop, it'll be the lastButtonState:
lastButtonState = reading;
}

I think this will do what you want. When a button is pressed, turn on a pair of relays 50 milliseconds apart and keep them turned on until that button is released. Note: While a button is pressed, other buttons are ignored.

const byte Button1Pin = 2;
const byte Button2Pin = 3;
const byte Button3Pin = 4;
const byte Button4Pin = 5;
const byte Button5Pin = 6;
const byte Relay1Pin = 7;
const byte Relay2Pin = 8;
const byte Relay3Pin = 9;
const byte Relay4Pin = 10;
const byte Relay5Pin = 11;
const byte Relay6Pin = 12;

void setup()
{
  pinMode(Relay1Pin, OUTPUT);
  pinMode(Relay2Pin, OUTPUT);
  pinMode(Relay3Pin, OUTPUT);
  pinMode(Relay4Pin, OUTPUT);
  pinMode(Relay5Pin, OUTPUT);
  pinMode(Relay6Pin, OUTPUT);

  pinMode(Button1Pin, INPUT_PULLUP);
  pinMode(Button2Pin, INPUT_PULLUP);
  pinMode(Button3Pin, INPUT_PULLUP);
  pinMode(Button4Pin, INPUT_PULLUP);
  pinMode(Button5Pin, INPUT_PULLUP);
}

void loop()
{
  digitalWrite(Relay1Pin, HIGH);
  digitalWrite(Relay2Pin, HIGH);
  digitalWrite(Relay3Pin, HIGH);
  digitalWrite(Relay4Pin, HIGH);
  digitalWrite(Relay5Pin, HIGH);
  digitalWrite(Relay6Pin, HIGH);

  while (digitalRead(Button1Pin) == LOW)
  {
    digitalWrite(Relay1Pin, LOW);
    delay(50);
    digitalWrite(Relay2Pin, LOW);
  }

  while (digitalRead(Button2Pin) == LOW)
  {
    digitalWrite(Relay2Pin, LOW);
    delay(50);
    digitalWrite(Relay3Pin, LOW);
  }

  while (digitalRead(Button3Pin) == LOW)
  {
    digitalWrite(Relay1Pin, LOW);
    delay(50);
    digitalWrite(Relay3Pin, LOW);
  }

  while (digitalRead(Button4Pin) == LOW)
  {
    digitalWrite(Relay5Pin, LOW);
    delay(50);
    digitalWrite(Relay6Pin, LOW);
  }

  while (digitalRead(Button5Pin) == LOW)
  {
    digitalWrite(Relay2Pin, LOW);
    delay(50);
    digitalWrite(Relay4Pin, LOW);
  }
}
1 Like

Hi Tom, my application is to control approx 50 to 60 LEDs (12v) by using relays. I have 8 relay module that connects with Arduino (GND,In1,In2,....In8,Vcc) .My aim is that when i pushed 1st button then 1st relay gets on and after 50 or 100ms 2nd relay gets on.There are multiple combinations such as R1R2,R5R8,R4R5,R6R8,etc

Thank you so much. I really appreciate it. Code works fluently, No errors Nothing

Hi,

So how many relays do you think you will need?
I hope you use plug in type relays to help tracking any problems in the future.

Have you considered MOSFETs?
If you need isolation the an Opto-Coupler and a MOSFET.

How are you measuring the delay?

Thanks.. Tom... :smiley: :+1: :coffee: :australia:

Problem has been solved. Just replacing if() with while() and the code was working fine, Johnwasser helped me. When I was using if() the voltage drop was not occurring in Arduino that operate relays. I tried using mosfets too but it didn't work out, as soon as the the function got replaced mosfets and relays were working good.

Which will probably prevent you from adding to the code if you needed more functionality. NEver mind!

1 Like