Help with my project for garage door arduino

Hi folks,
I need help about programming the arduino.
My project is arduino garage opener. My door has two wings, so I need two limit switches for closed door and two limit switches when a door is open.
I have two DC motors, and 4 relays for motors. I need 4 relays beacause I need to reverse polarity so I could open and close door.
I programmed arduino to wait for input from rf remote, then it should open A wing and then after 3sec open B wing. but when limit switch A is activate it need to stop wing A same for B wing.
My program activate relays but I try to make function for open and close but unsuccessful.
Please I need help

// constants won't change. They're used here to
// set pin numbers:

#define doorOpenSwitchAPin 2 // the number of the close door 1 safety switch
#define doorOpenSwitchBPin 3 // the number of the open door 1 safety switch
#define doorCloseSwitchAPin 4 // the number of the close door 2 safety switch
#define doorCloseSwitchBPin 5 // the number of the open door 2 safety switch
#define rfOpenPin 6 // the number of the RF Supra input OPEN pin
#define rfClosePin 7 // the number of the RF Supra input CLOSE pin
#define doorOpenAPin 8 // the number of output for door1
#define doorOpenBPin 9 // the number of output for door2
#define doorCloseAPin 10 // the number of output for door1
#define doorCloseBPin 11 // the number of output for door2
#define ledPin 13
// variables will change:
int doorOpenSwitchA = 0; // variable for reading the close door 1 safety switch
int doorOpenSwitchB = 0; // variable for reading the open door 1 safety switch
int doorCloseSwitchA = 0; // variable for reading the close door 2 safety switch
int doorCloseSwitchB = 0; // variable for reading the open door 2 safety switch
int rfOpen = 0; // variable for reading RF Supra input OPEN pin
int rfClose = 0; // variable for reading RF Supra input CLOSE pin
//int doorOpenA = 1; // variable for reading output for door1
//int doorOpenB = 1; // variable for reading output for door2
//int doorCloseA = 1; // variable for reading output for door1
//int doorCloseB = 1; // variable for reading output for door2

void setup()
{
pinMode(doorOpenSwitchAPin,INPUT); // Inicijalizuj pin kao ulaz
pinMode(doorOpenSwitchBPin,INPUT); // Inicijalizuj pin kao ulaz
pinMode(doorCloseSwitchAPin,INPUT); // Inicijalizuj pin kao ulaz
pinMode(doorCloseSwitchBPin,INPUT); // Inicijalizuj pin kao ulaz
pinMode(rfOpenPin,INPUT); // Inicijalizuj pin kao ulaz
pinMode(rfClosePin,INPUT); // Inicijalizuj pin kao ulaz
pinMode(doorOpenAPin,OUTPUT); // Inicijalizuj pin kao izlaz
pinMode(doorOpenBPin,OUTPUT); // Inicijalizuj pin kao izlaz
pinMode(doorCloseAPin,OUTPUT); // Inicijalizuj pin kao izlaz
pinMode(doorCloseBPin,OUTPUT); // Inicijalizuj pin kao izlaz
pinMode(ledPin,OUTPUT);
}
// Funkcija za otvaranje kapije
/*
void doorOpen()
{
do
{
digitalWrite(doorOpenAPin, LOW); // Pusti relej za otvaranje kapije A
} // sve dok
while (doorOpenSwitchA != 0); // kranji prekidac za otvaranje kapije A ne prekine funkciju
delay(2000); // sacekaj 2 sek

do
{
digitalWrite(doorOpenBPin, LOW); // Pusti relej za otvaranje kapije B
} // sve dok
while (doorOpenSwitchB != 0); // kranji prekidac za otvaranje kapije B ne prekine funkciju
}
// Funkcija za zatvaranje kapije
void doorClose()
{
do
{
digitalWrite(doorCloseBPin, LOW); // Pusti relej za zatvaranje kapije B
} // sve dok
while (doorCloseSwitchB != 0); // krajnji prekidac za zatvaranje kapije B ne prekine funkciju
delay(2000); // sacekaj 2 sek
do
{
digitalWrite(doorOpenAPin, LOW); // Pusti relej za zatvaranje kapije A
} // sve dok
while (doorCloseSwitchA != 0); // krajnji prekidac za zatvaranje kapije A ne prekine funkciju
}
*/
void loop()
{

digitalWrite(doorOpenAPin, HIGH); // podesene standardne vrednosti za izlaze releja
digitalWrite(doorOpenBPin, HIGH); // podesene standardne vrednosti za izlaze releja
digitalWrite(doorCloseAPin, HIGH); // podesene standardne vrednosti za izlaze releja
digitalWrite(doorCloseBPin, HIGH); // podesene standardne vrednosti za izlaze releja
rfOpen = digitalRead(rfOpenPin);
rfClose = digitalRead(rfClosePin);

if (rfOpen == HIGH) // Ako stigne komanda od supre za otvaranje
{
digitalWrite(ledPin, HIGH);
doorOpen(); // Pokreni funkciju za otvaranje kapije
}
else
{
digitalWrite(ledPin, LOW);
digitalWrite(doorOpenAPin, HIGH);
digitalWrite(doorOpenBPin, HIGH);
}
if (rfClose == HIGH) // Ako stigne komanda od supre za zatvaranje
{
digitalWrite(ledPin, HIGH);
doorClose(); // Pokreni funkciju za zatvaranje kapije
}
else
{
digitalWrite(ledPin, LOW);
digitalWrite(doorCloseAPin, HIGH);
digitalWrite(doorCloseBPin, HIGH);
}
}[/table]

Please use CODE tags when posting code.

In what way were your open/close functions unsuccessful? Does your code show your best attempt? What did you intend them to do, and what did they actually do?

Sorry for the code :-(, this is my firs time to post here.
I mean to call a function in this section:

if (rfOpen == HIGH)                    // Check if RF Remote trigger input for opening gate
 {         
    digitalWrite(ledPin, HIGH);
    doorOpen();                         // Start a function for opening
  }

And the function (I think it shoud be) something like this:

void doorOpen()
{
  do
  {
    digitalWrite(doorOpenAPin, LOW);    // Open relay for opening wing A
  }                                     // until
  while (doorOpenSwitchA != 0);         // limit switch for opening wing A doesnt close
  delay(2000);                          // wait 2-3 sec
  
  do
  {
    digitalWrite(doorOpenBPin, LOW);    // Open relay for opening wing B
  }                                     // until
  while (doorOpenSwitchB != 0);         // limit switch for opening wing B doesnt close
}

I think some thing like that...

Maybe the hole concept I am doing is wrong?

Some better names for the variables would certainly help. Clearly, the pins are all involved in opening or closing the door, or testing whether the door is open or closed. So, adding door to the name doesn't help.

You are clearly not activating a door to open the door. You are activating a relay to open the door. So, for those pins that are used to open or close the door, using relay in the name makes more sense.

For the pins that have limit switches attached, I'd expect to see limit in the name. The A side names of relayA1, limitA1, relayA2, and limitA2 are much easier to understand, and are immediately obvious which direction they are.

You have not told us anything about how the switches are wired. For my money, connecting one leg to the digital pin and the other leg to ground, and turning on the internal pullup resistor is the easiest, most foolproof way to connect the switches.

Break the problem down into 4 parts. The A door is open and needs to move until it is closed. The A door is closed and needs to move until it is open. The B door is open and needs to move until it is closed. The B door is closed and needs to move until it is open.

Write a function to handle each part for the A door. The same function can then be called for the B door, if the input is a pair of pin numbers.

Call the appropriate functions from loop(), when the appropriate input is received (with the appropriate delay, if necessary).

This doesn't look good:

do
  {
    digitalWrite(doorOpenAPin, LOW);    // Pusti relej za otvaranje kapije A
  }                                     // sve dok
  while (doorOpenSwitchA != 0);         // kranji prekidac za otvaranje kapije A ne prekine funkciju

Nothing in that loop will change doorOpenSwitchA, so it will loop forever. You need a digitalread in there.

Thanks PaulS for the advice about names, I will change that.
This is my first project with arduino. I was playing a little with it but nothing special.
All limit switches are made by simple button tutorial just like you said with pullup resistor.
I can make to open a doorA until limit switch is pressed, but I dont want to wait to open one wing to the end and then another.
How to make a function for exemple:

start to open wingA
Until limit switchA
Delay 3 sec
Start to open wingB
Until limit switchB

All limit switches are made by simple button tutorial just like you said with pullup resistor.

External pullup resistors?

How to make a function for exemple:

start to open wingA
Until limit switchA
Delay 3 sec
Start to open wingB
Until limit switchB

This is an overly complicated function. You'll see that there are two nearly identical blocks of code, there, one dealing with door A and one dealing with door B (and associated pins).

The function should open a door, given a relay pin and a limit switch pin.

void openDoor(int relayPin, int limitPin)
{
   digitalWrite(relayPin, HIGH); // Activate the relay to open the door
   while(digitalRead(limitPin) == HIGH)
   {
      // do nothing
   }
   digitalWrite(relayPin, LOW); // Deactivate the relay, shutting off the motor.
}

Some error handling might be necessary. Nothing should happen in this function, for instance, if the door is already open.

There may be some changes needed, depending on how the relays and limit switches are wired. But, it gives you an idea how the function should work.

You could then create another function to open both doors:

void openBoth()
{
   openDoor(relayA1, limitA1);
   delay(3000);
   openDoor(relayB1, limitB1);
}

The close functions would be nearly identical, just involving different pins. In fact, you could probably change the function names from openDoor() and closeDoor() to use just moveDoor(). After all, the idea is that the motor is activated and runs until a limit switch is pressed.

But, for now, stick with different functions for opening and closing the doors.

Ok so I take your advice and repair my program.
Now when I press open on rf remote relayA1 start to open and continiue until limitA1 is open.
And then when limitA1 is close then relayA2 starts to open.
here is new code:

// constants won't change. They're used here to 
// set pin numbers:

#define limitA1Pin 2        // the number of the close door 1 safety switch
#define limitA2Pin 3        // the number of the open door 1 safety switch
#define limitB1Pin 4        // the number of the close door 2 safety switch
#define limitB2Pin 5        // the number of the open door 2 safety switch
#define rfOpenPin 6         // the number of the RF Supra input OPEN pin
#define rfClosePin 7        // the number of the RF Supra input CLOSE pin
#define relayA1Pin 8        // the number of output for door1
#define relayA2Pin 9        // the number of output for door2
#define relayB1Pin 10       // the number of output for door1
#define relayB2Pin 11       // the number of output for door2
#define ledPin 13
// variables will change:
int limitA1 = 0;
int limitA2 = 0;
int limitB1 = 0;
int limitB2 = 0;
int rfOpen = 0; 
int rfClose = 0;
int relayA1 = 0;
int relayA2 = 0;
int relayB1 = 0;
int relayB2 = 0;

void setup()
{
  pinMode(limitA1Pin, INPUT);     // initialize the pin as an input:
  pinMode(limitA2Pin, INPUT);     // initialize the pin as an input:
  pinMode(limitB1Pin, INPUT);     // initialize the pin as an input:
  pinMode(limitB2Pin, INPUT);     // initialize the pin as an input:
  pinMode(rfOpenPin, INPUT);      // initialize the pin as an input:
  pinMode(rfClosePin, INPUT);     // initialize the pin as an input:
  pinMode(relayA1Pin, OUTPUT);    // initialize the pin as an output:
  pinMode(relayA2Pin, OUTPUT);    // initialize the pin as an output:
  pinMode(relayB1Pin, OUTPUT);    // initialize the pin as an output:
  pinMode(relayB2Pin, OUTPUT);    // initialize the pin as an output:
  pinMode(ledPin, OUTPUT);
}

void doorMoveA1()
{
  digitalWrite(relayA1Pin, LOW);
  while(digitalRead(limitA1Pin) == LOW)
  {
    // do nothing
  }
  digitalWrite(relayA1Pin, HIGH);
}
void doorMoveA2()
{
  digitalWrite(relayA2Pin, LOW);
  while(digitalRead(limitA2Pin) == LOW)
  {
    // do nothing
  }
  digitalWrite(relayA2Pin, HIGH);
}
void doorMoveB1()
{
  digitalWrite(relayB1Pin, LOW);
  while(digitalRead(limitB1Pin) == LOW)
  {
    // do nothing
  }
  digitalWrite(relayB1Pin, HIGH);
}
void doorMoveB2()
{
  digitalWrite(relayB2Pin, LOW);
  while(digitalRead(limitB2Pin) == LOW)
  {
    // do nothing
  }
  digitalWrite(relayB2Pin, HIGH);
}
void doorOpen()
{
    doorMoveA1();
    delay(3000);
    doorMoveA2();
}
void doorClose()
{
  
}

void loop()
{
digitalWrite(relayA1Pin, HIGH);
digitalWrite(relayA2Pin, HIGH);
digitalWrite(relayB1Pin, HIGH);
digitalWrite(relayB2Pin, HIGH);
rfOpen = digitalRead(rfOpenPin);
rfClose = digitalRead(rfClosePin);
  if (rfClose == HIGH) {     
     digitalWrite(ledPin, HIGH);
     doorOpen();   
  }
  else {
    // turn LED off:
    digitalWrite(ledPin, LOW);
  }
}

Yeeee
I solve the problem!!!
Function code is below. I will post final code later..

void doorOpen()
{
  digitalWrite(relayA1Pin, LOW);
  while(digitalRead(limitA2Pin) == LOW)
  {
    delay(3000);
    digitalWrite(relayA2Pin, LOW);
    while(digitalRead(limitA1Pin) == LOW)
    {
    }    
    digitalWrite(relayA1Pin, HIGH);
  }
   digitalWrite(relayA2Pin, HIGH);
}

I do not think that your doorOpen() function is good code. It starts one door moving. While that door is moving, it waits 3 seconds, starts the other door moving, waits until the other door is closed, and then checks again to see if the first door is closed.

The problem in the earlier code was that loop() was doing far to much stuff.

void loop()
{
digitalWrite(relayA1Pin, HIGH);
digitalWrite(relayA2Pin, HIGH);
digitalWrite(relayB1Pin, HIGH);
digitalWrite(relayB2Pin, HIGH);
rfOpen = digitalRead(rfOpenPin);

The relay pins are supposed to be set in the proper state as the door is opened or the door is closed. There is no reason to diddle with the relay pins here.

  if (rfClose == HIGH) {     
     digitalWrite(ledPin, HIGH);
     doorOpen();   
  }

If the "close the door" switch is pressed, open the door. I don't get it.

I don't understand why you have 4 doorMove() functions that are nearly identical. Pass one doorMove() function the pin numbers to use, rather than hardcoding them in the doorMoveXN functions.