Hi All,
Please excuse my poor programing skills, I am new and am not very proficient. I am using AccelStepper to make things a little easier. I am trying to input a value through serial to give my stepper an amount to move and send back the results, but I keep getting my loops messed up so they interrupt each other. This is what I have so far...
#include <AccelStepper.h>
#define HALFSTEP 8
// Motor pin definitions #define motorPin1 3 // IN1 on the ULN2003 driver 1 #define motorPin2 4 // IN2 on the ULN2003 driver 1 #define motorPin3 5 // IN3 on the ULN2003 driver 1 #define motorPin4 6 // IN4 on the ULN2003 driver 1 #define arraySize 32
AccelStepper stepper1(HALFSTEP, motorPin1, motorPin3, motorPin2, motorPin4);
int currentPos = stepper1.currentPosition();
void setup()
{
Serial.begin(9600);
}
void loop(){
if (Serial.available())
{
int val = Serial.parseInt();
stepper1.setMaxSpeed(1000.00);
stepper1.setAcceleration(900.0);
stepper1.setSpeed(350);
stepper1.moveTo(val);
stepper1.run();
Serial.println(val);
Serial.println(currentPos);
}
The run() function will try to move the motor (at most one step per call) from the current position to the target position set by the most recent call to this function. Caution: moveTo() also recalculates the speed for the next step. If you are trying to use constant speed movements, you should call setSpeed() after calling moveTo().
Add this instead.
Serial.print(val);
Serial.print(" ");
Serial.println(stepper1.currentPosition());
I think the key to the problem is what @PeterH has said.
stepper1.run() needs to called as often as possible. If the motor is already at its destination it will have no effect.
I also think the problem would be easier to debug if you think of it as two separate tasks - ( A ) getting a value from the serial input and saving it and ( B ) making the stepper motor move based on a value in a variable. That way you could try both bits in separate short sketches so that the two issues are not confusing each other.
Thank you for the help I have gotten it to work to an extent but I still need to write some math or have a zeroing button on the remote, here is the code. #include <AccelStepper.h>
#define HALFSTEP 8
// Motor pin definitions #define motorPin1 3 // IN1 on the ULN2003 driver 1 #define motorPin2 4 // IN2 on the ULN2003 driver 1 #define motorPin3 5 // IN3 on the ULN2003 driver 1 #define motorPin4 6 // IN4 on the ULN2003 driver 1 #define arraySize 32
AccelStepper stepper1(HALFSTEP, motorPin1, motorPin3, motorPin2, motorPin4);
void loop(){
int currentpos = stepper1.currentPosition();
if (Serial.available())
{
int val = Serial.parseInt();
//(currentpos+val);
int steps =(val*11);
stepper1.moveTo(steps);
stepper1.setSpeed(1000);
Serial.print(val);
Serial.print(" ");
Serial.println(currentpos);
Yes, since steppers don't inherently give you absolute positioning control, you need some other way to know where they are. Sometimes, limit switches (aka parking switches) are used for that.