Using code to interlock relays on the SEED relay shield

Hi, just starting out so I realize that my code is quite bloated, but I have working code that operates a DC motor in both directions, using a button for each direction. The CLOSE SW activates and latches relays 1 and 2 until it reaches the FULLY CLOSED SW, and the OPEN SW activates and latches relays 3 and 4 until it reaches the FULLY OPEN SW (It's for a power door). The problem I have is I'm unclear as to add code to interlock the relays so that only 1 direction can be operated at one time; when relays 1 and 2 are on, relays 3 and 4 cannot be on, and vice versa.

I hope that I've listed this code to forum protocol:

// constants won't change. They're used here to // set pin numbers: const int closeSW = 2; // CLOSE Switch const int openSW = 3; // OPEN Switch const int fullyOpenSw = 8; // the number of the fully open mag switch const int fullyClosedSw = 9; // the number of the fully closed mag switch const int relay1 = 7; // the number of the LED pin const int relay2 = 6; // the number of the LED pin const int relay3 = 5; // the number of the LED pin const int relay4 = 4; // the number of the LED pin

// variables will change: int buttonState = 0; // variable for reading the pushbutton status int buttonState2 = 0; // variable for reading the pushbutton status int buttonState3 = 0; // variable for reading the pushbutton status int buttonState4 = 0; // variable for reading the pushbutton status

void setup() { // initialize the LED pin as an output: pinMode(relay1, OUTPUT); pinMode(relay2, OUTPUT); pinMode(relay3, OUTPUT); pinMode(relay4, OUTPUT); // initialize the pushbutton pin as an input: pinMode(closeSW, INPUT_PULLUP); pinMode(openSW, INPUT_PULLUP); pinMode(fullyOpenSw, INPUT_PULLUP); pinMode(fullyClosedSw, INPUT_PULLUP);

}

void loop() { // read the state of the pushbutton value: buttonState = digitalRead(closeSW); buttonState3 = digitalRead(fullyOpenSw); buttonState4 = digitalRead(fullyClosedSw);

// check if the pushbutton is pressed. // if it is, the buttonState is HIGH: if (buttonState == LOW) { // toggle the LED state: digitalWrite(relay1, !digitalRead(relay1)); digitalWrite(relay2, !digitalRead(relay2)); // debounce: delay(250); } // read the state of the pushbutton value: buttonState4 = digitalRead(fullyClosedSw);

// check if the pushbutton is pressed. // if it is, the buttonState is HIGH: if (buttonState4 == LOW) { // toggle the LED state: digitalWrite(relay1,LOW); digitalWrite(relay2,LOW); // debounce: delay(550);

} // read the state of the pushbutton value: buttonState2 = digitalRead(openSW);

// check if the pushbutton is pressed. // if it is, the buttonState is HIGH: if (buttonState2 == LOW) { // toggle the LED state: digitalWrite(relay3, !digitalRead(relay3)); digitalWrite(relay4, !digitalRead(relay4)); // debounce: delay(250); } // read the state of the pushbutton value: buttonState3 = digitalRead(fullyOpenSw);

// check if the pushbutton is pressed. // if it is, the buttonState is HIGH: if (buttonState3 == LOW) { // toggle the LED state: digitalWrite(relay3,LOW); digitalWrite(relay4,LOW); // debounce: delay(550); } }

The problem I have is I'm unclear as to add code to interlock the relays so that only 1 direction can be operated at one time; when relays 1 and 2 are on, relays 3 and 4 cannot be on, and vice versa.

One way would be to regard opening and closing as states and to add states for open an closed. The program would be any only one of the 4 states at any time.

Personally I would define the for states something like this

const byte open = 0;
const byte closing = 1;
const byte closed = 2;
const by opening = 3;

This would be best done using an enum, but let's not add complications yet.

Set the state variable to the current state of the doors, let's say open for illustration, then in loop() use switch/case based on the state to control which code is executed each time through loop(). For instance, when in an open state test for inputs that would cause the door to start closing. When detected, change to state closing and run the code to close the door. When the door becomes closed change to the closed state and so on.

Using switch/case ensures that only the required code is executed and the use of states ensures that only one is active at any one time. In loop() us

Thanks, but I went another direction that I'm more familiar with. I'll keep knocking off those example to get exposure to more efficient, streamlined approaches.

// check if the relay is on. // if it is, the buttonState is HIGH: if (relayState2 == HIGH) { // toggle the Relay states: digitalWrite(relay3,LOW); digitalWrite(relay4,LOW); // debounce: delay(550);