Controlling Stepper with switch and pushbutton

I have a sketch that I wrote to power a stepper motor so that it goes clockwise when a switch is flipped, and counter clockwise when the switch is flipped to the other position. It also has a 10K pot to adjust the speed. I have also added a push button to make the motor go to top speed in either direction when held down. This is to raise and lower the head on a milling machine. I have the code working, but when I push the pushbutton, the motor kind of jumps when it completes a revolution at the high speed. Also, the motor seems to keep spinning for about a 1/2 second after the release of the pushbutton. I am new at this, so any help would be appreciated.

stepper_speedControl_with_buttons_with_Function.ino (2.37 KB)

For short programs it is much easier for everyone if you include them in a Post like this

/*
 Stepper Motor Control - speed control with buttons

 This program drives a bipolar stepper motor.
 The motor is attached to digital pins 8 - 11 of the Arduino.
 A potentiometer is connected to analog input 0.

 A On/Off/On switch is used to run the up and down motions.  The motor runs
 until the switch is returned to off.  A momentary button is used to increase the
 speed of the motor temporarily.
 
 Created 3 May. 2019
 
 */

#include <Stepper.h>

const int stepsPerRevolution = 6400;                       // change this for your motor

#define UP_PIN 2
#define DOWN_PIN 3
#define FAST 4

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

void setup() {
  pinMode(UP_PIN, INPUT_PULLUP);                           //set up the switch positions as inputs with pullups
  pinMode(DOWN_PIN, INPUT_PULLUP);
  pinMode(FAST, INPUT_PULLUP);                             //a button is used to temporarily increase speed
}

void loop() {
  if (digitalRead(UP_PIN)==0){                             //run motor clockwise
    int sensorReading = analogRead(A0);                    //read the sensor value: 
    int motorSpeed = map(sensorReading, 0, 1023, 0, 255);  //map it to a range from 0 to 255:
      if (motorSpeed > 0)                                  //set the motor speed:
        myStepper.setSpeed(motorSpeed);
        myStepper.step(stepsPerRevolution / 100);
  } 
    
  if (digitalRead(DOWN_PIN)==0) {                          //run motor counter clockwise
    int sensorReading = analogRead(A0);                    //read the sensor value: 
    int motorSpeed = map(sensorReading, 0, 1023, 0, 255);  //map it to a range from 0 to 255:
      if (motorSpeed > 0)                                  //set the motor speed:
         myStepper.setSpeed(motorSpeed);
         myStepper.step(-stepsPerRevolution / 100);        //step 1/100 of a revolution:
  }
   
  if (digitalRead(FAST)==0&&digitalRead(UP_PIN)==0&&digitalRead(DOWN_PIN)==1)       //rapid traverse up 
  { 
    myStepper.setSpeed(stepsPerRevolution);
    myStepper.step(stepsPerRevolution);
  }

   else if (digitalRead(FAST)==0&&digitalRead(UP_PIN)==1&&digitalRead(DOWN_PIN)==0) //rapid traverse down
  { 
    myStepper.setSpeed(stepsPerRevolution);
    myStepper.step(-stepsPerRevolution);
  }  

}

I suspect the problem is your use of the Stepper library because its moves block the Arduino until they complete.

The AccelStepper library allows for non-blocking moves with the functions run() and runSpeed(). Its usage is different from the standard Stepper library so be sure to study the examples.

...R
Stepper Motor Basics
Simple Stepper Code

Thanks. This was my first post and i'm not sure how to get it in the format that you suggest. I will do more research. I looked at the AccelStepper.h library but did not find very many examples of how to execute the code.

Growls:
I looked at the AccelStepper.h library but did not find very many examples of how to execute the code.

I don't think I understand.

Have you tried the examples?

...R