ok, guys, i decide to make this project a little differently, and use dc motors, i found suitable code and I changed code that alow it to work with L293D bridge, but now i trying to introduce HC-SR04 range sensor, and i made something wrong. So maybe someone could help me with this code and try to explain where is the mistake? In the original code was used Sharp GP2D120 IR sensor, so now i want to introduce HC-SR04 in its place.
Edited:
algorithm looks like that. There are two opening/closing modes, manual and automatic, these modes are controled by the swithc. if automatic, dc starts turning and opening doors till doors push 1 button (opened doors sensor), then if closed doors button LOW range sensor cheking pet, if there are no pets, after few seconds doors starts to closing till push 2 button (closed doors sensor). if in opened doors range sensor capture pet, DC do not receiv the signal to start doors closing. In manual mode if pushed 3 button (manual open/close button) dc starts till doors push the 1 button, then push 3 button again and starting doors closing.
Now model works like without range sensor, just open doors till push 1 button, then after few seconds close the doors tll push 2 button, and loop repeats.
const int trigPin=13;
const int echoPin=12;
const int DOORSENSOR_OPEN=5;//input
const int DOORSENSOR_CLOSED=6;//input
const int MANUALMODE_pin=7;//input
const int MANUALOPEN_pin=8;//input
const int OPEN=1;
const int CLOSE=0;
int door_state;
int val;Â Â Â // variable to store the value coming from the IR sensor
int motorpin1 = 3;Â Â Â Â Â Â Â Â Â //define digital output pin no.
int motorpin2 = 4;
//IR emitter is always on (5V)
void setup() {
 Serial.begin(9600);     // set up Serial library at 9600 bps
 Serial.println("Motor test!");
 digitalWrite(14, LOW);
 digitalWrite(15, LOW);
 digitalWrite(16, LOW);
 digitalWrite(17, LOW);
 pinMode(motorpin1,OUTPUT);   Â
 pinMode(motorpin2,OUTPUT);
 door_state=CLOSE;
}
void loop() {
Â
 if(digitalRead(MANUALMODE_pin)==HIGH) //switch set to manual
 {
  delay(10);
  if(digitalRead(MANUALMODE_pin)==HIGH)//debouncing, switch really set to manual
  {
   if(digitalRead(MANUALOPEN_pin)==HIGH) //toggle button is pressed
   {
    delay(10);
    if(digitalRead(MANUALOPEN_pin)==HIGH)//debouncing, toggle button is really pressed
    {Â
     if(door_state==OPEN)Â
     {
      Serial.println("trying to close now ...");
      MoveTray(CLOSE);//close the door
     }
     else if(door_state==CLOSE)Â
     {
      Serial.println("trying to open now ...");
      MoveTray(OPEN);//close the door
     }    Â
    }
   }
  }
 }
 else if(digitalRead(MANUALMODE_pin)==LOW) //switch is set to automatic
 {Â
  delay(10);
  if(digitalRead(MANUALMODE_pin)==LOW) //debouncing, switch really set to automatic
  {
   if(door_state==OPEN)Â
   {
   Serial.println("trying to close now ...");
   MoveTray(CLOSE);//close the door
   }
   else if(door_state==CLOSE)Â
   {
   Serial.println("trying to open now ...");
   MoveTray(OPEN);//close the door
   }  Â
   delay(1000);
  }
 }
}Â
void MoveTray(int openclose)
  {
  long duration, distance;
  digitalWrite(trigPin, LOW); // Added this line
 delayMicroseconds(2); // Added this line
 digitalWrite(trigPin, HIGH);
//Â delayMicroseconds(1000); - Removed this line
 delayMicroseconds(10); // Added this line
 digitalWrite(trigPin, LOW);
 duration = pulseIn(echoPin, HIGH);
 distance = (duration/2) / 29.1;
 Â
  int cat_alert;
 cat_alert=0;Â
  if(openclose==CLOSE) //close the door!
   {
  digitalWrite(motorpin1,LOW); // sukimas atgal
  digitalWrite(motorpin2,HIGH);
//Â Â Â while ( (digitalRead(DOORSENSOR_CLOSED)==LOW) &&Â (analogRead(IR_pin)<200) ); //while the door is not entirely closed and there is no cat
   while ( (digitalRead(DOORSENSOR_CLOSED)==LOW) && cat_alert==0)
    {if(distance > 20)
     {delay(20);
     if(distance > 20)
      {
      cat_alert=1;Â
      digitalWrite(motorpin1,LOW); // stabdymas
      digitalWrite(motorpin2,LOW);
      openclose=OPEN;
      }
     }
    }Â
  Â
   //while the door is not entirely closed and there is no cat
    if(cat_alert==0)
     {
    digitalWrite(motorpin1,LOW); // stabdymas
    digitalWrite(motorpin2,LOW);
    door_state=CLOSE;
    Serial.println("it's closed.");
     }
   Â
   }
 Â
  if(openclose==OPEN) //open the door!
   {
   digitalWrite(motorpin1,HIGH);   // sukimas i prieki
   digitalWrite(motorpin2,LOW);
   while ((digitalRead(DOORSENSOR_OPEN)==LOW) ); //while the door is not entirely open
   digitalWrite(motorpin1,LOW); // stabdymas
   digitalWrite(motorpin2,LOW);
  door_state=OPEN;
  Serial.println("it's open.");
  }
}
original code source: http://www.writtensound.com/arduino/dc_motor_with_stoppers_and_buttons_and_IR.pde
http://www.instructables.com/id/RFID-pet-feeder/?ALLSTEPS