AccelStepper maximum speed cannot be reached[SOLVED]

Need some help, when the A3 phaser is pushed to the highest, the maximum speed of the motor reaches the limit, is there a way to break through the maximum speed? thank you so much! ( I have tried to change the maximum speed and acceleration value, but it does not work)

#include <AccelStepper.h>
#include "Arduino.h"
#include "LiquidCrystal.h"
#include "Button.h"

// Pin Definitions
#define LCD_PIN_RS 8
#define LCD_PIN_E 7
#define LCD_PIN_DB4 3
#define LCD_PIN_DB5 4
#define LCD_PIN_DB6 5
#define LCD_PIN_DB7 6
#define PUSHBUTTON_PIN_2 9
#define STEPPER_PIN_STEP 11
#define STEPPER_PIN_DIR 10
#define POTENTIOMETERSLIDE_5V_PIN_WIPER A3

// Global variables and objects
LiquidCrystal lcd(LCD_PIN_RS, LCD_PIN_E, LCD_PIN_DB4, LCD_PIN_DB5, LCD_PIN_DB6, LCD_PIN_DB7);
Button pushButton(PUSHBUTTON_PIN_2);
AccelStepper stepper(AccelStepper::DRIVER, STEPPER_PIN_STEP, STEPPER_PIN_DIR);
int speed = 0;

// Constants
#define STEPPER_ACCELERATION 3000
#define STEPPER_MAX_SPEED 500
#define STEPPER_MIN_SPEED 0
#define STEPPER_CONTROL_INTERVAL 0.01 

// Variables
bool motorRunning = false;
bool buttonState = false;
bool buttonPreviousState = false;
unsigned long previousControlTime = 0;

void setup() {
  Serial.begin(9600);

  lcd.begin(16, 2);

  stepper.setMaxSpeed(STEPPER_MAX_SPEED);
  stepper.setAcceleration(STEPPER_ACCELERATION);

  lcd.setCursor(0, 0);
  lcd.print("Speed: 0 RPM");
}

void loop() {
  buttonState = pushButton.read();

  if (buttonState != buttonPreviousState) {
    if (buttonState) {
      if (!motorRunning) {
        stepper.setSpeed(0);
        stepper.enableOutputs();
        motorRunning = true;
        lcd.setCursor(0, 1);
        lcd.print("Motor Running    ");
      } else {
        stepper.disableOutputs();
        motorRunning = false;
        lcd.setCursor(0, 1);
        lcd.print("Motor Stopped    ");
      }
    }
  }

  buttonPreviousState = buttonState;

  if (motorRunning) {
    unsigned long currentMillis = millis();
    if (currentMillis - previousControlTime >= STEPPER_CONTROL_INTERVAL) {
      int potentiometerValue = analogRead(POTENTIOMETERSLIDE_5V_PIN_WIPER);
      int speed = map(potentiometerValue, 0, 1023, STEPPER_MIN_SPEED, STEPPER_MAX_SPEED);
      if (speed != stepper.speed()) {
        stepper.setSpeed(speed);
        lcd.setCursor(7, 0);
        lcd.print(speed);
        lcd.print(" RPM      ");
      }

      stepper.runSpeed();

      previousControlTime = currentMillis;
    }
  }
}

The maximum speed is dependent on how often you call the runSpeed() function. Take runSpeed() out of that timing if structure. Make it so that runSpeed() is called every time through loop().

3 Likes

there is a major flaw in your code

you have a define

#define STEPPER_CONTROL_INTERVAL 0.01 

which defines a float-value of 0.01
and then you use this flow-value in this if-condition

    if (currentMillis - previousControlTime >= STEPPER_CONTROL_INTERVAL) {

This condition will always be true at the second iteration because currentMillis and previousControlTime are integers

this means
currentMillis - previousControlTime = 0
or
currentMillis - previousControlTime = 1
currentMillis - previousControlTime = 2
currentMillis - previousControlTime = 3
....
0 >= 0.01 false
1 >= 0.01 true

You have a lot of commands that are slowing down the code-execution
communicating with an LCD takes quite some time

I recommend using the MobaTools because with the MobaTools step-pulse creation is independend of the frequency with which loop is iterating.

And If you want to go up to high step-pulse-frequencies using an ESP32 instead of a "classical" arduino uno or mega

best regards Stefan

1 Like

Yes, I changed the program and it works
Solved my problem, thank you very much master!

Thank you very much for the detailed explanation, now the problem is completely solved, thank you master!

Glad that I could help.

If your question was answered to your satisfaction, please mark the thread as solved so that other members do not waste their time opening the thread wanting to help only to find that the problem has been solved.

1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.