Hi,
I try to write a code to run stepper motor which turns desired steps and stops for desired period. Each step and stop period movement is a one cycle and I also attached desired cycle into code. Surprisingly it works fine rather than a problem. İn every 2 cycle(1 forward and 1 backward), final position slightly shift left from initial start position. I could not solve the problem.Can anyone help me to solve it? You can check the shifted position from png file.
#include <Arduino.h>
#include <AccelStepper.h>
AccelStepper stepper(1, 50, 52);
int speed = 5000; //set speed
int acc = 5000; //set accelearation
int incCount = 20; // Desired step in a cycle
unsigned long count = 0; //Step Count
unsigned long katCount = 0; //Cycle Count
int state; //0= running 1=waiting
unsigned long timer;
const unsigned long delayTime = 50; //waiting time of the motor
unsigned long distance = 30; //
int layer = 100; //Total Cycle
void setup()
{
Serial.begin(9600);
Serial.println("Testing accel");
stepper.setMaxSpeed(speed);
stepper.setAcceleration(acc);
stepper.move(0);//start by moving
}
void loop()
{
if (katCount != layer) {
if (state == 0) {
if (stepper.distanceToGo() == 0) {
state = 1;
count = count + 1 ;
if (count == incCount) {
katCount = katCount + 1;
count = 0;
stepper.setSpeed(acc);
distance = distance * (-1);
}
timer = millis() + delayTime;
}
} else { //state == waiting
if (millis() >= timer) {
state = 0;
stepper.move(distance);
}
stepper.run();
Serial.println(katCount);
}
else {
stepper.stop();
stepper.disableOutputs();
}
}