Why the setspeed does not work ?

# include <AccelStepper.h>
const int dirPin= 3;
const int stepPin=2;
long steps = 0;       // The number of steps the motor runs per delay
long accelration = 0; // The current acceleration applied to the motor
long speed = 0 ;      // The current speed applied to the motor 
String Command; // the command from User
/* stop // means stop the motor
  start // means restart the motor
  set accelertion // give an acceleration to the motor
  reset // the motor will back to the initial place
*/
bool Command_received, run_allowance = false;
AccelStepper myStepper(AccelStepper::DRIVER,dirPin,stepPin);


void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  Serial.println("The motor is now activated, please enter Commands");
  myStepper.setMaxSpeed(2000); // limit the value of setspeed (unit step/sec)
  myStepper.setAcceleration(2000); // The defalut value of acceleration (unit step/sec^2)
  //myStepper.disableOutputs(); // provide a low voltage to the driver when no Commands is provided
}

void loop() {
  // put your main code here, to run repeatedly:
  // while(Serial.available()==0){}
 // Serial.println(Serial.available());
  execute_Commadns();
  if(run_allowance){
  myStepper.enableOutputs();
  myStepper.runSpeed();
  Serial.println(myStepper.speed());
  }
  //continuousRun();
}



void execute_Commadns(){
  //Serial.println("enter");
  if (Serial.available()!=0){
    Command = Serial.readString();
    Command.trim();
    Serial.println(Command);
    Command_received = true; // When Command_received is true, User has already typed something.
  if(Command_received){
    //Serial.println('Hi');
    Serial.println(Command);
    if(Command =="stop"){
      //Serial.println("World");
      run_allowance = false;
      myStepper.stop(); // Stop the motor (remember to try to put a parameter into the bracket of stop)
      myStepper.disableOutputs(); //Disconnect the power of the motor driver
      Serial.println("The motor is now stopped");
      Serial.end();
      Serial.begin(9600); // Clean the serial buffer ! 
    }else if(Command =="start"){
      run_allowance = true;
      Serial.println("please enter the number of steps:");
      while(Serial.available()==0){};
      steps=Serial.parseFloat();
      Serial.end();
      Serial.begin(9600);
      Serial.println("please enter the speed of the motor:");
      while(Serial.available()==0){};
      //speed=Serial.parseFloat();
      //Serial.println(speed);
      myStepper.setSpeed(Serial.parseFloat());
      Serial.println("The motor is now started");
      myStepper.move(steps);   // set a target position
      Serial.end();
      Serial.begin(9600);
      Serial.println(myStepper.targetPosition());
      Serial.println(myStepper.speed());
    }else if(Command =="set accelertion"){
      run_allowance = false;      // While updating varibale, disabling the motor
      Serial.println("please enter the value of acceleration");
      accelration = Serial.parseFloat();
      myStepper.setAcceleration(accelration);
      Serial.println("please restart the motor");
    }else if(Command =="reset"){
      myStepper.move(0);
      //myStepper.stop();
      Serial.println("The position of the motor has been reseted, please restart the motor!");
    }else{
      Serial.println("invalid command");
    }
  }
  }
  }

Please ignore all the parts of the code except the part with in the bracket of if(Command =="start"). I try to apply set speed to change the speed of the motor, but it seems this function does not work. The motor speed is different than the one entered from the serial monitor

You will never find the reason if you do so. But luckily you posted the whole sketch. Your problem is in loop:

runSpeed is the only call that may create a step. You must call this as often as possible - at least faster then the steprate you want. But the following Serial.println slows down your loop dramatically - especially with a baudrate of 9600. You must not print with every loop cycle.

N.B. your myStepper.move(xxx) does not make sense if you only use runSpeed(). runSpeed() steps the motor endlessly, without taking care of a target position.

1 Like

There was a very similar problem a few days ago - the problem was a divide-by-zero error in a library function.

And this "similar" one...

Maybe setSpeed does not accept a float? > Syntax >``` >setSpeed(rpms) >``` >### Parameters >* `rpms`: the speed at which the motor should turn in rotations per minute (**positive long**)

My post is referencing the wrong library. See next post.

setSpeed is float in steps per second according to the class reference

1 Like

Issue could be you are setting speed before target position. For const speed movements you need to use moveTo(pos in steps) then set speed, then run speed to position

Ok. I shall read it. My reference was arduino.cc...

thank you, the problem solved if i set speed after applying move

That's for Stepper.h:

the AccelStepper ref would be some doxygen autodoc stuff:

https://www.airspayce.com/mikem/arduino/AccelStepper/classAccelStepper.html#ae79c49ad69d5ccc9da0ee691fa4ca235

sorry, why move() can not be applied togther with runspeed? Both move() and moveTo() just set a target position right ?

Yes, but runSpeed() doesn't care about the target position. So your target position is useless. To automatically stop at the target position you have to use runSpeedToPosition(). But this does not implement acceleration or deceleration, what often leads to problems with loosing steps or not moving at all ( depending on speed and/or your mechanics ).
To use acceleration you have to use run(). But then you must not use setSpeed() but only setMaxSpeed().

2 Likes

Move sets target position relative to where it currently is, move to is relative to start position. Run speed to position runs to target position set by those functions and run speed just runs the speed

1 Like

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