Hello,
Thanks for reading sorry if the formatting is off or if this is in the wrong subcategory but here it is.
I am trying to program a stepper motor where I input x. which is the target position. I would like to be able to change the target while still moving and have the change be instantaneous i.e without pause and smooth acceleration. Sorry if this isn't clear. I can answer any question anybody has.
#include <FlexyStepper.h>
//
//assigning pins
const int Step_Pin = 2;
const int Direction_Pin = 4;
int x; //position
//create the motor object
//
FlexyStepper stepper;
void setup() {
Serial.begin(9600);
stepper.connectToPins(Step_Pin, Direction_Pin);
stepper.setSpeedInStepsPerSecond(1000);
stepper.setAccelerationInStepsPerSecondPerSecond(500);
}
//void serialEvent(){
//x=Serial.parseInt();
}
void loop() {
//set speed and accleration rates
if (Serial.available() != 0) {
x = Serial.parseInt();
stepper.setTargetPositionInSteps(x);
}
while (!stepper.motionComplete()) {
stepper.processMovement();
long cur_pos=stepper.getCurrentPositionInSteps();
if (cur_pos == x) {
if (serialEvent()!= 0) {
exit
}
}
}
}
//need to break loop to input new value quickly
//pauses between serial inputs.
The motor should be running continuously. The direction of rotation is the sign of my input which is from serial.parseInt() so just a number and negative sign. Same direction then it should stop sooner/later. Opposite direction to an immediate switch in direction. I amended the code to comment out the additions that didn't work. Namely the serial.Event() function.
#include <FlexyStepper.h>
//
//assigning pins
const int Step_Pin = 2;
const int Direction_Pin = 4;
int x; //position
//create the motor object
//
FlexyStepper stepper;
void setup() {
Serial.begin(9600);
stepper.connectToPins(Step_Pin, Direction_Pin);
stepper.setSpeedInStepsPerSecond(1000);
stepper.setAccelerationInStepsPerSecondPerSecond(500);
}
//void serialEvent(){
//x=Serial.parseInt();
//}
void loop() {
//set speed and accleration rates
if (Serial.available() != 0) {
x = Serial.parseInt();
stepper.setTargetPositionInSteps(x);
}
while (!stepper.motionComplete()) {
stepper.processMovement();
long cur_pos=stepper.getCurrentPositionInSteps();
if (cur_pos == x) {
// if (serialEvent()!= 0) {
// exit
// }
}
}
}
//need to break loop to input new value quickly
//pauses between serial inputs.
// change move distance after. i.e x*100000 to avoid sinusoidal acceleration
//is stepper.moveRelativeInSteps a blocking function? -> yes, have to change it out.
//may have to switch back to position based control
Very often the following code will limit you to one character of keyboard input. Since that is not what you want, spend some time studying the Serial Basics link posted earlier.
if (Serial.available() != 0) {
x = Serial.parseInt();
stepper.setTargetPositionInSteps(x);
}
The direction of rotation is the sign of my input
What should happen if the target position is positive, but less than the current position?
Are "positions" cumulative, so that the command "move to x", if already at x, does nothing?