controling 2 relays independantly

Hi

I have managed after much frustration to get my double relay module to work...Hooray, but I cant find (And believe me ive looked for days) any help regarding code to create a timed stop between relay1 & relay 2 -to allow motor to come to a stop before rotating in the opposite direction.

THIS IS THE CODE IM USING, IT WORKS BUT I NEED TO INSERT A (MILLIS) BREAK/STOP

void setup(){ pinMode(11,OUTPUT); pinMode(12,OUTPUT); }

void loop(){ digitalWrite(11,LOW); // SOMEHOW I NEED TO INSERT THE CODE TO MAKE 5 SECOND BREAK BETWEEN digitalWrite(12,HIGH); RELAY 1 AND RELAY 2 delay(500); digitalWrite(11,HIGH); digitalWrite(12,LOW); delay(500); } This is what im trying to achieve

relay 1 on for 5 secs

then BREAK/STOP

relay 2 will start also for 5 secs AFTER STOP

THEN BREAK/STOP

The two relays must NEVER be on at the same time.

I am trying to turn a motor one way, then let it stop, then turn it the other way

As i have said I have managed to get both relays activated but it seems that i need to split my programme in half and add a STOP command

Yes weve all heard it before but can someone please throw me a crumb of kindness and lend some advice

Many many thanks in advance

Something like this?

const int Relay1Pin = 11;
const int Relay2Pin = 12;
const int RelayOn = HIGH;
const int RelayOff = LOW;

void setup() {
  digitalWrite(Relay1Pin,RelayOff);
  pinMode(Relay1Pin,OUTPUT);
  digitalWrite(Relay2Pin,RelayOff);
  pinMode(Relay2Pin,OUTPUT);
}

void loop() {
  // Relay 1 ON for 5 seconds
  digitalWrite(Relay1Pin,RelayOn);
  delay(5000);

  //Both relays OFF for 5 seconds
  digitalWrite(Relay1Pin,RelayOff);
  digitalWrite(Relay2Pin,RelayOff);
  delay(5000);

  // Relay 2 ON for 5 seconds
  digitalWrite(Relay2Pin,RelayOn);
  delay(5000);
  
  //Both relays OFF for 5 seconds
  digitalWrite(Relay1Pin,RelayOff);
  digitalWrite(Relay2Pin,RelayOff);
  delay(5000);
}

WOW!! Thank you so much for taking the time to write all that code, I'm going to jump straight on it and see what comes out at the end, I will surely get back to you and let you know how I got on, once again thank you so much!!

digitalWrite(Relay1Pin,RelayOff); pinMode(Relay1Pin,OUTPUT);

John is there a reason for putting the digitalWrite before the pinMode?

JimboZA:

digitalWrite(Relay1Pin,RelayOff); pinMode(Relay1Pin,OUTPUT);

John is there a reason for putting the digitalWrite before the pinMode?

Yes. It's so the outputs will be in the RelayOff state when the output drivers are activated. The original poster didn't say if their relays were Active High (write HIGH for ON and LOW for OFF) or Active Low (Write LOW for ON and HIGH for OFF). If they are Active Low and the outputs aren't set HIGH before being changed to OUTPUT the pins will be pulled LOW, turning on both relays and causing some sort of dire problems that the original poster did not specify.

I defined "RelayOn" and "RelayOff" to make the choice between Active High and Active Low easy to do and to make the intent of the code more obvious.

Thanks John