AccelStepper Library, Can't Change Speed of Stepper Motor

I'm a novice coder. This project allows three buttons to rotate a stepper motor clockwise, counterclockwise, and stop it. I can also change the speed with a potentiometer. I have that code working just fine. I've decided to add two more buttons that will advance the stepper by only one fixed increment in either direction. I have that working fine, as well. The problem is that I can't seem to change the speed of these new fixed increment distance movements. They rotate much slower than the three primary buttons (stop and rotation in both directions).

I'm not sure if the original code is somehow taking control of the speed for these new movements, or if I'm not controlling their speed correctly. Maybe they belong in a new function and/or the scope is wrong?

Here is the code/feature I'm trying to add:

if (digitalRead(LEFT_ONE_PIN) == 0) {
    stepper1.move(20);
    stepper1.runToPosition();
    stepper1.setSpeed(10000);
    stepper1.runSpeed();
  }

This runs at the same speed regardless of whether setSpeed and runSpeed included or deleted.

I've tried many code variations using the AccelStepper library's functions to no avail.

Here is the AccelSteppr info/fuctions page: LINK

Here is the full code:

// Example5 code for Brian Schmalz's Easy Driver Example page
// http://www.schmalzhaus.com/EasyDriver/Examples/EasyDriverExamples.html

#include <AccelStepper.h>

// Define the stepper and the pins it will use
AccelStepper stepper1(AccelStepper::DRIVER, 9, 8);

// Define our three input button pins
#define  LEFT_PIN  4
#define  STOP_PIN  3
#define  RIGHT_PIN 2
#define  LEFT_ONE_PIN 5
#define  RIGHT_ONE_PIN 6

// Define our analog pot input pin
#define  SPEED_PIN 0

// Define our maximum and minimum speed in steps per second (scale pot to these)
#define  MAX_SPEED 500
#define  MIN_SPEED 0.01

void setup() {
  // The only AccelStepper value we have to set here is the max speed, which is higher than we'll ever go 
  stepper1.setMaxSpeed(10000.0);
  
  // Set up the three button inputs, with pullups. This simply diverts the current to a resistor in the Arduino when there is no signal. "When connecting a sensor to a pin configured with INPUT_PULLUP, the other end should be connected to ground. In the case of a simple switch, this causes the pin to read HIGH when the switch is open, and LOW when the switch is pressed."
  pinMode(LEFT_PIN, INPUT_PULLUP);
  pinMode(STOP_PIN, INPUT_PULLUP);
  pinMode(RIGHT_PIN, INPUT_PULLUP);
  pinMode(RIGHT_ONE_PIN, INPUT_PULLUP);
  pinMode(LEFT_ONE_PIN, INPUT_PULLUP);
}

void loop() {
  static float current_speed = 0.0;         // Holds current motor speed in steps/second
  static int analog_read_counter = 1000;    // Counts down to 0 to fire analog read
  static char sign = 0;                     // Holds -1, 1 or 0 to turn the motor on/off and control direction
  static int analog_value = 0;              // Holds raw analog value.
  
  // If a switch is pushed down (low), set the sign value appropriately
  if (digitalRead(LEFT_PIN) == 0) {
    sign = 1;
  }
  else if (digitalRead(RIGHT_PIN) == 0) {    
    sign = -1;
  }
  else if (digitalRead(STOP_PIN) == 0) {
    sign = 0;
  }

  if (digitalRead(LEFT_ONE_PIN) == 0) {
    stepper1.move(20);
    stepper1.runToPosition();
    stepper1.setSpeed(10000);
    stepper1.runSpeed();
  }
 
  // We only want to read the pot every so often (because it takes a long time we don't
  // want to do it every time through the main loop).  
  if (analog_read_counter > 0) {
    analog_read_counter--;
  }
  else {
    analog_read_counter = 3000;
    // Now read the pot (from 0 to 1023)
    analog_value = analogRead(SPEED_PIN);
    // Give the stepper a chance to step if it needs to
    stepper1.runSpeed();
    //  And scale the pot's value from min to max speeds
    current_speed = sign * (((analog_value/1023.0) * (MAX_SPEED - MIN_SPEED)) + MIN_SPEED);
    // Update the stepper to run at this new speed
    stepper1.setSpeed(current_speed);
  }

  // This will run the stepper at a constant speed
  stepper1.runSpeed();
}

Simplify the code in loop() to the following and see what happens

void loop() {
  static float current_speed = 0.0;         // Holds current motor speed in steps/second
  static int analog_read_counter = 1000;    // Counts down to 0 to fire analog read
  static char sign = 0;                     // Holds -1, 1 or 0 to turn the motor on/off and control direction
  static int analog_value = 0;              // Holds raw analog value.
  

  if (digitalRead(LEFT_ONE_PIN) == 0) {
    stepper1.move(20);
    // stepper1.runToPosition();
    stepper1.setSpeed(10000);
    stepper1.runSpeed();
  }
 
  
}

...R

Thanks @Robin2, that was helpful advice. When I stripped the code down to the following, it does work as expected, rotating at an appropriate speed and distance.

#include <AccelStepper.h>

AccelStepper stepper(AccelStepper::DRIVER, 9, 8);

bool flag = false;

#define  LEFT_ONE_PIN 5

void setup()
{
  pinMode(LEFT_ONE_PIN, INPUT_PULLUP);

}
void loop()
{
  if (digitalRead(LEFT_ONE_PIN) == 0)
  {
    stepper.setMaxSpeed(200.0);
    stepper.setAcceleration(500.0);
    stepper.move(200);
    flag = false;
  }

  stepper.run();
}

I'm still having trouble integrating this feature into the previous code. I'm new to programming with Arduino, so I'm not quite sure what approach to take. I tried at first to simply add this feature/function as one of the "else if" statements. But this didn't work. The stepper motor didn't move (or maybe it moved one position). I think the issue is that the new function executes "stepper.run();" on its own and then the old code executes the similar function "stepper1.runSpeed();" after at the bottom of the void loop() function. The new function won't run if it uses "runSpeed" so I need a workaround.

A second issue is that my new function won't work if the "stepper1.run();" is placed alongside the other functions. I guess the scope is important here?

For example, this doesn't work. Instead of moving 200 positions, it simply moves for as long as the pin is activated.

 if (digitalRead(LEFT_ONE_PIN) == 0)
  {
    void test()
    {
      stepper1.setMaxSpeed(200.0);
      stepper1.setAcceleration(500.0);
      stepper1.move(200);
      flag = false;
      stepper1.run();
    }
 
  }

Here are a few approaches I'm thinking of, and have tried, but don't work. I'm probably breaking some basic Arduino coding rules.

  1. Placing the new function in the old code as an "if else" statement along with the others, but allowing each if statement to execute its own run() or runSpeed(), using a nested function to allow run/runSpeed to stay one level (scope?) outside the other functions. But I get errors when I try to do this:
 if (digitalRead(LEFT_ONE_PIN) == 0)
  {
    void test()
    {
      stepper1.setMaxSpeed(200.0);
      stepper1.setAcceleration(500.0);
      stepper1.move(200);
      flag = false;
    }
    stepper1.run();
  }
  1. Simply adding the new code/feature as an entirely new function outside of the void loop function. In this example, the old buttons work as expected, but the new buttons don't move the motor at all.
#include <AccelStepper.h>

// Define the stepper and the pins it will use
AccelStepper stepper1(AccelStepper::DRIVER, 9, 8);

bool flag = false;

// Define our three input button pins
#define  LEFT_PIN  4
#define  STOP_PIN  3
#define  RIGHT_PIN 2
#define  LEFT_ONE_PIN 5
#define  RIGHT_ONE_PIN 6

// Define our analog pot input pin
#define  SPEED_PIN 0

// Define our maximum and minimum speed in steps per second (scale pot to these)
#define  MAX_SPEED 500
#define  MIN_SPEED 0.01

void setup() {
  // The only AccelStepper value we have to set here is the max speeed, which is higher than we'll ever go
  stepper1.setMaxSpeed(10000.0);

  // Set up the three button inputs, with pullups. This simply diverts the current to a resistor in the Arduino when there is no signal. "When connecting a sensor to a pin configured with INPUT_PULLUP, the other end should be connected to ground. In the case of a simple switch, this causes the pin to read HIGH when the switch is open, and LOW when the switch is pressed."
  pinMode(LEFT_PIN, INPUT_PULLUP);
  pinMode(STOP_PIN, INPUT_PULLUP);
  pinMode(RIGHT_PIN, INPUT_PULLUP);
  pinMode(RIGHT_ONE_PIN, INPUT_PULLUP);
  pinMode(LEFT_ONE_PIN, INPUT_PULLUP);
}

void loop() {
  static float current_speed = 0.0;         // Holds current motor speed in steps/second
  static int analog_read_counter = 1000;    // Counts down to 0 to fire analog read
  static char sign = 0;                     // Holds -1, 1 or 0 to turn the motor on/off and control direction
  static int analog_value = 0;              // Holds raw analog value.

  // If a switch is pushed down (low), set the sign value appropriately
  if (digitalRead(LEFT_PIN) == 0) {
    sign = 1;
  }
  else if (digitalRead(RIGHT_PIN) == 0) {
    sign = -1;
  }
  else if (digitalRead(STOP_PIN) == 0) {
    sign = 0;
  }

  // We only want to read the pot every so often (because it takes a long time we don't
  // want to do it every time through the main loop).
  if (analog_read_counter > 0) {
    analog_read_counter--;
  }
  else {
    analog_read_counter = 3000;
    // Now read the pot (from 0 to 1023)
    analog_value = analogRead(SPEED_PIN);
    // Give the stepper a chance to step if it needs to
    stepper1.runSpeed();
    //  And scale the pot's value from min to max speeds
    current_speed = sign * (((analog_value / 1023.0) * (MAX_SPEED - MIN_SPEED)) + MIN_SPEED);
    // Update the stepper to run at this new speed
    stepper1.setSpeed(current_speed);
  }

  // This will run the stepper at a constant speed
  stepper1.runSpeed();
}

void oneStepButtons()
{
  if (digitalRead(LEFT_ONE_PIN) == 0)
  {
    stepper1.setMaxSpeed(200.0);
    stepper1.setAcceleration(500.0);
    stepper1.move(200);
    flag = false;
  }

  stepper1.run();
}

However, if I simply replace the old "void loop()" with my new feature, the new feature works just fine.

#include <AccelStepper.h>

// Define the stepper and the pins it will use
AccelStepper stepper1(AccelStepper::DRIVER, 9, 8);

bool flag = false;

// Define our three input button pins
#define  LEFT_PIN  4
#define  STOP_PIN  3
#define  RIGHT_PIN 2
#define  LEFT_ONE_PIN 5
#define  RIGHT_ONE_PIN 6

// Define our analog pot input pin
#define  SPEED_PIN 0

// Define our maximum and minimum speed in steps per second (scale pot to these)
#define  MAX_SPEED 500
#define  MIN_SPEED 0.01

void setup() {
  // The only AccelStepper value we have to set here is the max speeed, which is higher than we'll ever go
  stepper1.setMaxSpeed(10000.0);

  // Set up the three button inputs, with pullups. This simply diverts the current to a resistor in the Arduino when there is no signal. "When connecting a sensor to a pin configured with INPUT_PULLUP, the other end should be connected to ground. In the case of a simple switch, this causes the pin to read HIGH when the switch is open, and LOW when the switch is pressed."
  pinMode(LEFT_PIN, INPUT_PULLUP);
  pinMode(STOP_PIN, INPUT_PULLUP);
  pinMode(RIGHT_PIN, INPUT_PULLUP);
  pinMode(RIGHT_ONE_PIN, INPUT_PULLUP);
  pinMode(LEFT_ONE_PIN, INPUT_PULLUP);
}


void loop()
{
  if (digitalRead(LEFT_ONE_PIN) == 0)
  {
    stepper1.setMaxSpeed(200.0);
    stepper1.setAcceleration(500.0);
    stepper1.move(200);
    flag = false;
  }

  stepper1.run();
}

In this case, maybe I need to not use "stepper1" and declare a new function(?) name. But I'm not sure how to do that. For example, this doesn't work. The old buttons work as expected but the new button feature does nothing.

// Define the stepper and the pins it will use
AccelStepper stepper1(AccelStepper::DRIVER, 9, 8);
AccelStepper stepper2(AccelStepper::DRIVER, 9, 8);

forestkelley:
I'm still having trouble integrating this feature into the previous code.

If part of the solution is to discard a huge chunk of the code (as I suggested) then I don't think integrating will be much use.

Much better to define your requirement clearly and build forwards from the part that works.

You have several programs in Reply #2. Which one should I look at?

...R

Ok, I think I'm getting closer. I've decided to create two separate functions, one for the buttons that require run() and one for those that require runSpeed(). Each function (buttonsOne and buttonsTwo) works if I delete the other, but if they are both present, as below, buttonsTwo won't work (meaning, the stepper doesn't rotate). How is buttonsOne function creating an issue for buttonsTwo? Arent each function's parameters (I'm not sure what the right term is) scoped to itself so they shouldn't interfere with each other?

#include <AccelStepper.h>

AccelStepper stepper1(AccelStepper::DRIVER, 9, 8);

bool flag = false;

#define  LEFT_PIN  4
#define  STOP_PIN  3
#define  RIGHT_PIN 2
#define  LEFT_ONE_PIN 5
#define  RIGHT_ONE_PIN 6

void setup() {
  #define  SPEED_PIN 0
  #define  MAX_SPEED 500
  #define  MIN_SPEED 0.01

  stepper1.setMaxSpeed(10000.0);

  pinMode(LEFT_PIN, INPUT_PULLUP);
  pinMode(STOP_PIN, INPUT_PULLUP);
  pinMode(RIGHT_PIN, INPUT_PULLUP);
  pinMode(RIGHT_ONE_PIN, INPUT_PULLUP);
  pinMode(LEFT_ONE_PIN, INPUT_PULLUP);
}

void loop() {
  buttonsOne();
  buttonsTwo();
}

void buttonsTwo() { // X number of steps forward and reverse
  if (digitalRead(LEFT_ONE_PIN) == 0)
  {
    stepper1.setMaxSpeed(200.0);
    stepper1.setAcceleration(500.0);
    stepper1.move(200);
    flag = false;
  }
  stepper1.run();
}

void buttonsOne() { // Forward, Reverse, Stop
  static float current_speed = 0.0;         // Holds current motor speed in steps/second
  static int analog_read_counter = 1000;    // Counts down to 0 to fire analog read
  static char sign = 0;                     // Holds -1, 1 or 0 to turn the motor on/off and control direction
  static int analog_value = 0;              // Holds raw analog value.

  if (digitalRead(LEFT_PIN) == 0) {
    sign = 1;
  }
  else if (digitalRead(RIGHT_PIN) == 0) {
    sign = -1;
  }
  else if (digitalRead(STOP_PIN) == 0) {
    sign = 0;
  }

  if (analog_read_counter > 0) {
    analog_read_counter--;
  }
  else {
    analog_read_counter = 3000;
    // Now read the pot (from 0 to 1023)
    analog_value = analogRead(SPEED_PIN);
    // Give the stepper a chance to step if it needs to
    stepper1.runSpeed();
    //  And scale the pot's value from min to max speeds
    current_speed = sign * (((analog_value / 1023.0) * (MAX_SPEED - MIN_SPEED)) + MIN_SPEED);
    // Update the stepper to run at this new speed
    stepper1.setSpeed(current_speed);
  }

  // This will run the stepper at a constant speed
  stepper1.runSpeed();
}