Unable to change speed of Stepper using AccelStepper

Hello Arduino Community,
I have the following code that is working as expected in both the simulator and real life. I am trying to increase the speed of the stepper motor but I can never achieve that, I have tried all the possibilities. I followed the AccelStepper library guide but it still doesn't work.

Here is the code below, also you can try it directly if you want on the simultaor HERE.


#include <Servo.h>
#include <AccelStepper.h>

#define dirPin 12
#define stepPin 13
#define motorInterfaceType 1

Servo myservo;  // create servo object to control a servo
#define servoPin1 3 //~
#define pushButtonPin 2

int led = 5;         // the PWM pin the LED is attached to
int brightness = 0;  // how bright the LED is
int fadeAmount = 5;  // how many points to fade the LED by

int angle = 179;    // initial angle  for servo (beteen 1 and 179)
int angleStep = 1;
int stepperMotorPosition = 0;

bool raisePlatform = false; // declares a boolean to check its ok to raise platform

const int numberOfPins = 4;  // change this to fit the number of steps per revolution

// initialize the stepper library on pins 8 through 11:
//Stepper myStepper(stepsPerRevolution, 8, 9, 10, 11);
//AccelStepper myStepper(numberOfPins, 8, 9, 10, 11);
AccelStepper myStepper = AccelStepper(motorInterfaceType, stepPin, dirPin);

void setup() {
  myStepper.setCurrentPosition(0);
  // set the speed at 30 rpm:
  myStepper.setMaxSpeed(30);       //set max speed the motor will turn (steps/second)
  //myStepper.setSpeed(100);
  myStepper.setAcceleration(10);  //set acceleration (steps/second^2)

  // declare pin 5 to be an output:
  pinMode(led, OUTPUT);

  // initialize the serial port:
  Serial.begin(9600);          //  setup serial
  myservo.attach(servoPin1);  // attaches the servo on pin 3 to the servo object
  pinMode(pushButtonPin, INPUT_PULLUP);
  //Serial.println("Servo Button ");

}

void loop() {

  if (digitalRead(pushButtonPin) == LOW) {  //switch is ON

    analogWrite(led, brightness); //begin LED dimming code

    // change the brightness for next time through the loop:
    brightness = brightness + fadeAmount;

    // reverse the direction of the fading at the ends of the fade:
    if (brightness <= 0 || brightness >= 100) {
      fadeAmount = -fadeAmount;
    }
    // wait for 20 milliseconds to see the dimming effect
    delay(20);

    //end LED dimming

    if (angle == 0) { //if door is fully open raise platform
      raisePlatform = true;
      myStepper.moveTo(100);
      myStepper.setMaxSpeed(100);//set desired speed of stepper motor
      myStepper.run();
      stepperMotorPosition = myStepper.currentPosition();

      //Serial.println("raising platform ");
      //Serial.println(stepperMotorPosition);

      //Serial.println(stepperMotorPosition);
    }

    if (angle > 0 && angle <= 180) {

      angle = angle - angleStep;

      if (angle < 0) {
        angle = 0;
      }

      else {
        myservo.write(angle); // move the servo to desired angle: open doors
        raisePlatform = false; //Do not raise platform until door is fully open
      }

    }

    //this part to switch off LEDs once doors are open
    if (stepperMotorPosition == 100) {
      brightness = 0;
    }
    //end of turning OFF LEDs

    delay(20); // waits for the servo to get there

  }


  if (digitalRead(pushButtonPin) == HIGH) { //switch is OFF

    if (angle >= 179) {
      analogWrite(led, 0); //begin LED dimming code
    }
    else {
      analogWrite(led, brightness); //begin LED dimming code
    }
    // change the brightness for next time through the loop:
    brightness = brightness + fadeAmount;

    // reverse the direction of the fading at the ends of the fade:
    if (brightness <= 0 || brightness >= 100) {
      fadeAmount = -fadeAmount;
    }
    
    // wait for 20 milliseconds to see the dimming effect
    delay(20);

    myStepper.moveTo(0);
    myStepper.setMaxSpeed(100);//set desired speed of stepper motor
    myStepper.run();
    stepperMotorPosition = myStepper.currentPosition();

    //Serial.println("lowering platform ");
    //Serial.println(stepperMotorPosition);

    //Serial.println("Switch is OFF");
    //Serial.println(stepperMotorPosition);

    // Servo button demo by Robojax.com

    if (stepperMotorPosition == 0) {
      if (angle >= 0 && angle <= 180) {
        angle = angle + angleStep;
        if (angle > 180) {
          angle = 179; //was set to 180 should be same as starting position
        }

        else {
          myservo.write(angle); // move the servo to desired angle
        }
      }
    }
    delay(20); // waits for the servo to get there, to keep same speed this has to be the total
    //of dimming LED delay and Servo delay when switch is in ON position
  }


}

from the run function reference of the AccelStepper library.

Note that each call to run() will make at most one step, and then only when a step is due,

You are misusing the run() function. That function needs to be called very often, like every time through a loop and loop time must be shorter than the time between steps for the desired speed. As it is the run() function is inside of if structures with delays and can only execute when the if structures execute.

Don't use delay() in AccelStepper code if you want the code to perform. For instance:

      delay(20);

      myStepper.moveTo(0);
      myStepper.setMaxSpeed(100);//set desired speed of stepper motor
      myStepper.run();
      stepperMotorPosition = myStepper.currentPosition();

Because of the 20ms delay the loop() rate cant be more the 50 per second, but for a 100 step per second step rate, run() would have to be called at least every 10ms.

Use millis() for timing and run the loop at full speed.
Non-blocking timing tutorials:
Blink without delay().
Beginner's guide to millis().
Several things at a time.

Hello @groundFungus and nice to hear from you again !
I did follow your advice and the use of millis() was productive.
I modfied the code without the use of delay() and it worked (there was some challenge and could still use some improvement, but it works! )
So I was able to change the speed but not by using setSpeed() like the documentation says but by using setMaxSpeed() and thre setAcceleration(), however I can only increase to a certain max level no matter how high I set the setMaxSpeed(). Here is the code you can also try it in the simulator I set up HERE.


#include <Servo.h>
#include <AccelStepper.h>

#define dirPin 12
#define stepPin 13
#define motorInterfaceType 1

Servo myservo;  // create servo object to control a servo
#define servoPin1 3 //~
#define pushButtonPin 2

int led = 5;         // the PWM pin the LED is attached to
int brightness = 0;  // how bright the LED is
int fadeAmount = 5;  // how many points to fade the LED by

int angle = 179;    // initial angle  for servo (beteen 1 and 179)
int angleStep = 1;
int stepperMotorPosition = 0;

bool raisePlatform = false; // declares a boolean to check its ok to raise platform

const int numberOfPins = 4;  // change this to fit the number of steps per revolution

unsigned long startMillis;
unsigned long currentMillis;
unsigned long currentMillisClose;
const unsigned long period = 40;

// initialize the stepper library on pins 8 through 11:
//Stepper myStepper(stepsPerRevolution, 8, 9, 10, 11);
//AccelStepper myStepper(numberOfPins, 8, 9, 10, 11);
AccelStepper myStepper = AccelStepper(motorInterfaceType, stepPin, dirPin);

void setup() {
  myStepper.setCurrentPosition(0);
  // set the speed at 30 rpm:
  //myStepper.setMaxSpeed(3000);       //set max speed the motor will turn (steps/second)
  //myStepper.setSpeed(100);
  myStepper.setAcceleration(40);  //set acceleration (steps/second^2)

  // declare pin 5 to be an output:
  pinMode(led, OUTPUT);

  // initialize the serial port:
  Serial.begin(9600);          //  setup serial
  myservo.attach(servoPin1);  // attaches the servo on pin 3 to the servo object
  pinMode(pushButtonPin, INPUT_PULLUP);
  startMillis = millis();
  myservo.write(angle);
  //Serial.println("Servo Button ");

}

void loop() {

  if (digitalRead(pushButtonPin) == LOW) {  //switch is ON

    currentMillis = millis();  //get the current time
  if (currentMillis - startMillis >= period)  //test whether the period has elapsed
  {
    analogWrite(led, brightness);    //set the brightness of the LED
    brightness = brightness + fadeAmount;
    if (brightness <= 0 || brightness >= 120) {
      fadeAmount = -fadeAmount;
    }
    startMillis = currentMillis;  //IMPORTANT to save the start time of the current LED brightness
  
  if (angle > 0 && angle <= 180) {

      angle = angle - angleStep;

      if (angle < 0) {
        angle = 0;
      }

      else {
        myservo.write(angle); // move the servo to desired angle: open doors
        raisePlatform = false; //Do not raise platform until door is fully open
      }

    }
  
  }
    // wait for 20 milliseconds to see the dimming effect
    //delay(20);

    //end LED dimming

    if (angle == 0) { //if door is fully open raise platform
      raisePlatform = true;
      myStepper.moveTo(100);
      myStepper.setMaxSpeed(300.0);//set desired speed of stepper motor
      myStepper.run();
      stepperMotorPosition = myStepper.currentPosition();

      //Serial.println("raising platform ");
      //Serial.println(stepperMotorPosition);

      //Serial.println(stepperMotorPosition);
    }

    

    //this part to switch off LEDs once doors are open
    if (stepperMotorPosition == 100) {
      brightness = 0;
    }
    //end of turning OFF LEDs

    //delay(20); // waits for the servo to get there

  }


  if (digitalRead(pushButtonPin) == HIGH) { //switch is OFF

    startMillis = millis();
    // wait for 20 milliseconds to see the dimming effect
    //delay(20);

    myStepper.moveTo(0);
    myStepper.setMaxSpeed(300.0);//set desired speed of stepper motor
    myStepper.run();
    stepperMotorPosition = myStepper.currentPosition();
    currentMillisClose = millis(); 

    if (angle >= 179) {
      analogWrite(led, 0); //begin LED dimming code
    }
    else {
       //get the current time    
      //analogWrite(led, brightness); //begin LED dimming code
      if (startMillis - currentMillis >= period){
        analogWrite(led, brightness); //begin LED dimming code
                // change the brightness for next time through the loop:
    brightness = brightness + fadeAmount;
        
        // reverse the direction of the fading at the ends of the fade:
    if (brightness <= 0 || brightness >= 120) {
      fadeAmount = -fadeAmount;
    }
    currentMillis = startMillis;
    if (stepperMotorPosition == 0) {
      if (angle >= 0 && angle <= 180) {
        angle = angle + angleStep;
        if (angle > 180) {
          angle = 179; //was set to 180 should be same as starting position
        }

        else {
          myservo.write(angle); // move the servo to desired angle
        }
      }

      }

    }

    //Serial.println("lowering platform ");
    //Serial.println(stepperMotorPosition);

    //Serial.println("Switch is OFF");
    //Serial.println(stepperMotorPosition);

    // Servo button demo by Robojax.com

    
    }
    //delay(20); // waits for the servo to get there, to keep same speed this has to be the total
    //of dimming LED delay and Servo delay when switch is in ON position
  }


}

Move myStepper.run() to the top of the loop() function. There is no reason to have it embedded within conditional statements.

+1 for what @ToddL1962 wrote.

The AccelStepper library has 2 types of motion. Constant speed and acceleration. The setSpeed function is for constant speed and setMaxSpeed is for acceleeated motion.