Multiple relays

Hello everyone. I want to control 2 relays after I press a button. First both relays needs to stay on 1 minute, after this one of the relay need to stay on for 1 minute, then the second 1 minute and at the finish both needs to stay on for one minute. The cycle starts again after I press the button. I tried with delay but only works with the first two steps. This can be done only with mills, or do I need to use arrays also? Any help will be welcome. Thank you.

Step 1: both relays stay on 1 minute
Step 2: relay1 stays on for 1 minute
Step 3: relay2 stays on for 1 minute
Step 4 : both relays stay on 1 minute
Step 5: if button pressed, repeat 1-5, else wait.

So what happen after steps 1 and 2?

if you are Ok with blocking code, it's just a matter of sending the commands in the right order / timing

const byte pinButton = 2;
const byte pinRelay1 = 3;
const byte pinRelay2 = 4;

const byte RELAY_ON = LOW;
const byte RELAY_OFF = HIGH;


void setup() {
  pinMode(pinButton, INPUT_PULLUP);
  pinMode(pinRelay1, OUTPUT);
  pinMode(pinRelay2, OUTPUT);
  digitalWrite(pinRelay1, RELAY_OFF);
  digitalWrite(pinRelay2, RELAY_OFF);
}

void loop() {

  // wait for the button to be pressed (blocking)
  while (digitalRead(pinButton) == HIGH) yield(); 

  // button has been pressed, turn relays on
  digitalWrite(pinRelay1, RELAY_ON);
  digitalWrite(pinRelay2, RELAY_ON);

  // now wait 1 minute = 60,000 ms
  delay(60000ul);

  // turn relay 1 off
  digitalWrite(pinRelay1, RELAY_OFF);

  // now wait 1 minute = 60,000 ms
  delay(60000ul);

  // turn relay 1 on and relay2 off
  digitalWrite(pinRelay1, RELAY_ON);
  digitalWrite(pinRelay2, RELAY_OFF);

  // now wait 1 minute = 60,000 ms
  delay(60000ul);

  // now both on for one minute
  digitalWrite(pinRelay2, RELAY_ON); 

  // now wait 1 minute = 60,000 ms
  delay(60000ul);
}

(typed here, mind typos)

I was making this table before Jim posted. I'm confused by the original description of what you want. Does this table show what you want?

Yes Dave, just like you did the table.

You will need to add the following at the end of the loop in @J-M-L's code

1 Like

oops indeed ! thanks for catching that. I modified the code (moved them from the setup to the start of the loop where they should have been)

1 Like

Could the holding current of the relays be sinking too much power, and the board is resetting?

Current when the relay is active: 70mA (each relay)

Try this with only one realy.

Thank you all for your responses. I already added the relay off code. This relays should stop at the end, when a certain position is reached. I have some magnetic sensor that I added to another input. This works in the loop above only if the pin is ALREADY connected( HIGH state). But how should I code it to wait for the magnetic sensor to become HIGH and then to stop the relays? Thank you in advance

So what happens after the first two steps?

in the code there is an active wait for the button to be pressed

polling (blocking) your sensor state would be done in a similar way.

In an ideal world you would not block the loop like this and you could benefit from studying state machines. Here is a small introduction to the topic: Yet another Finite State Machine introduction

I have 2 "buttons". First one that starts the relays cycle and the second one (magnetic) that stops the relays at the end(the line that you didn't wrote). I will take a look at topic. Thank you.

how do you read the magnetic button's state ?

I'd suggest you check the sample codes for one of the numerous button library such as Button in easyRun or OneButton or Toggle or EasyButton or Bounce2, ...

A button library might make your code easier to write.

const byte pinButton = 7;
const int Button = 6;
const byte pinRelay1 = 2;
const byte pinRelay2 = 3;
int Buttonstate = 0;

const byte RELAY_ON = LOW;
const byte RELAY_OFF = HIGH;

void setup() {
pinMode(pinButton, INPUT);
pinMode(Button, INPUT);
pinMode(pinRelay1, OUTPUT);
pinMode(pinRelay2, OUTPUT);
digitalWrite(pinRelay1, RELAY_OFF);
digitalWrite(pinRelay2, RELAY_OFF);
}

void loop() {

// wait for the button to be pressed (blocking)
while (digitalRead(pinButton) == HIGH) yield();

// button has been pressed, turn relays on
digitalWrite(pinRelay1, RELAY_ON);
digitalWrite(pinRelay2, RELAY_ON);

// now wait 1 minute = 60,000 ms
delay(5000ul);

// turn relay 1 off
digitalWrite(pinRelay1, RELAY_OFF);

// now wait 1 minute = 60,000 ms
delay(5000ul);

// turn relay 1 on and relay2 off
digitalWrite(pinRelay1, RELAY_ON);
digitalWrite(pinRelay2, RELAY_OFF);

// now wait 1 minute = 60,000 ms
delay(5000ul);

// now both on for one minute
digitalWrite(pinRelay2, RELAY_ON);

// now wait 1 minute = 60,000 ms
delay(5000ul);
digitalWrite(pinRelay2, RELAY_OFF);
digitalWrite(pinRelay1, RELAY_OFF);
delay(1000ul);

digitalWrite(pinRelay1, RELAY_ON);
digitalWrite(pinRelay2, RELAY_ON);

Buttonstate=digitalRead(Button);
if (Buttonstate == HIGH)
{
digitalWrite(pinRelay2, RELAY_OFF);
digitalWrite(pinRelay1, RELAY_OFF);
}

I added some delays to find out how program works. I will read more about it and try to figure it out.

Please correct your post and add code tags around your code.

const byte pinButton = 7;
const int Button = 6;
const byte pinRelay1 = 2;
const byte pinRelay2 = 3;
int Buttonstate = 0;



const byte RELAY_ON = LOW;
const byte RELAY_OFF = HIGH;



void setup() {
  pinMode(pinButton, INPUT);
  pinMode(Button, INPUT);
  pinMode(pinRelay1, OUTPUT);
  pinMode(pinRelay2, OUTPUT);
  digitalWrite(pinRelay1, RELAY_OFF);
  digitalWrite(pinRelay2, RELAY_OFF);
}

void loop() {

  // wait for the button to be pressed (blocking)
  while (digitalRead(pinButton) == HIGH) yield(); 

  // button has been pressed, turn relays on
  digitalWrite(pinRelay1, RELAY_ON);
  digitalWrite(pinRelay2, RELAY_ON);

  // now wait 1 minute = 60,000 ms
  delay(5000ul);

  // turn relay 1 off
  digitalWrite(pinRelay1, RELAY_OFF);

  // now wait 1 minute = 60,000 ms
  delay(5000ul);

  // turn relay 1 on and relay2 off
  digitalWrite(pinRelay1, RELAY_ON);
  digitalWrite(pinRelay2, RELAY_OFF);

  // now wait 1 minute = 60,000 ms
  delay(5000ul);

  // now both on for one minute
  digitalWrite(pinRelay2, RELAY_ON); 

  // now wait 1 minute = 60,000 ms
  delay(5000ul);
  digitalWrite(pinRelay2, RELAY_OFF);
  digitalWrite(pinRelay1, RELAY_OFF);
  delay(1000ul);

  digitalWrite(pinRelay1, RELAY_ON);
  digitalWrite(pinRelay2, RELAY_ON);

  Buttonstate=digitalRead(Button);
  if (Buttonstate == HIGH)
  {
    digitalWrite(pinRelay2, RELAY_OFF);
    digitalWrite(pinRelay1, RELAY_OFF);
  } 

I am also thinking to count the second button. If it is =1 to turn off the relays. But I am not sure if it is running with blocking chains.

Your code is fine.

Add serial printing so you know what it is doing.

Test with no relays, just use LEDs with series current limiting resistors as proxy indicators.

I did make a number of changes, but none to the logic. In particular I used a slide switch for the part that decides wheterh to extiguish the LEDs at the bottom of the loop.

Sketch is fine.
const byte pinButton = 7;
const int Button = 6;

const byte pinRelay1 = 2;
const byte pinRelay2 = 3;
int Buttonstate = 0;



const byte RELAY_ON = HIGH;
const byte RELAY_OFF = LOW;

# define PRESST LOW

void setup() {
  Serial.begin(115200);
  Serial.println("\nwake up\n");

// it wasn't even printing?  for (; ; );

  pinMode(pinButton, INPUT_PULLUP);
  pinMode(Button, INPUT_PULLUP);

  pinMode(pinRelay1, OUTPUT);
  pinMode(pinRelay2, OUTPUT);

  digitalWrite(pinRelay1, RELAY_OFF);
  digitalWrite(pinRelay2, RELAY_OFF);
}

# define MINUTE 1000

void loop() {

  Serial.println("LOOP");

  // wait for the button to be pressed (blocking)
  while (digitalRead(pinButton) != PRESST) yield(); 

  // button has been pressed, turn relays on
  digitalWrite(pinRelay1, RELAY_ON);
  digitalWrite(pinRelay2, RELAY_ON);

  // now wait 1 minute = 60,000 ms
  delay(MINUTE);

  // turn relay 1 off
  digitalWrite(pinRelay1, RELAY_OFF);

  // now wait 1 minute = 60,000 ms
  delay(MINUTE);

  // turn relay 1 on and relay2 off
  digitalWrite(pinRelay1, RELAY_ON);
  digitalWrite(pinRelay2, RELAY_OFF);

  // now wait 1 minute = 60,000 ms
  delay(MINUTE);

  // now both on for one minute
  digitalWrite(pinRelay2, RELAY_ON); 

  // now wait 1 minute = 60,000 ms
  delay(MINUTE);
  digitalWrite(pinRelay2, RELAY_OFF);
  digitalWrite(pinRelay1, RELAY_OFF);
  delay(1000ul);

  digitalWrite(pinRelay1, RELAY_ON);
  digitalWrite(pinRelay2, RELAY_ON);

  Buttonstate=digitalRead(Button);
  if (Buttonstate == PRESST)
  {
    digitalWrite(pinRelay2, RELAY_OFF);
    digitalWrite(pinRelay1, RELAY_OFF);
  }
}

HTH

a7