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);
}}