run stepper then change speed direction with button

so I am fairly new to all this, but what I am trying to do is have a stepper motor run continuously in 1 direction until I press 1 of 2 buttons. 1 button to speed up in same direction and another to speed up in reverse. The motor is running a clock, but want 2 buttons (forward and reverse) to increase speed for setting time. I got the buttons working but when I add this

myStepper.setSpeed(1);
myStepper.step(-stepsPerRevolution);
delay(0);

to the bottom for continuous running, the buttons no longer work. But the continuous runs fine. Not sure what I am missing here. I tried putting this at the top of the void loop also tried putting it in the setup still the same.

thanks

#include <Stepper.h>

const int stepsPerRevolution = 200; // change this to fit the number of steps per revolution
// for your motor

const int buttonPin = 2; // the number of the pushbutton pin
const int buttonPin2 = 4; // the number of pushbutton2 pin

// variables will change:
int buttonState = HIGH; // variable for reading the pushbutton status
int buttonState2 = HIGH;

// initialize the stepper library on pins 9 through 12:
Stepper myStepper(stepsPerRevolution, 9, 10, 11, 12);

void setup() {
// set the speed at __ rpm:
myStepper.setSpeed(1);
pinMode(buttonPin, INPUT);
pinMode(buttonPin2, INPUT);
// initialize the serial port:
//Serial.begin(9600);
}

void loop() {
buttonState = digitalRead(buttonPin);
buttonState2 = digitalRead(buttonPin2);

if (buttonState == HIGH) {

// step one revolution in one direction:
//Serial.println("clockwise");
myStepper.setSpeed(100);
myStepper.step(stepsPerRevolution);
delay(0);
}

else if (buttonState2 == HIGH) {

// step one revolution in one direction:
//Serial.println("clockwise");
myStepper.setSpeed(100);
myStepper.step(-stepsPerRevolution);
delay(0);

}

myStepper.setSpeed(1);
myStepper.step(-stepsPerRevolution);
delay(0);

}

First , what is the purpose of delay(0)?

The reason it no longer works is that you when you call the following nothing happens until the motor finishes stepping an entire revolution, which at 1 RPM takes 1 minute. Therefore you are only checking the buttons once per minute.

  myStepper.setSpeed(1);
  myStepper.step(-stepsPerRevolution);

ok thanks that makes sense,

anyway to have it check continuously?

You could try this although it may slightly affect the accuracy of your clock:

#include <Stepper.h>

const int stepsPerRevolution = 200;  // change this to fit the number of steps per revolution
// for your motor

const int buttonPin = 2;     // the number of the pushbutton pin
const int buttonPin2 =  4;      // the number of pushbutton2 pin

// variables will change:
int buttonState = HIGH;         // variable for reading the pushbutton status
int buttonState2 = HIGH;

// initialize the stepper library on pins 9 through 12:
Stepper myStepper(stepsPerRevolution, 9, 10, 11, 12);

void setup() {
  // set the speed at __ rpm:
  myStepper.setSpeed(1);
  pinMode(buttonPin, INPUT);
  pinMode(buttonPin2, INPUT);
  // initialize the serial port:
  //Serial.begin(9600);
}

void loop() {
  buttonState = digitalRead(buttonPin);
  buttonState2 = digitalRead(buttonPin2);
  int stepDirection;
  if (buttonState == HIGH) {

    // step one revolution  in one direction:
    //Serial.println("clockwise");
    myStepper.setSpeed(100);
    stepDirection = 1;
  }

  else if (buttonState2 == HIGH) {

    // step one revolution  in one direction:
    //Serial.println("clockwise");
    myStepper.setSpeed(100);
    stepDirection = -1;
  }
  else {
    myStepper.setSpeed(1);
    stepDirection = -1;
  }
  myStepper.step(stepDirection);
}

The standard Stepper library blocks until all the steps are complete. You could write the program to move one step at a time and do the step timing in your own code.

A better option may be to use the non-blocking runSpeed() function in the AccelStepper library

...R
Stepper Motor Basics
Simple Stepper Code

Thanks a lot, that worked, now just running it for a while to see if it keeps time.

ToddL1962:
You could try this although it may slightly affect the accuracy of your clock:

#include <Stepper.h>

const int stepsPerRevolution = 200;  // change this to fit the number of steps per revolution
// for your motor

const int buttonPin = 2;    // the number of the pushbutton pin
const int buttonPin2 =  4;      // the number of pushbutton2 pin

// variables will change:
int buttonState = HIGH;        // variable for reading the pushbutton status
int buttonState2 = HIGH;

// initialize the stepper library on pins 9 through 12:
Stepper myStepper(stepsPerRevolution, 9, 10, 11, 12);

void setup() {
  // set the speed at __ rpm:
  myStepper.setSpeed(1);
  pinMode(buttonPin, INPUT);
  pinMode(buttonPin2, INPUT);
  // initialize the serial port:
  //Serial.begin(9600);
}

void loop() {
  buttonState = digitalRead(buttonPin);
  buttonState2 = digitalRead(buttonPin2);
  int stepDirection;
  if (buttonState == HIGH) {

// step one revolution  in one direction:
    //Serial.println("clockwise");
    myStepper.setSpeed(100);
    stepDirection = 1;
  }

else if (buttonState2 == HIGH) {

// step one revolution  in one direction:
    //Serial.println("clockwise");
    myStepper.setSpeed(100);
    stepDirection = -1;
  }
  else {
    myStepper.setSpeed(1);
    stepDirection = -1;
  }
  myStepper.step(stepDirection);
}

reichert2011:
Thanks a lot, that worked, now just running it for a while to see if it keeps time.

Also, you might want to use the millis() timer to determine when to step rather than relying on the library. That way you don't have to worry about "drift". For example, you move it one step every 300 milliseconds to get 1 RPM. For 100 RPM you move it 1 step every 3 milliseconds.