Start and stop stepper with a single button.

Someone told me you’re supposed to be able to start and stop a stepper motor with one button with the “Blink Without Delay” code.

/*
  Blink without Delay

  Turns on and off a light emitting diode (LED) connected to a digital pin,
  without using the delay() function. This means that other code can run at the
  same time without being interrupted by the LED code.

  The circuit:
  - Use the onboard LED.
  - Note: Most Arduinos have an on-board LED you can control. On the UNO, MEGA
    and ZERO it is attached to digital pin 13, on MKR1000 on pin 6. LED_BUILTIN
    is set to the correct LED pin independent of which board is used.
    If you want to know what pin the on-board LED is connected to on your
    Arduino model, check the Technical Specs of your board at:
    https://www.arduino.cc/en/Main/Products

  created 2005
  by David A. Mellis
  modified 8 Feb 2010
  by Paul Stoffregen
  modified 11 Nov 2013
  by Scott Fitzgerald
  modified 9 Jan 2017
  by Arturo Guadalupi

  This example code is in the public domain.

  http://www.arduino.cc/en/Tutorial/BlinkWithoutDelay
*/

// constants won't change. Used here to set a pin number:
const int ledPin =  LED_BUILTIN;// the number of the LED pin

// Variables will change:
int ledState = LOW;             // ledState used to set the LED

// Generally, you should use "unsigned long" for variables that hold time
// The value will quickly become too large for an int to store
unsigned long previousMillis = 0;        // will store last time LED was updated

// constants won't change:
const long interval = 1000;           // interval at which to blink (milliseconds)

void setup() {
  // set the digital pin as output:
  pinMode(ledPin, OUTPUT);
}

void loop() {
  // here is where you'd put code that needs to be running all the time.

  // check to see if it's time to blink the LED; that is, if the difference
  // between the current time and last time you blinked the LED is bigger than
  // the interval at which you want to blink the LED.
  unsigned long currentMillis = millis();

  if (currentMillis - previousMillis >= interval) {
    // save the last time you blinked the LED
    previousMillis = currentMillis;

    // if the LED is off turn it on and vice-versa:
    if (ledState == LOW) {
      ledState = HIGH;
    } else {
      ledState = LOW;
    }

    // set the LED with the ledState of the variable:
    digitalWrite(ledPin, ledState);
  }
}

However, I am too much of a noob at this and I am really helping a friend and if I’m being honest I will not do much more programming after this. I was wondering if someone could be so kind and complete the code for me so that I can stop and start the motor with a button connected to pin2?

Or even better have the code start over when going from stop to start, not just “pausing it”.

The current code:

//Watch Winder with Arduino @ Wouter Pieper

//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()
{

}

void loop()
{

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


    // About 15 rotations Clockwise (1 full rotation is 2050 steps)
  steppermotor.setSpeed(700);    
  StepsRequired  =  61250;
  steppermotor.step(StepsRequired);
  delay(2000);
  
  // About 15 rotations Counterclockwise
  steppermotor.setSpeed(700);    
  StepsRequired  =  - 101250;
  steppermotor.step(StepsRequired);

    // About 15 rotations Clockwise (1 full rotation is 2050 steps)
  steppermotor.setSpeed(600);    
  StepsRequired  =  161250;
  steppermotor.step(StepsRequired);
  delay(2000);
  
  // About 15 rotations Counterclockwise
  steppermotor.setSpeed(450);    
  StepsRequired  =  - 9250;
  steppermotor.step(StepsRequired);
  
  
  // Wait 1 hour before rotation again
  delay(3600000);

  // About 15 rotations Clockwise (1 full rotation is 2050 steps)
  steppermotor.setSpeed(700);    
  StepsRequired  =  100000;
  steppermotor.step(StepsRequired);
  delay(2000);
  
  // About 15 rotations Counterclockwise
  steppermotor.setSpeed(700);    
  StepsRequired  =  - 8250;
  steppermotor.step(StepsRequired);


    // About 15 rotations Clockwise (1 full rotation is 2050 steps)
  steppermotor.setSpeed(700);    
  StepsRequired  =  61250;
  steppermotor.step(StepsRequired);
  delay(2000);
  
  // About 15 rotations Counterclockwise
  steppermotor.setSpeed(700);    
  StepsRequired  =  - 101250;
  steppermotor.step(StepsRequired);

    // 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(550);    
  StepsRequired  =  - 15250;
  steppermotor.step(StepsRequired);


    // Wait 1 hour before rotation again
  delay(3600000);

  
}

Robin2's simple stepper code tutorial has example code for running a stepper non-blocking using millis().

Here is an example showing how to toggle an output state using the state change detection method. Change to toggle the state of a boolean variable instead of an output.

// This is to illustrate using tne state change detectin method to
// toggle the state of a pin (Pin 13, the on board LED)
// a momentary switch (pushbutton) is wired from pin 8 to ground
// by C Goulding aka groundFungus

// this constant won't change:
const int  buttonPin = 8;    // the pin that the pushbutton is attached to
const int ledPin = 13;       // the pin that the LED is attached to
// Variables will change:
boolean buttonState = 0;         // current state of the button
boolean lastButtonState = 0;     // previous state of the button

void setup()
{
   // initialize the button pin as a input with internal pullup enabled
   pinMode(buttonPin, INPUT_PULLUP);
   // initialize the LED as an output:
   pinMode(ledPin, OUTPUT);
   // initialize serial communication:
   Serial.begin(9600);
}

void loop()
{
   static unsigned long timer = 0;
   unsigned long interval = 50;
   if (millis() - timer >= interval)
   {
      timer = millis();
      // read the pushbutton input pin:
      buttonState = digitalRead(buttonPin);
      // compare the buttonState to its previous state
      if (buttonState != lastButtonState)
      {
         if (buttonState == LOW)
         {
            // if the current state is LOW then the button
            // went from off to on:
            digitalWrite(ledPin, !digitalRead(ledPin)); // toggle the output
         }
      }
      // save the current state as the last state,
      //for next time through the loop
      lastButtonState = buttonState;
   }
}

Someone told me you're supposed to be able to start and stop a stepper motor with one button with the "Blink Without Delay" code.

That does not make much sense to me but if you look at the StateChangeDetection example in the IDE you will see you to take action when an input [u]becomes[/u] LOW. If you change the state of the stepper from running to not running and vice versa at that point then you will have done what you described

"Someone told me you're supposed to be able to start and stop a stepper motor with one button with the "Blink Without Delay" code."

You probably use a "toggle" function triggered by a button to switch between run and not run.