# 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
long home_reference = 0 ; // a variable that used to remeber the home of the sensor
String commands; // 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 Switch = true;
bool data_recevied, run_allowance = false;
AccelStepper myStepper(AccelStepper::DRIVER,dirPin,stepPin);
void setup() {
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
myStepper.setCurrentPosition(0);
}
void loop() {
// put your main code here, to run repeatedly:
Command_action();
if(run_allowance)
{
keep_running();
}
}
void Command_action()
{
if (Serial.available()>0)
{
commands = Serial.readString();
commands.trim();
data_recevied = true;
}
if (data_recevied)
{
if(commands == "start")
{
run_allowance = true;
Serial.println("please enter the value of steps (positive number only)");
while(Serial.available() == 0){};
steps = Serial.parseFloat()-1; // The motor will move one additional step to stop
Serial.print("STEP : ");
Serial.println(steps+1);
myStepper.move(steps);
Serial.end();
Serial.begin(9600);
Serial.println("please enter the value of speed");
while(Serial.available() == 0){};
speed = Serial.parseFloat();
Serial.print("SPEED :");
Serial.println(speed);
myStepper.setSpeed(speed);
Serial.println(myStepper.speed()); // Testing only
Serial.println(myStepper.targetPosition()); // Testing only
Serial.println(myStepper.currentPosition()); // Testing only
Serial.println("intial setting is done, the motor start to rotating ");
delay(10000);
Serial.end();
Serial.begin(9600);
data_recevied = false;
}
else if(commands == "stop")
{
run_allowance = false;
home_reference = home_reference + myStepper.currentPosition();
myStepper.setCurrentPosition(0); // Remeber to call this function inside the homing function
myStepper.stop();
Serial.println("The Stepper is now stop and the position has been already reset");
Serial.end();
Serial.begin(9600);
data_recevied = false;
}
else if (commands == "homing" )
{
run_allowance = true;
Switch = false;
myStepper.setSpeed(200);
myStepper.move(-1*home_reference);
//myStepper.setSpeed(200);
Serial.println(myStepper.targetPosition());
Serial.println(" The motor is now homing");
delay(1000);
Serial.end();
Serial.begin(9600);
}
else
{
Serial.println("not available command");
}
}
}
void keep_running()
{
if(run_allowance && abs(myStepper.targetPosition()) >= abs(myStepper.currentPosition()))
{
myStepper.enableOutputs();
myStepper.runSpeed();
Serial.println(myStepper.speed());
Serial.println(myStepper.currentPosition());
}
else
{
run_allowance = false;
myStepper.disableOutputs();
Serial.print("curr POS: ");
Serial.println(myStepper.currentPosition());
if (Switch)
{
home_reference = home_reference + myStepper.currentPosition();
} // Update the home reference
//myStepper.stop();
Switch = true;
myStepper.setCurrentPosition(0);
Serial.println("Action done!");
}
}
I am not sure why, the homing function does not work well, can anyone help me to solve this problem ?