SAS system implemantation

hello I'm trying to program Arduino for SAS (security access system)

the system has 2 doors each one with lock and reed switch 1 push button used to open door 1 and 1 push button to open door 2

door 1 won't open if door 2 is open and vice versa

the problem I am having that I need to modify my code so after the door one open the door 2 won't open until reed switch on door 1 read that the door has opened then closed

this is needed because the door mechanism doesn't relock until it's opened then closed

// set pin numbers:
const int door_contact_door1 = 2;     
const int door_contact_door2 = 3;      
const int open_button_door1 = 4;     
const int open_button_door2=  5;      
const int door2 = 8;     
const int door1=  9; 
    
//set variable
int door1_state = 0;     
int door2_state = 0;
int open_button_door1_state=0;
int open_button_door2_state=0;
int door_contact_door1_state=0;
int door_contact_door2_state=0;
void setup() {
 //set pin mode

  pinMode(door1, OUTPUT);
  pinMode(door2, OUTPUT);
  pinMode(door_contact_door1, INPUT);
  pinMode(door_contact_door2, INPUT);
  pinMode(open_button_door1, INPUT);
  pinMode(open_button_door2, INPUT);
}

void loop() {
  // read the state of the pushbutton and doors value:
  open_button_door1_state = digitalRead(open_button_door1);
  open_button_door2_state = digitalRead(open_button_door2);
  door_contact_door1_state = digitalRead(door_contact_door1);
  door_contact_door2_state = digitalRead(door_contact_door2);

  if(door_contact_door1_state==LOW&&door_contact_door2_state==LOW)
  {if (open_button_door1_state == LOW) 
  {
    //open door1
    digitalWrite(door1, HIGH);
    delay (1000);
  } 
  else
  {
    // close door1:
    digitalWrite(door1, LOW);
  }
  if (open_button_door2_state == LOW) 
  {
    //open door2
    digitalWrite(door2, HIGH);
    delay (1000);
  } 
  else
  {
    // close door2:
    digitalWrite(door2, LOW);
  }}
  
}

You need a variable that keeps track of the state of the system. For example (for door1) it might have the value 'C' for closed, 'O' for open and 'R' for reed-switch-closed-once. To get from state 'O' to state 'C' it should go through the sequence O-R-O-C

And you may want to ensure that the O-R-O-C happens within a short space of time.

...R

hello thank you for your reply this is what I was trying to do I used door state variable and the previous state of the reed switch but can't seem to put them together my programming skills are not very good

can anyone help me with it

AliAlameh: hello thank you for your reply this is what I was trying to do I used door state variable

You are not using it in the way I intended. All you are doing is recording the states of the switches. What I am suggesting is that you also need another variable to keep track of the progress of the system. For example if a reed switch was closed and now it is open it means that the door has changed from state 'C' to state 'O' or from state 'R' to state 'O'

...R

Break the sequences needed down into a pseudo list and then write the code for the sections.

Maybe something like this...

if Door 1 & door 2 closed?
 {
   if Door 1 button pressed?
   {
     Send open 1 signal high
     Wait for door 1 open
     Send open 1 signal low
     wait for door 1 closed
     wait for door 1 button released
   }
   if Door 2 button pressed?
   {
     Send open 2 signal
     Wait for door 2 open
     Stop open 2 signal
     wait for door 2 closed
     wait for door 2 button released
   }
 }

your comments, please

// set pin numbers:
const int door_contact_door1 = 2;
const int door_contact_door2 = 3;
const int open_button_door1 = 4;
const int open_button_door2 =  5;
const int door2 = 8;
const int door1 =  9;

//set variable
int door1_state = 0;
int door2_state = 0;
int open_button_door1_state = 0;
int open_button_door2_state = 0;
int door_contact_door1_state = 0;
int door_contact_door2_state = 0;
void setup() {
  //set pin mode

  pinMode(door1, OUTPUT);
  pinMode(door2, OUTPUT);
  pinMode(door_contact_door1, INPUT);
  pinMode(door_contact_door2, INPUT);
  pinMode(open_button_door1, INPUT);
  pinMode(open_button_door2, INPUT);
}

void loop() {
  // read the state of the pushbutton and doors value:
  open_button_door1_state = digitalRead(open_button_door1);
  open_button_door2_state = digitalRead(open_button_door2);
  door_contact_door1_state = digitalRead(door_contact_door1);
  door_contact_door2_state = digitalRead(door_contact_door2);

  if (door_contact_door1_state == LOW && door_contact_door2_state == LOW)
  {
    if (open_button_door1_state == LOW && door2_state == 0)
    {
      //open door1
      digitalWrite(door1, HIGH);
      delay(500);
      digitalWrite(door1, LOW);
      door1_state = 1;
      if (door_contact_door1_state == HIGH)
        door1_state = 0;
      do {
        door_contact_door1_state = digitalRead(door_contact_door1);
        open_button_door1_state = digitalRead(open_button_door1);
        if (door_contact_door1_state == HIGH)
          door1_state = 0;
      } while (door_contact_door1_state == LOW && open_button_door1_state == HIGH);
    }
    if (open_button_door2_state == LOW && door1_state == 0)
    {
      //open door2
      digitalWrite(door2, HIGH);
      delay(500);
      digitalWrite(door2, LOW);
      door2_state = 1;
      do {
        door_contact_door2_state = digitalRead(door_contact_door2);
        open_button_door2_state = digitalRead(open_button_door2);
        if (door_contact_door2_state == HIGH)
          door2_state = 0;
      } while (door_contact_door2_state == LOW && open_button_door2_state == HIGH);
    }
  }
}

AliAlameh:
your comments, please

You first - I’m shy.

Or, to be more helpful - tell us what your program actually does and tell us what you want it to do that is different.

…R

Robin2:
Or, to be more helpful - tell us what your program actually does and tell us what you want it to do that is different.

I think the OP outlined the requirement in the initial post.

Try this, based on my pseudo code.

// set pin numbers:
const byte door1_contact_pin = 2;                       // Door open/close contact. HIGH = Open, LOW = Closed
const byte door2_contact_pin = 3;
const byte door1_button_pin = 4;                        // Door release button. HIGH = Pressed, LOW = Released
const byte door2_button_pin =  5;
const byte door2_lock_pin = 8;                          // Door lock/latch output. HIGH = Unlock, LOW = Lock
const byte door1_lock_pin =  9;

// Global variables
uint32_t time;                                          // Used to record current millis time

void setup() {
  //set pin mode
  pinMode(door1_lock_pin, OUTPUT);
  pinMode(door2_lock_pin, OUTPUT);
  pinMode(door1_contact_pin, INPUT);
  pinMode(door2_contact_pin, INPUT);
  pinMode(door1_button_pin, INPUT);
  pinMode(door2_button_pin, INPUT);
}

void loop() {
  
  if (digitalRead(door1_contact_pin) == LOW && digitalRead(door2_contact_pin) == LOW) // Are both doors closed?
  { 
  // At this point both doors closed
    if (digitalRead(door1_button_pin) == HIGH)          // Door open button pressed?
    { 
    // Doors closed, button pressed
      digitalWrite(door1_lock_pin, HIGH);               // Send unlock signal
      
      time = millis();                                  // Get current time
      while((millis() - time) < 500)
      {
        if(digitalRead(door1_contact_pin) == HIGH);     // Wait for door to be pushed open or timeout
        {
          break;
        }
      }
      //delay(100);                                       // Wait a bit more
      digitalWrite(door1_lock_pin, LOW);                // Send lock signal
      // At this point the button is/was pressed and the door could be open/closed
      while(digitalRead(door1_contact_pin) == HIGH){};  // Wait for door to close
      while(digitalRead(door1_button_pin) == HIGH){};   // Wait for button to be released
    }
    
    if (digitalRead(door2_button_pin) == HIGH)          // Door open button pressed?
    { 
    // Doors closed, button pressed
      digitalWrite(door2_lock_pin, HIGH);               // Send unlock signal
      
      time = millis();                                  // Get current time
      while((millis() - time) < 500)
      {
        if(digitalRead(door2_contact_pin) == HIGH);     // Wait for door to be pushed open or timeout
        {
          break;
        }
      }
      //delay(100);                                       // Wait a bit more
      digitalWrite(door2_lock_pin, LOW);                // Send lock signal
      // At this point the button is/was pressed and the door could be open/closed
      while(digitalRead(door2_contact_pin) == HIGH){};  // Wait for door to close
      while(digitalRead(door2_button_pin) == HIGH){};   // Wait for button to be released
    }
    
  }
}

Riva: I think the OP outlined the requirement in the initial post.

He posted an updated program in Reply #5 without any indication of how it performs or what the remaining problems are.

...R

Hi,
How have you got your buttons wired?
How are you controlling the relays?

Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?

Thanks… Tom… :slight_smile:

Robin2: You first - I'm shy.

Or, to be more helpful - tell us what your program actually does and tell us what you want it to do that is different.

...R

sorry for the misunderstanding the updated program was based on your comments that helped me i was asking for comments as if i used your instructions as guidelines

well after using the updated program i noticed that when the door start to close the door contact change state many times i think the debounce function as RIVA stated will solve it

AliAlameh: sorry for the misunderstanding the updated program was based on your comments that helped me i was asking for comments as if i used your instructions as guidelines

Sorry, but NO is the answer. I see no evidence of a variable that is keeping track of the progress of the system.

And there should not be any WHILEs anywhere.

...R

sorry my bad put the wrong code earlier :o this is the one

// set pin numbers:
const int door_contact_door1 = 0;
const int door_contact_door2 = 1;
const int open_button_door1 = 2;
const int open_button_door2 = 3;
const int door2 = 8;
const int door1 =  9;

//set variable
int door1_state = 0;
int door2_state = 0;
int open_button_door1_state = 0;
int open_button_door2_state = 0;
int door_contact_door1_state = 0;
int door_contact_door2_state = 0;
void setup() {
  //set pin mode

  pinMode(door1, OUTPUT);
  pinMode(door2, OUTPUT);
  pinMode(door_contact_door1, INPUT);
  pinMode(door_contact_door2, INPUT);
  pinMode(open_button_door1, INPUT);
  pinMode(open_button_door2, INPUT);
}

void loop() {
  // read the state of the pushbutton and doors value:
  open_button_door1_state = digitalRead(open_button_door1);
  open_button_door2_state = digitalRead(open_button_door2);
  door_contact_door1_state = digitalRead(door_contact_door1);
  door_contact_door2_state = digitalRead(door_contact_door2);

  if (door_contact_door1_state == LOW && door_contact_door2_state == LOW)
  {
    if (open_button_door1_state == LOW && door2_state == 0)
    {
      //open door1
      digitalWrite(door1, HIGH);
      delay(500);
      digitalWrite(door1, LOW);
      door1_state = 1;
      if (door_contact_door1_state == HIGH)
        door1_state = 0;
      do {
        door_contact_door1_state = digitalRead(door_contact_door1);
        open_button_door1_state = digitalRead(open_button_door1);
        if (door_contact_door1_state == HIGH)
          door1_state = 0;
      } while (door_contact_door1_state == LOW && open_button_door1_state == HIGH);
    }
    if (open_button_door2_state == LOW && door1_state == 0)
    {
      //open door2
      digitalWrite(door2, HIGH);
      delay(500);
      digitalWrite(door2, LOW);
      door2_state = 1;
      do {
        door_contact_door2_state = digitalRead(door_contact_door2);
        open_button_door2_state = digitalRead(open_button_door2);
        if (door_contact_door2_state == HIGH)
          door2_state = 0;
      } while (door_contact_door2_state == LOW && open_button_door2_state == HIGH);
    }
  }
}

Hi, Please read post #9.

Thanks.. Tom... :)

AliAlameh: sorry my bad put the wrong code earlier :o this is the one

I like that you are reading all the switches and saving their values.

I don't like WHILE as it blocks loop() from repeating quickly. There should be no need for WHILE.

In my world view the states of the switches should be used to update another 1 or 2 variables that determine the state of the system. And the actions to trigger a lock should be determined by the system-state variables - not directly by the switches.

In effect, the switches are unaware of the locks and the locks are unaware of the switches. The only common ground between them should be the system state variables. I'm not actually sure whether you need one for each door or a single one that takes account of both doors.

Separating the inputs and outputs will make the whole thing much easier to manage. Each part can be tested separately.

...R