Stepper Motor Programming

My project is a automatic pet door using an Arduino Uno with Arduino Motor shield. I have a Stepper Motor(12V 200steps/rev) wired to the motor shield. I'm using a passive infrared sensor to activate the motor to open a small door. My problem is in the programming code. I've gotten the motor to rotate when the sensor is High or Low but my problem is getting the stepper motor to stop after a given amount of time, or after a certain number of revolutions. Since its a LOOP the motor will continually rotate without stopping when I want it to, say when the door has been lifted a foot. I want the motor to lift the door a foot when the sensor reads High, then rotate the opposite direction when the sensor reads Low to close the door a foot. So if anyone knows the correct code to control the motor in this way that would be great. Here is the code I have so far.

#include <Stepper.h>

const int stepsPerRevolution = 200; // change this to fit the number of steps per revolution
// for your motor

// initialize the stepper library on the motor shield
Stepper myStepper(stepsPerRevolution, 12,13);

// give the motor control pins names:
const int pwmA = 3;
const int pwmB = 11;
const int brakeA = 9;
const int brakeB = 8;
const int dirA = 12;
const int dirB = 13;
int motion_1 = 2;

void setup() {
Serial.begin(9600);
// set the PWM and brake pins so that the direction pins // can be used to control the motor:
pinMode(pwmA, OUTPUT);
pinMode(pwmB, OUTPUT);
pinMode(brakeA, OUTPUT);
pinMode(brakeB, OUTPUT);
pinMode(motion_1, INPUT);
digitalWrite(pwmA, HIGH);
digitalWrite(pwmB, HIGH);
digitalWrite(brakeA, LOW);
digitalWrite(brakeB, LOW);

// initialize the serial port:
Serial.begin(9600);
// set the motor speed (for multiple steps only):
myStepper.setSpeed(80);
}
void loop()
{
int sensor_1 = digitalRead(motion_1);
if(sensor_1 == HIGH){

myStepper.step(200);
}
int sensor_2 = digitalRead(motion_1);
if(sensor_2 == LOW){

myStepper.step(-200);

}}

BrianWSU:
Here is the code I have so far.

You need a flag to keep track of when the door is open and when it is closed.

...
boolean doorOpen = 0;
 //Assumes the door is closed at start. If not, doorOpen = 1;
void setup() {
 Serial.begin(9600);
 // set the PWM and brake pins so that the direction pins  
// can be used to control the motor:
pinMode(pwmA, OUTPUT);
 pinMode(pwmB, OUTPUT);
 pinMode(brakeA, OUTPUT);
 pinMode(brakeB, OUTPUT);
 pinMode(motion_1, INPUT);
 digitalWrite(pwmA, HIGH);
 digitalWrite(pwmB, HIGH);
 digitalWrite(brakeA, LOW);
 digitalWrite(brakeB, LOW);

 
 // initialize the serial port:
 Serial.begin(9600);
 // set the motor speed (for multiple steps only):
 myStepper.setSpeed(80);
} 
void loop()
{
if( digitalRead(motion_1)== HIGH && doorOpen == 0){ 
//PIR detecting and door not open
  myStepper.step(200);  //adjust to do number of steps to open door
  doorOpen =1;  //remember that the door is open
   }
if( digitalRead(motion_1)==LOW&& doorOpen == 1){ 
//PIR NOT detecting and door open  
  myStepper.step(-200);   //adjust to do number of steps to close door
   doorOpen =0;  //remember that the door is closed
   }
}

Please post your code between code tags, the # key above the input box.

A servo would be a much better option (simpler to program, easier to fit and cheaper) than a stepper motor for opening and closing a door. Keep the stepper and it's driver for another project.

...R

You need to make the motor step when the pin BECOMES HIGH (or LOW) not when it IS HIGH (or LOW). Look at the state change detection example.