Opinions about my first sketch

Got me a project with 2 IR sensors and a RFDigital wireless shield.

The sketch is working as-is, but I feel it is poorly written.
I’m extremely green when it comes to writing code, so please be easy on me.

Maybe there is someone out there that have a suggestion how I could get a better sketch?
I have 1 IR sensor for each of my two garage doors. I have LED’s inside my house that shows the position of the doors.
2 LEDs to indicate door open, lights up(independent)
1 LED to indicate when both are closed.

const char out1 = 0x10;                    // bit 4, LED Output 1 on the wireless shield in the house
const char out2 = 0x20;                    // bit 5, LED Output 2 on the wireless shield in the house
const char out3 = 0x40;                    // bit 6, LED Output 3 on the wireless shield in the house
int sensorPinR = A0;                       // Right Garage door sensor
int sensorPinL = A1;                       // Left Garage door sensor
int sensorValR = 0;
int sensorValL = 0;


void setup() {
  Serial.begin(9600);
}

void loop() {
  sensorValR = analogRead(sensorPinR);
  sensorValL = analogRead(sensorPinL);
  
  if(sensorValR >= 250 && sensorValL >= 250)   // Both garage doors open
  {
  Serial.write(out1 && out2);                  // Output 1 goes high and 2xRED LED's is on
  delay(500);
  }
  if(sensorValR <= 250 && sensorValL >= 250)   // Only Left garage door open
  {
  Serial.write(out2);                          // Output 2 goes high and 1xRED LED is on
  delay(500);
  }
  if(sensorValR >= 250 && sensorValL <= 250)   // Only Right garage door open
  {
  Serial.write(out1);                          // Output 1 goes high and 1xRED LED is on
  delay(500);
  }
    else                                       // Both garage doors are closed
    {
    Serial.write(out3);                        // Output 3 goes high and 1xGREEN LED is on
    delay(20); 
    }
}

Cheers

It works. That's a lot more progress than many achieve. Hooray.

It's a short sketch. There is no requirement to "improve" it - it won't work any better.

Looking to the future, and to larger projects a couple of things spring to mind. You should learn how to do without the delay() function and you should organize your code into different functions that deal with specific parts of the problem.

The demo sketch in the first post of this Thread may give you some ideas about what I am talking about.

Have fun

...R

I don’t like your indentation.
Why the last delay() have a different time for the others?
Why you use Serial.write(); and you only write the value of the variables, and not different messages?
I don’t like when you write sensorValR >= 250 in one if and sensorValR <= 250 in the other. Why the 2 if's have the “=” sign?
Why don’t use else if instead of only if?

Thanks for your replies!

luisilva:
I don’t like your indentation.
Why the last delay() have a different time for the others?

This is because I want the 1 green LED that indicate both garage doors closed to be always on when they are closed.
The RFdigital wireless shield is programmed so that when the Serial.write to out1,out2, or out3, the Output goes high for 500ms and then off. After trial and error, I have been able to get a constant light in the green LED with this delay.

Why you use Serial.write(); and you only write the value of the variables, and not different messages?
I don’t like when you write sensorValR >= 250 in one if and sensorValR <= 250 in the other. Why the 2 if's have the “=” sign?
Why don’t use else if instead of only if?

This is what I maybe had hoped someone would tell me a bit more about, since I’m not sure how to use the else if.
This sketch is made of very little knowledge, and only by stuff I have figured out with trial and error.

The sketch work, yes, but maybe not as originally intended. :slight_smile:
Have uploaded a quick video just to show some of my problems :stuck_out_tongue:

To help with the indentation (which makes the structure of the code more visible at a glance), use auto-format (CTRL-T) in the Arduino IDE.

Hackscribble:
To help with the indentation (which makes the structure of the code more visible at a glance), use auto-format (CTRL-T) in the Arduino IDE.

Thank you,

Here is the auto-formated code:

const char out1 = 0x10;                    // bit 4, LED Output 1 on the wireless shield in the house
const char out2 = 0x20;                    // bit 5, LED Output 2 on the wireless shield in the house
const char out3 = 0x40;                    // bit 6, LED Output 3 on the wireless shield in the house
int sensorPinR = A0;                       // Right Garage door sensor
int sensorPinL = A1;                       // Left Garage door sensor
int sensorValR = 0;
int sensorValL = 0;


void setup() {
  Serial.begin(9600);
}

void loop() {
  sensorValR = analogRead(sensorPinR);
  sensorValL = analogRead(sensorPinL);

  if (sensorValR >= 250 && sensorValL >= 250)  // Both garage doors open
  {
    Serial.write(out1 && out2);                  // Output 1 goes high and 2xRED LED's is on
    delay(500);
  }
  if (sensorValR <= 250 && sensorValL >= 250)  // Only Left garage door open
  {
    Serial.write(out2);                          // Output 2 goes high and 1xRED LED is on
    delay(500);
  }
  if (sensorValR >= 250 && sensorValL <= 250)  // Only Right garage door open
  {
    Serial.write(out1);                          // Output 1 goes high and 1xRED LED is on
    delay(500);
  }
  else                                       // Both garage doors are closed
  {
    Serial.write(out3);                        // Output 3 goes high and 1xGREEN LED is on
    delay(20);
  }
}
  if (sensorValR >= 250 && sensorValL <= 250)  // Only Right garage door open
  {
    Serial.write(out1);                          // Output 1 goes high and 1xRED LED is on
    delay(500);
  }
  else                                       // Both garage doors are closed
  {
    Serial.write(out3);                        // Output 3 goes high and 1xGREEN LED is on
    delay(20);
  }

This last if / else statement is separate from the first two if statements. So the else part will be true every time (sensorValR >= 250 && sensorValL <= 250) is false. And this won’t just be when both doors are closed.

Try changing to if / else if / else …

if (sensorValR >= 250 && sensorValL >= 250)  // Both garage doors open
{
    ...
}
else if (sensorValR <= 250 && sensorValL >= 250)  // Only Left garage door open
{
    ...
}
else if (sensorValR >= 250 && sensorValL <= 250)  // Only Right garage door open
{
    ...
}
else // Both garage doors are closed
{
   ...
}

Also, as luisilva said, a reading of exactly 250 is taken to mean both open and closed, so you should change one of the inequalities. For example, make all the “<=” to be “<”.

The RFdigital wireless shield is programmed so that when the Serial.write to out1,out2, or out3, the Output goes high for 500ms and then off.

It would be simpler if the appropriate outputs went HIGH and did not turn off unless commanded to. That way you could check each input and turn the LED on or off without needing a delay() in your code.

Hackscribble:

  if (sensorValR >= 250 && sensorValL <= 250)  // Only Right garage door open

{
    Serial.write(out1);                          // Output 1 goes high and 1xRED LED is on
    delay(500);
  }
  else                                       // Both garage doors are closed
  {
    Serial.write(out3);                        // Output 3 goes high and 1xGREEN LED is on
    delay(20);
  }




This last if / else statement is separate from the first two if statements. So the else part will be true every time (sensorValR >= 250 && sensorValL <= 250) is false. And this won't just be when both doors are closed.

Try changing to if / else if / else ...



if (sensorValR >= 250 && sensorValL >= 250)  // Both garage doors open
{
    …
}
else if (sensorValR <= 250 && sensorValL >= 250)  // Only Left garage door open
{
    …
}
else if (sensorValR >= 250 && sensorValL <= 250)  // Only Right garage door open
{
    …
}
else // Both garage doors are closed
{
   …
}




Also, as luisilva said, a reading of exactly 250 is taken to mean both open and closed, so you should change one of the inequalities. For example, make all the "<=" to be "<".

Thank you Hackscribble!
With your easy sugestions it is now working as I wanted, except that the wireless shield can not set HIGH to two outputs at the time, so had to use:

if(sensorValR > 250 && sensorValL > 250)   // Both garage doors open
  {
  Serial.write(out1);                  // Output 1 and output 2 is flashing 2xRED LED
  delay(500);
  Serial.write(out2);
  delay(500);
  }

But if they blink every other time or together, I don’t really care about :stuck_out_tongue:

UKHeliBob:

The RFdigital wireless shield is programmed so that when the Serial.write to out1,out2, or out3, the Output goes high for 500ms and then off.

It would be simpler if the appropriate outputs went HIGH and did not turn off unless commanded to. That way you could check each input and turn the LED on or off without needing a delay() in your code.

Absolutely, that would be the best. But that is not in my hands. The wireless shield i programmed with specific modes, and this mode(mode 4), is the only one I can use with my setup.
According to the manufacturer. :relaxed:

Storic, can you post the link to the RF module page? It's easy for me if I know what I'm talking about.

luisilva:
Storic, can you post the link to the RF module page? It's easy for me if I know what I'm talking about.

Sure,
This is the receiving part, in the house:
http://www.rfdigital.com/product/rfd21813-dip-version-of-rfd21733/

And this is the part that is connected to the arduino and transmits:
http://www.rfdigital.com/product/rfd21815-eval-board-for-rfd21733/

Both run on the RFD21733 RFDP8 RF Module.
Manual: http://www.rfdigital.com/wp-content/uploads/2014/03/RFDP8.RF_.Modules.Manual.pdf
Mode overview is on page 19

I’m watching the page 16, the 3rd figure, and what they say is :“Bit positions 4, 5, 6 represent OUT1, OUT2, OUT3 on the receiver, all other bits must be zero.” Now I can understand your code, and I can say that you have one error.
This line:

    Serial.write(out1 && out2);                  // Output 1 goes high and 2xRED LED's is on

must be:

    Serial.write(out1 & out2);                  // Output 1 goes high and 2xRED LED's is on

The “&” operator its called “bitwise and” and the result from my suggestion is: 0x10 & 0x20 = 0001 0000 & 0010 0000= 0011 0000=0x30.
The result of the operation with your “&&” is 0x01, I think. This operator is caller “logic and” or “boolean and”. You can find more details here and here.

About the program, now that I’ve seen this, I don,t like it.

Here is my suggestion:

const char out1 = 0x10;                    // bit 4, LED Output 1 on the wireless shield in the house
const char out2 = 0x20;                    // bit 5, LED Output 2 on the wireless shield in the house
const char out3 = 0x40;                    // bit 6, LED Output 3 on the wireless shield in the house
int sensorPinR = A0;                       // Right Garage door sensor
int sensorPinL = A1;                       // Left Garage door sensor
int sensorValR = 0;
int sensorValL = 0;

int msg;

void setup() {
  Serial.begin(9600);
}

void loop() {
  sensorValR = analogRead(sensorPinR);
  sensorValL = analogRead(sensorPinL);

  if (sensorValR >= 250 && sensorValL >= 250) { // Both garage doors open
      msg = out1 & out2;
  }
  else if (sensorValR < 250 && sensorValL >= 250)  { // Only Left garage door open
      msg = out2;
  }
  else if (sensorValR >= 250 && sensorValL < 250) { // Only Right garage door open
      msg = out1;
  }
  else {                                      // Both garage doors are closed
      msg = out3;
  }
  Serial.write(msg);                        // Output 3 goes high and 1xGREEN LED is on
  delay(100);
}

Other option is:

const char right = 0x10;                    // bit 4, LED Output 1 on the wireless shield in the house
const char left  = 0x20;                    // bit 5, LED Output 2 on the wireless shield in the house
const char none  = 0x40;                    // bit 6, LED Output 3 on the wireless shield in the house
const char clear = 0x00;                    // msg clear 

int sensorPinR = A0;                       // Right Garage door sensor
int sensorPinL = A1;                       // Left Garage door sensor

int sensorValR = 0;
int sensorValL = 0;

int msg;

void setup() {
  Serial.begin(9600);
}

void loop() {
  sensorValR = analogRead(sensorPinR);
  sensorValL = analogRead(sensorPinL);
  
  msg = clear;

  if (sensorValR >= 250) {  // The Right garage door is open 
      msg &= right;
  }

  if (sensorValL >= 250)  { // The Left garage door is open
      msg &= left;
  }

  if (msg == clear) {
     msg = none;
  }

  Serial.write(msg);                        // Output 3 goes high and 1xGREEN LED is on
  delay(100);
}

Can you understand the 2 programs?

Thanx a lot luisilva. For taking the time and checking it out!

To be honest, the 2 programs you posted seams out of my league atm.

But I tried to upload them to the arduino as you posted them, but then nothing works as it should. (?)

This is the sketch I’m using “to get what I want”.

const char out1 = 0x10;                    // bit 4, LED Output 1 on the wireless shield in the house
const char out2 = 0x20;                    // bit 5, LED Output 2 on the wireless shield in the house
const char out3 = 0x40;                    // bit 6, LED Output 3 on the wireless shield in the house
int sensorPinR = A0;                       // Right Garage door sensor
int sensorPinL = A1;                       // Left Garage door sensor
int sensorValR = 0;
int sensorValL = 0;


void setup() {
  Serial.begin(9600);
}

void loop() {
  sensorValR = analogRead(sensorPinR);
  sensorValL = analogRead(sensorPinL);

  if (sensorValR > 120 && sensorValL > 120)  // Both garage doors open
  {
    Serial.write(out1);                  // Output 1 and output 2 is flashing 2xRED LED
    delay(500);
    Serial.write(out2);
    delay(500);
  }
  else if (sensorValR < 120 && sensorValL > 120)  // Only Left garage door open
  {
    Serial.write(out2);                          // Output 2 goes high and 1xRED LED is on
    delay(1000);
  }
  else if (sensorValR > 120 && sensorValL < 120)  // Only Right garage door open
  {
    Serial.write(out1);                          // Output 1 goes high and 1xRED LED is on
    delay(1000);
  }
  else                                       // Both garage doors are closed
  {
    Serial.write(out3);                        // Output 3 goes high and 1xGREEN LED is on
    delay(20);
  }
}

I don’t know very well where can be the error in my code, but try to change:

int msg;

to:

char msg;

In your code, now you change something. You replace “2xRED LED’s is on” by “is flashing 2xRED LED”.

One of the problems in your first program was the “>=” and “<=”. If you do:

  if(sensorValR >= 250 (...)

in the first if, you can’t do this

  if(sensorValR <= 250 (...)

in the second. Why? Because you don’t “are giving an order that the Arduido can understand what you really want”. Think this way: “What the Arduino will do if the sensorValR it’s EQUAL to 250? The first or the second if?”
This code must be:

  if(sensorValR >= 250 (...)
   (...)
  if(sensorValR < 250 (...)

This was one of my changes. Now you know that if the sensorValR it’s equal to 250 the Arduino will do the first if.

Other thing is that, in this case, you must use the else if. Because if don’t, the Arduino could do what you are saying to be done in the first if and in the third. If you use else you know that only one if will be done.

Other thing is that you are repeating the same couple of instructions:

    Serial.write(...);
    delay(...);

What is in my mind when I’m writing a program is if I use to much copy/paste and I only change a little thing from one block to another, I’m doing something wrong. Is much more easy to read and more understandable if you do the things that are equal only in one place. That’s the reason why I add a new variable msg and in the if's I only change the content of this variable, and at the end I send the message and I wait. (I only send and wait ONE time).

What my first code will do:

  • read the 2 sensors;
  • if both doors are open, the message will be msg = out1 & out2;, that is, turn ON both led;
  • else if only the left door is open the message will be msg = out2;, that is, turn ON led 2;
  • else if only the right door is open the message will be msg = out1;, that is, turn ON led 1;
  • else (that is, both doors are closed) the message will be msg = out3;, that is, turn ON led 3;
  • after this (I know what I must send) I send the message and I wait.

I wait only 100ms because for what I understand in the manual, we need to wait more than 20ms between each time we write, and less than 500ms (after 500ms if the output is on it will turn off). So this value can be changed to some value between 20ms and 500ms.

About your new sketch:

  • read the 2 sensors;
  • if both doors are open (>120), you turn ON the led 1 wait 1/2sec., turn ON the led 2 wait 1/2sec.;
  • else if only the left door is open (>120) you turn ON led 2 1/2sec. and then OFF another 1/2sec.;
  • else if only the right door is open (>120) you turn ON led 1 1/2sec. and then OFF another 1/2sec.;
  • else (that is, both doors are closed or sensor1=120 or sensor2=120 ) you turn ON the led3 and wait 20ms (that is, in the time that the doors are closed the led 3 will be always ON, I believe that, it’s the same that wait any value less than 501ms).

Other thing that it’s not good in your code (neither in mine, because I only remember to point that after post the reply) is that if we need to change the reference value, we need to change it in 6 different places. This is WRONG. We must do the code so we only made the change one time. Like this:

#define REFERENCE 120

const char out1 = 0x10;                    // bit 4, LED Output 1 on the wireless shield in the house
const char out2 = 0x20;                    // bit 5, LED Output 2 on the wireless shield in the house
const char out3 = 0x40;                    // bit 6, LED Output 3 on the wireless shield in the house
int sensorPinR = A0;                       // Right Garage door sensor
int sensorPinL = A1;                       // Left Garage door sensor
int sensorValR = 0;
int sensorValL = 0;


void setup() {
  Serial.begin(9600);
}

void loop() {
  sensorValR = analogRead(sensorPinR);
  sensorValL = analogRead(sensorPinL);

  if (sensorValR > REFERENCE && sensorValL > REFERENCE)  // Both garage doors open
  {
    Serial.write(out1);                  // Output 1 and output 2 is flashing 2xRED LED
    delay(500);
    Serial.write(out2);
    delay(500);
  }
  else if (sensorValR < REFERENCE && sensorValL > REFERENCE)  // Only Left garage door open
  {
    Serial.write(out2);                          // Output 2 goes high and 1xRED LED is on
    delay(1000);
  }
  else if (sensorValR > REFERENCE && sensorValL < REFERENCE)  // Only Right garage door open
  {
    Serial.write(out1);                          // Output 1 goes high and 1xRED LED is on
    delay(1000);
  }
  else                                       // Both garage doors are closed
  {
    Serial.write(out3);                        // Output 3 goes high and 1xGREEN LED is on
    delay(20);
  }
}

You can add a shorter name, or add one reference for left sensor and a different for the right, but the idea is to change the value less times possible.

I’m sorry for my pour English and my big and boring post.

Thanx again, luisilva!

Post may be long, but not booring.
Just happy someone take their time and explain in depth. :slight_smile:

First I tried to alter the

int msg

to

char msg

Not working on the second sketch, but had some effect on the first sketch.
On the first one, the all-closed LED was always on when it should.
And the LED's i wanted flashing when doors is open, did not flash. Just on.
But when they where going to indicate that both doors are open(both flashing), they went black.
I suspect that is because the wireless shield can't broadcast to two outputs at a time.
Have to be programmed as flashing to be working.

My english ain't that good either, so hope you get the essence of what I'm trying to tell you :wink:

Storic, thanks for your answer.
I'm sorry about the errors in the code, but, as I don't have the hardware is more difficult to do it right at the first attempt (and without testing it).

In the first program, this line:

      msg = out1 & out2;

is wrong too! Maybe induced in error by your program, but what I mean to do was:

      msg = out1 | out2;

I get this error because you wrote:

Not working on the second sketch (...)

So, the lines:

      msg &= right;
(...)
      msg &= left;

are wrong too! What I wanted to do was:

      msg |= right;
(...)
      msg |= left;

Once again, sorry about that.

When you said:

(...) the all-closed LED was always on when it should.

It's OK, the program was done like that, but when you say:

And the LED's i wanted flashing when doors is open, did not flash. Just on.

the program was done turning ON the LED's, not flashing it. NOTE that in your first post you said that:

2 LEDs to indicate door open, lights up(independent)

"lights up" not "flash up", and the comments of the program was:

(...) // Output 1 goes high and 2xRED LED's is on

again, LED is ON not LED is flashing. So, I wrote the code to turn on the led on and not to flashing it.

But when they where going to indicate that both doors are open(both flashing), they went black.

I believe that this is because of the "&" instead of the "|", like I said before, so can you test this for me, please?

I suspect that is because the wireless shield can't broadcast to two outputs at a time.

No! The shield can broadcast to (I believe) to many receiver shields, and in any up to 3 outputs (only any of the outputs, or 2 different output, or all the 3 outputs, etc).

Have to be programmed as flashing to be working.

Tell me exactly what you want (and don't change it in the middle of the way :slight_smile: ) that I will try to write the sketch in my way for you (so you can learn a different way, and more correct, I hope).

Seams that there have been a lot of confusion around my little project.
Just have to blame english not being my first language, and not familiar to "code talk". :cold_sweat:

But anyway,
After fixing the code mentioned in your last post, both of your sketches work! Not bad :wink:

I can absolutely see that my comments and first post would lead you to think I wanted the LED's to be on, not flashing.


I can try to describe what I want one more time:
Have 3 LED's in total.
Want them to represent 4 states.
State 1: Doors closed - 1 LED(output 3) constant on.
State 2: Only right door open - 1 RED LED flashing(output 1)
State 3: Only left door open - 1 RED LED flashing(output2)
State 4: Both doors open - 2 RED LED flashing, not at the same time.
Something like this:

Serial.write(out1);                  // Output 1 and output 2 is flashing 2xRED LED one after another
    delay(500);
    Serial.write(out2);
    delay(500);

But don't know if that is the best way to write it.

If you feel like it, I'm a bit curious about how I can expand my sketch to do something more. But I can wait a bit before I confuse you even more! :slight_smile:

This is my approach of the code:

const char msg_right = 0x10;   // bit 4, LED Output 1 on the wireless shield in the house
const char msg_left  = 0x20;   // bit 5, LED Output 2 on the wireless shield in the house
const char msg_none  = 0x40;   // bit 6, LED Output 3 on the wireless shield in the house
const char msg_clear = 0x00;   // msg clear 

int sensorPinR = A0;           // Right Garage door sensor
int sensorPinL = A1;           // Left Garage door sensor

int sensorValR = 0;            // 
int sensorValL = 0;            //

int msg;                       // message that will be sent to RF shield

#define LEFT    0              // 
#define RIGHT   1              //

#define TIME_ON 10              // 10 x 100ms = 1s

unsigned long tick, tock;
int count = TIME_ON;

boolean left_or_right = LEFT;

void setup() {
  Serial.begin(9600);
}

void loop() {
  
  tick = millis() / 100;
  
  sensorValR = analogRead(sensorPinR);
  sensorValL = analogRead(sensorPinL);
  
  if (tick != tock) {
     tock = tick;
     
     msg = msg_clear;
  
     if (sensorValR >= 250 && left_or_right == RIGHT) {        // The Right garage door is open 
         msg = msg_right;
     }

     else if (sensorValL >= 250  && left_or_right == LEFT)  {  // The Left garage door is open
         msg = msg_left;
     }

     if (sensorValR < 250 && sensorValL < 250) {
        msg = msg_none;     
     }
     
     Serial.write(msg);                                        // Output 3 goes high and 1xGREEN LED is on
     
     if (--count < 0) {
        count = TIME_ON;
        
        if (left_or_right == LEFT) {
           left_or_right = RIGHT;
        } 
        else {
           left_or_right = LEFT;
        }
        
     }
  }
  
}

My approach relies in the principle that even when the 2 doors are open only one LED will be light at the time, so, you don’t need to even see if the 2 sensors are HIGH.
I add another change that is not important for this application that is the wait without the delay() function.
Also, you can change very quickly the time that the LED’s are ON and OFF, you only have to change the number in the #define TIME_ON 10 .
This block:

        if (left_or_right == LEFT) {
           left_or_right = RIGHT;
        } 
        else {
           left_or_right = LEFT;
        }

is only to alternate between the 2 LED’s or to make flash only one LED (is the same thing, the only difference is that when only on LED flashes when it turns OFF the other don’t turns ON). This block can be made in a different way, but in this way, is more clear what is happen.

Other things that you could do with your project is to add “noise” when the doors are opened, but in that case you must “sacrifice” one of the LED’s. At least that is done “locally” (in the garage with the Arduino) but I don’t see a great advantage from that.

EDIT: What I did to remove the delay is the same idea that you can find in this sketch/tutorial.

Again, thank you luisilva.

I think I'm gonna need some time to read and understand the entire sketch. But it is working great. :slight_smile:

I have a spare Raspberry Pi laying around, and I have a plan to mount this in the garage.
So that I can connect a webcam and look at the garage doors when I'm not home.
Lately I have been thinking that I may be able to connect the Raspberry Pi to the Arduino, so that I get a status without the webcam.
I also have bought a relay that I'm gonna connect to the garage door motor. So I can just click 2 buttons on the webpage to control the doors.

Is it a lot of work to get the arduino to send a digital output for each door?
Don't need a output to indicate when doors are closed, but would be nice to have a signal from the arduino so I could have a indication of the position.

If I'm not to wrong, it will be possible to just add a output to go HIGH on the 2 different states?

Is it a lot of work to get the arduino to send a digital output for each door?
Don’t need a output to indicate when doors are closed, but would be nice to have a signal from the arduino so I could have a indication of the position.

A digital input of the door state (open/closed) to the Arduino should be simple enough. Just a switch on each door connected to an Arduino pin. Positional information would be more difficult as it would either involve the door mechanism turning a potentiometer to provide a voltage indicating the door position or a series of switches triggered as the door passes them.

Once you have the input sorted then creating an output or outputs based on the input state(s) would be easy.