Interlocking system, need help with the code

Dear All

My name is Rafiq I work in Dubai, I am new to Arduino programming and trying to make an interlocking system using two motorized locks,

I have successfully completed the main task of operating the 2 locks connected to Arduino UNO, I am able to lock and unlock both the locks using 4 push buttons based on the below code.

Now I want to add interlocking between both locks if one lock open the other lock remain close and vise versa

I can do this by using some manual relays but I want to use a simple limit switch with Arduino to control the interlocking operation. if anyone can help with the code please reply.

const int potPin1 = A0;      //Potentiometer1 to control motor RPM
const int unlock1pbPin = 13;// push button to lock
const int lock1pbPin = 12;  // push button to unlock
const int pin1 = 11;        // Hbridge pin1
const int pin2 = 10;        // Hbridge pin2
const int interlock2 = 3;    //interlock switch how to use it for interlock?

const int potPin2 = A1;
const int unlock2pbPin = 7;
const int lock2pbPin = 8;
const int pin3 = 5;
const int pin4 = 6;

int potValue1 = 0;
int motorValue1 = 0;
int fwdbuttonState1 = 0;
int bckbuttonState1 = 0;
int interlockState2 = 0;

int potValue2 = 0;
int motorValue2 = 0;
int fwdbuttonState2 = 0;
int bckbuttonState2 = 0;


void setup()
{

  pinMode(unlock1pbPin, INPUT_PULLUP); 
  pinMode(lock1pbPin, INPUT_PULLUP);  
  pinMode(pin1, OUTPUT);
  pinMode(pin2, OUTPUT);
  pinMode(interlock2, OUTPUT);


  pinMode(unlock2pbPin, INPUT_PULLUP);
  pinMode(lock2pbPin, INPUT_PULLUP);
  pinMode(pin3, OUTPUT);
  pinMode(pin4, OUTPUT);

}

void loop()
{

  potValue1 = analogRead(potPin1);
  motorValue1 = map(potValue1, 0, 1023, 0, 255);
  fwdbuttonState1 = digitalRead(unlock1pbPin);
  bckbuttonState1 = digitalRead(lock1pbPin);
  interlockState2 = digitalRead(interlock2);

  potValue2 = analogRead(potPin2);
  motorValue2 = map(potValue2, 0, 1023, 0, 255);
  fwdbuttonState2 = digitalRead(unlock2pbPin);
  bckbuttonState2 = digitalRead(lock2pbPin);


  if(fwdbuttonState1 == LOW)
  {

    analogWrite(pin1, motorValue1);
    delay(500);
    digitalWrite (pin2, LOW);

  }

  else if (bckbuttonState1 == LOW)
  {

    analogWrite(pin2, motorValue1);
    delay(500);
    digitalWrite (pin1, LOW);

  }

  else
  {

    digitalWrite (pin1, LOW);
    digitalWrite (pin2, LOW);

  }

  if (fwdbuttonState2 == LOW)
  {

    analogWrite(pin3, motorValue1);
    delay(500);
    digitalWrite (pin4, LOW);

  }

  else if (bckbuttonState2 == LOW)
  {

    analogWrite(pin4, motorValue1);
    delay(500);
    digitalWrite (pin3, LOW);

  }

  else
  {

    digitalWrite (pin3, LOW);
    digitalWrite (pin4, LOW);

  }

}

Thank you
RAK

If you want to prevent both locks open at the same time use logic like this

if (shallOpen1 && !isOpen2) doOpen1;
if (shallOpen2 && !isOpen1) doOpen2;

Dear DrDiettrich

I really want to say thanks to you, your reply was very short and brief and it helped a lot, it solved the problem and the project is completed.

I have a small request and if you have some spare time then I would like you to see the below code and give me some tips and advice to improve my skills.

Thank you
RAK

// Lock1
const int potPin1 = A0;      //Potentiometer1 to control motor Speed
const int unlock1pbPin = 13;// push button to lock
const int lock1pbPin = 12;  // push button to unlock
const int pin1 = 11;        // Hbridge pin1
const int pin2 = 10;        // Hbridge pin2
const int interlock2 = 3;    //interlock Reed switch
// Lock2
const int potPin2 = A1;
const int unlock2pbPin = 7;
const int lock2pbPin = 8;
const int pin3 = 5;
const int pin4 = 6;
const int interlock1 = 9;

int potValue1 = 0;
int motorValue1 = 0;
int fwdbuttonState1 = 0;
int bckbuttonState1 = 0;
int interlockState2 = 0;

int potValue2 = 0;
int motorValue2 = 0;
int fwdbuttonState2 = 0;
int bckbuttonState2 = 0;
int interlockState1 = 0;


void setup()
{

  pinMode(unlock1pbPin, INPUT_PULLUP); //push button to open lock1
  pinMode(lock1pbPin, INPUT_PULLUP);  // push button to close lock1
  pinMode(pin1, OUTPUT);
  pinMode(pin2, OUTPUT);
  pinMode(interlock2, INPUT_PULLUP);


  pinMode(unlock2pbPin, INPUT_PULLUP);
  pinMode(lock2pbPin, INPUT_PULLUP);
  pinMode(pin3, OUTPUT);
  pinMode(pin4, OUTPUT);
  pinMode(interlock1, INPUT_PULLUP);

}

void loop()
{

  potValue1 = analogRead(potPin1);
  motorValue1 = map(potValue1, 0, 1023, 0, 255);
  fwdbuttonState1 = digitalRead(unlock1pbPin);
  bckbuttonState1 = digitalRead(lock1pbPin);
  interlockState2 = digitalRead(interlock2);

  potValue2 = analogRead(potPin2);
  motorValue2 = map(potValue2, 0, 1023, 0, 255);
  fwdbuttonState2 = digitalRead(unlock2pbPin);
  bckbuttonState2 = digitalRead(lock2pbPin);
  interlockState1 = digitalRead(interlock1);


  if (fwdbuttonState1 == LOW && interlockState2 == LOW)
  {

    analogWrite(pin1, motorValue1);
    delay(500);
    digitalWrite (pin2, LOW);

  }

  else if (bckbuttonState1 == LOW && interlockState2 == LOW)
  {

    analogWrite(pin2, motorValue1);
    delay(500);
    digitalWrite (pin1, LOW);

  }

  else
  {

    digitalWrite (pin1, LOW);
    digitalWrite (pin2, LOW);

  }

  if (fwdbuttonState2 == LOW && interlockState1 == LOW)
  {

    analogWrite(pin3, motorValue1);
    delay(500);
    digitalWrite (pin4, LOW);

  }

  else if (bckbuttonState2 == LOW && interlockState1 == LOW)
  {

    analogWrite(pin4, motorValue1);
    delay(500);
    digitalWrite (pin3, LOW);

  }

  else
  {

    digitalWrite (pin3, LOW);
    digitalWrite (pin4, LOW);

  }

}

At a first glance I see names like pin1, pin2. Assign meaningful names to these as well.
Then I miss debounce or state change detection code with the buttons.
A few more comments on the code will be helpful to other people or later also to you.

Think about the data types you need. Everything that's const int could be const byte. Every variable that you store the result of a digitalRead in could be byte too.

Your loop function is too long for my taste. Consider splitting parts of it out into separate functions.

Const variables can be optimized away by the compiler. Then the type size doesn't matter for memory usage. But it may matter in expression evaluation, where all operations are evaluated using the biggest argument type.