Hello everyone.
I am attempting to program a standa stage which uses a bipolar stepper motor. The motor must change direction once it reaches the end of the stage, the end is indicated by a push button. However it doesn't change direction. Could you please verify whether or not my code is correct?
#include <Stepper.h>
#define BUTTON1 4
#define BUTTON2 5
#define BUZZER 1
#define LED 7
int moveleft = 0;
int moveright = 1;
const int stepsPerRevolution = 48; // change this to fit the number of steps per revolution
// for your motor
int maxsteps = 250000;
// 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 x = 0;
void setup() {
Serial.begin(9600);
// set the PWM and brake pins so that the direction pins // can be used to control the motor:
pinMode(BUTTON1, INPUT);
pinMode(BUTTON2, INPUT);
pinMode(pwmA, OUTPUT);
pinMode(pwmB, OUTPUT);
pinMode(brakeA, OUTPUT);
pinMode(brakeB, OUTPUT);
digitalWrite(pwmA, HIGH);
digitalWrite(pwmB, HIGH);
digitalWrite(brakeA, LOW);
digitalWrite(brakeB, LOW);
pinMode(LED, OUTPUT);
// initialize the serial port:
Serial.begin(9600);
// set the motor speed (for multiple steps only):
myStepper.setSpeed(200);
}
void loop() {
myStepper.step(48);
delay(20);
if (digitalRead(BUTTON1)){
digitalWrite(LED, HIGH);
}
while (moveleft, HIGH){
myStepper.step(48);
delay(20);
if (digitalRead(BUTTON1) && !digitalRead(BUTTON2)){
moveleft = HIGH;
moveright = LOW;
}
}
while (moveright, HIGH){
myStepper.step(-48);
delay(20);
if (digitalRead(BUTTON2) && !digitalRead(BUTTON2)){
moveright = HIGH;
moveleft = LOW;
}
}
}
note that the buzzer isn't currently in use. The only other thing that could explain this problem is that the buttons are broken.