Stepper Motor Help

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);
}

}


Please Help!

Stepper_Motor_Project_3_1.ino (747 Bytes)

These go in the setup() function:

stepper1.setMaxSpeed(1000.00);
  stepper1.setAcceleration(900.0);
  stepper1.setSpeed(350);

This value is never changing:
Serial.println(currentPos);

What does this show?
Serial.println(val);

Take a look at the examples provided with this library. AccelStepper

Serial.println(val); just echos the value that was sent to the stepper.
The result was the same.

But does the stepper move?

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());

The stepper does not move at all not, the original program moved it one step. That was my original thought that the loop cut it off.

I suggest you move this call outside the 'if(Serial.available())' statement, so that it is called each time loop() executes:

stepper1.run();

At the moment, you will only try to step the stepper once, for each received command.

The rotation seems to be completely random no matter what number I input. But it does move!

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.

...R

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 setup()
{
stepper1.setMaxSpeed(1000.00);
stepper1.setAcceleration(900.0);
stepper1.setSpeed(1000);
Serial.begin(9600);
}

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);

}

stepper1.run();

}

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.