Hello,
I am working on a Project with a stepper motor. a small example of my code is:
// 400 step = 1 revolution -> linear motion.
#include <AccelStepper.h>
double receivedSteps = 0; //Number of steps
double receivedSpeed = 0; //Steps / second
double receivedAcceleration = 0; //Steps / second^2
char receivedCommand;
//-------------------------------------------------------------------------------
int directionMultiplier = 1; // = 1: positive direction, = -1: negative direction
bool newData, runallowed = false; // booleans for new data from serial, and runallowed flag
// direction Digital 9 (CCW), pulses Digital 10 (CLK)
AccelStepper stepper(1, 10, 9);
void setup()
{
Serial.begin(9600); //define baud rate
Serial.println("Demonstration of AccelStepper Library"); //print a messages
Serial.println("Send 'C' for printing the commands.");
//setting up some default values for maximum speed and maximum acceleration
stepper.setMaxSpeed(6200); //SPEED = Steps / second -> default
stepper.setAcceleration(1200); //ACCELERATION = Steps /(second)^2
//aLastState = digitalRead(outputA);
}
void loop()
{
checkSerial(); //check serial port for new commands
RunTheMotor(); //function to handle the motor
}
void RunTheMotor() //function for the motor
{
if (runallowed == true)
{
//stepper.enableOutputs(); //enable pins
stepper.run(); //step the motor (this will step the motor by 1 step at each loop)
}
}
void StopTheMotor() //function for the motor
{
if (runallowed == true)
{
receivedAcceleration = 1200.0;
stepper.setAcceleration(receivedAcceleration);
stepper.stop(); //stop motor
stepper.runToPosition();
//counter = 0;
runallowed = false; //disable running
}
}
void checkSerial() //function for receiving the commands
{
if (Serial.available() > 0) //if something comes from the computer
{
receivedCommand = Serial.read(); // pass the value to the receivedCommad variable
newData = true; //indicate that there is a new data by setting this bool to true
if (newData == true) //we only enter this long switch-case statement if there is a new command from the computer
{
switch (receivedCommand) //we check what is the command
{
case 'D': //P uses the move() function of the AccelStepper library, which means that it moves relatively to the current position.
receivedSteps = Serial.parseFloat(); //value for the steps
receivedSteps = receivedSteps;
receivedSpeed = Serial.parseFloat(); //value for the speed
receivedSpeed = receivedSpeed;
if(receivedSpeed > 6000) {
receivedAcceleration = receivedSpeed*0.2; // update acceleration
stepper.setAcceleration(receivedAcceleration);
}
if(receivedSpeed > 20000) { receivedSpeed = 20000;}
directionMultiplier = 1; //We define the direction
RotateRelative();
break;
case 'U': //N uses the move() function of the AccelStepper library, which means that it moves relatively to the current position.
receivedSteps = Serial.parseFloat(); //value for the steps
receivedSteps = receivedSteps;
receivedSpeed = Serial.parseFloat(); //value for the speed
receivedSpeed = receivedSpeed;
if(receivedSpeed > 6000) {
receivedAcceleration = receivedSpeed*0.2; // update acceleration
stepper.setAcceleration(receivedAcceleration);
}
if(receivedSpeed > 20000) { receivedSpeed = 20000;}
directionMultiplier = -1; //We define the direction
RotateRelative();
break;
//example: N2000 400 - 2000 steps (5 revolution with 400 step/rev microstepping) and 500 steps/s speed; will rotate in the other direction
//In theory, this movement should take 5 second
case 'S': // Stops the motor
StopTheMotor();
break;
case 'E': // Stops the motor but only use for Emergency
stepper.stop();
stepper.setSpeed(0);
break;
default:
break;
}
}
//after we went through the above tasks, newData is set to false again, so we are ready to receive new commands again.
newData = false;
}
}
void RotateRelative()
{
//We move X steps from the current position of the stepper motor in a given direction.
//The direction is determined by the multiplier (+1 or -1)
runallowed = true; //allow running - this allows entering the RunTheMotor() function.
stepper.setMaxSpeed(receivedSpeed); //set speed
stepper.move(directionMultiplier * receivedSteps); //set relative distance and direction
}
my Problem is that i want for any Emergency to stop the Motor without deceleration. if I use steopper.Setspeed(0) the Motor will Stop immediately but in the next command the Motor will try to continue the last instruction than excute the current instruction. is there any possibility to avoid this?
thank you all