On/off button, stepper motor.

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

}

You never use the variable "status" to do anything but control an LED. Also one of the first things you do is freeze the system for an hour with a delay(). How can it do anything while that is going on?

I am rotating the motor for a couple of rotations and then I let it rest for about an hour and then I want it to automatically start over. How come I can start the motor with my button but I can not stop it?

I would love if someone could give me some code to try out.

Disable the motor for a bit, put some print statements in so that you can see what your code is actualy doing and change the 1H delay to 30s while you are debugging.

See Arduino - Button Toggle LED example. It is similar to your requirement. You just need to replace LED code by stepper motor code.

I think I will just get a normal switch connected to the 5v and turn it off manually, no code needed.

I was wondering something else though.

Is there a way to make the stepper motor rotations random in both direction and rotations?

webdiddy:
I think I will just get a normal switch connected to the 5v and turn it off manually, no code needed.

I was wondering something else though.

Is there a way to make the stepper motor rotations random in both direction and rotations?

Yes. Just generate random numbers in your code. Note that these are pseudo random numbers but by using something like the time as a seed the results will appear totally random.