Hey guys!
I'm controlling a stepper motor.
I wanted to use a push-button to turn the motor on and off.
The button works to turn it on but when pressing the button again to turn it off nothing happens. Would someone please take a look at the code and help me out?
//BUTTON
int button = 2;
int led = 13;
int status = false;
//END BUTTON
//Include the Arduino Stepper Library
#include <Stepper.h>
// Number of steps per internal motor revolution
const float STEPS_PER_REV = 32;
// Number of Steps Required
int StepsRequired;
// The pins used are 8,9,10,11
// Connected to ULN2003 Motor Driver In1, In2, In3, In4
// Pins entered in sequence 1-3-2-4 for proper step sequencing
Stepper steppermotor(STEPS_PER_REV, 8, 10, 9, 11);
void setup()
{
//BUTTON
pinMode(led, OUTPUT);
pinMode(button, INPUT_PULLUP); // set the internal pull up resistor, unpressed button is HIGH
}
//END BUTTON
// Nothing (Stepper Library sets pins as outputs)
void loop(){
//BUTTON
//a) if the button is not pressed the false status is reversed by !status and the LED turns on
//b) if the button is pressed the true status is reveresed by !status and the LED turns off
if (digitalRead(button) == true) {
status = !status;
digitalWrite(led, status);
} while(digitalRead(button) == true);
delay(50); // keeps a small delay
//END BUTTON
\
// About 15 rotations Clockwise (1 full rotation is 2050 steps)
steppermotor.setSpeed(500);
StepsRequired = 161250;
steppermotor.step(StepsRequired);
delay(2000);
// About 15 rotations Counterclockwise
steppermotor.setSpeed(500);
StepsRequired = - 161250;
steppermotor.step(StepsRequired);
// Wait 1 hour before rotation again
delay(100000);
}