Dual Motor Gate Opener with Lock. Problem with 2nd set of limit switches

First off I would like to apologize if this has been discussed before. I'm fairly new to programming. I recently have started working on a electric gate program and all was working well until I added a second motor to the program. Now it seems the code ignores the second motors inputs (the gateOpen1, gateClosed1). I have to be missing something. The motors both run but as soon as gateOpen, or gateClosed is tripped it shuts off both motors. My code is below (all pins are not currently used in program). Any help would be greatly appreciated.

int activate = 1; // Button for C.O.S. int gateOpen = 2; // arm 1 int gateClosed =3; // arm 1 int relay1 = 5; //open! arm 1 int statLED =4; //LED int relay2 = 6; //close arm 1 int gateOpen1 = 7; // arm 2 int gateClosed1 = 8; // arm 2 int relay3 = 9; //open arm 2 int relay4 = 10; //close arm 2 int ExitWand = 11; //Auto exit (bypass activate button) int Safety = 12; //photo sensor for saftey int Lock = 13; //gate holding lock

unsigned long currentTime = 0; boolean gateState = true; //false = closed true = open boolean gateState1 = true; //false = closed true = open void setup() { pinMode(activate, INPUT_PULLUP); //set up I/O pinMode(gateOpen, INPUT_PULLUP); pinMode(gateClosed, INPUT_PULLUP); pinMode(relay1, OUTPUT); pinMode(relay2, OUTPUT); pinMode(gateOpen1, INPUT_PULLUP); pinMode(gateClosed1, INPUT_PULLUP); pinMode(relay3, OUTPUT); pinMode(relay4, OUTPUT); pinMode(ExitWand, INPUT_PULLUP); pinMode(Safety, INPUT_PULLUP); pinMode(Lock, OUTPUT); pinMode(statLED, OUTPUT); digitalWrite(relay1, HIGH); digitalWrite(relay2, HIGH); digitalWrite(relay3, HIGH); digitalWrite(relay4, HIGH); digitalWrite(Lock, HIGH); digitalWrite(statLED, HIGH); } void loop() {

if (digitalRead(activate) == LOW && gateState == false) { //check to see if the button is pressed and the gate is closed

digitalWrite(Lock, LOW); delay(1000); digitalWrite(relay2, HIGH); digitalWrite(relay1, LOW); delay(2500); digitalWrite(relay4, HIGH); digitalWrite(relay3, LOW); delay(1000); digitalWrite(Lock, HIGH);

while(1){ //run motor until switch is tripped if (digitalRead(gateOpen) == LOW) { //check switch state gateState = true; digitalWrite(relay1, HIGH); digitalWrite(relay2, HIGH); digitalWrite(statLED,HIGH);

if (digitalRead(gateOpen1) == LOW) gateState1 = true; digitalWrite(relay3, HIGH); digitalWrite(relay4, HIGH); delay(5000); break;

} if (millis() > currentTime + 500) { //flash status LED once digitalWrite(statLED, LOW); delay(500); currentTime = millis(); } else { digitalWrite(statLED, HIGH);

} } } if (digitalRead(activate) == LOW && gateState == true) { //check to see if the button is pressed and the gate is open

digitalWrite(relay1, HIGH); digitalWrite(relay2, LOW); delay(2500); digitalWrite(relay3, HIGH); digitalWrite(relay4, LOW);

while(1){ if (digitalRead(gateClosed) == LOW) { gateState = false; digitalWrite(relay1, HIGH); digitalWrite(relay2, HIGH); digitalWrite(statLED, HIGH);

if (digitalRead(gateClosed1) == LOW) gateState1 = false; digitalWrite(relay4, HIGH); digitalWrite(relay3, HIGH); digitalWrite(statLED, HIGH); delay(5000); break; }

if (millis() > currentTime + 500) { //flash status LED once digitalWrite(statLED, LOW); delay(500); currentTime = millis(); } else { digitalWrite(statLED, HIGH); } } } }

It would have made life a great deal easier if you had posted your code in code tags like this

intactivate=1;//ButtonforC.O.S.
intgateOpen=2;//arm1
intgateClosed=3;//arm1
intrelay1=5;//open!arm1
intstatLED=4;//LED
intrelay2=6;//closearm1
intgateOpen1=7;//arm2
intgateClosed1=8;//arm2
intrelay3=9;//openarm2
intrelay4=10;//closearm2
intExitWand=11;//Autoexit(bypassactivatebutton)
intSafety=12;//photosensorforsaftey
intLock=13;//gateholdinglock

unsignedlongcurrentTime=0;
booleangateState=true;//false=closedtrue=open
booleangateState1=true;//false=closedtrue=open
voidsetup(){
    pinMode(activate,INPUT_PULLUP);//setupI/O
    pinMode(gateOpen,INPUT_PULLUP);
    pinMode(gateClosed,INPUT_PULLUP);
    pinMode(relay1,OUTPUT);
    pinMode(relay2,OUTPUT);
    pinMode(gateOpen1,INPUT_PULLUP);
    pinMode(gateClosed1,INPUT_PULLUP);
    pinMode(relay3,OUTPUT);
    pinMode(relay4,OUTPUT);
    pinMode(ExitWand,INPUT_PULLUP);
    pinMode(Safety,INPUT_PULLUP);
    pinMode(Lock,OUTPUT);
    pinMode(statLED,OUTPUT);
    digitalWrite(relay1,HIGH);
    digitalWrite(relay2,HIGH);
    digitalWrite(relay3,HIGH);
    digitalWrite(relay4,HIGH);
    digitalWrite(Lock,HIGH);
    digitalWrite(statLED,HIGH);
}
voidloop(){

    if(digitalRead(activate)==LOW&&gateState==false){//checktoseeifthebuttonispressedandthegateisclosed

        digitalWrite(Lock,LOW);
        delay(1000);
        digitalWrite(relay2,HIGH);
        digitalWrite(relay1,LOW);
        delay(2500);
        digitalWrite(relay4,HIGH);
        digitalWrite(relay3,LOW);
        delay(1000);
        digitalWrite(Lock,HIGH);


        while(1){//runmotoruntilswitchistripped
            if(digitalRead(gateOpen)==LOW){//checkswitchstate
                gateState=true;
                digitalWrite(relay1,HIGH);
                digitalWrite(relay2,HIGH);
                digitalWrite(statLED,HIGH);

            if(digitalRead(gateOpen1)==LOW)
                gateState1=true;
                digitalWrite(relay3,HIGH);
                digitalWrite(relay4,HIGH);
                delay(5000);
                break;

            }
            if(millis()>currentTime+500){//flashstatusLEDonce
                digitalWrite(statLED,LOW);
                delay(500);
                currentTime=millis();
        }
            else{
                digitalWrite(statLED,HIGH);


        }
        }
    }
    if(digitalRead(activate)==LOW&&gateState==true){//checktoseeifthebuttonispressedandthegateisopen

        digitalWrite(relay1,HIGH);
        digitalWrite(relay2,LOW);
        delay(2500);
        digitalWrite(relay3,HIGH);
        digitalWrite(relay4,LOW);

        while(1){
            if(digitalRead(gateClosed)==LOW){
                gateState=false;
                digitalWrite(relay1,HIGH);
                digitalWrite(relay2,HIGH);
                digitalWrite(statLED,HIGH);

            if(digitalRead(gateClosed1)==LOW)
                gateState1=false;
                digitalWrite(relay4,HIGH);
                digitalWrite(relay3,HIGH);
                digitalWrite(statLED,HIGH);
                delay(5000);
                break;
    }


            if(millis()>currentTime+500){//flashstatusLEDonce
                digitalWrite(statLED,LOW);
                delay(500);
                currentTime=millis();
            }
        else{
            digitalWrite(statLED,HIGH);
            }
        }
    }
}

Without studying it closely I suspect the problem is your use of WHILE loops that block everything else until they finish.

Look at how the code is organized in the demo several things at a time and in the Thread planning and implementing a program. They use variables to keep track of the state of the system (for example in your case one of them might be gate1Status and it might have 4 options W for wide-open, O for opening, C for closing and S for shut). That way the code to close (or open) the gate does not have to wait while is happens.

...R

Thanks! I will check out your suggestions.