# 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