Hello,
I am using three stepper motor with suitable drivers. Attached code is working fine except I am not able to vary speed of motor with pot in case RUNZ and RUNZ1. I tried runToPosition() as well as runSpeed(). But no result. Please guide
void runFSM() {
Data_recieve(a, n);
AM = A[7];
FR = A[6];
ang = A[2];
drive_sp = A[3];
arm_sp = A[4];
stepperZ.setMaxSpeed(40000);
stepperZ.setAcceleration(40000);
drive_sp = map(A[3], 0, 1023, 0, Auto_Speed);
switch (state) {
case IDLE:
if (AM == 0) {
state = RUNZ;
}
break;
case RUNZ:
if (AM == 0) {
stepperZ.setCurrentPosition(0);
Serial.println("2");
arm_sp = map(A[4], 0, 1023, 0, 40000);
digitalWrite(Relay_Air, HIGH);
stepperZ.moveTo(18000);
stepperZ.setSpeed(arm_sp);
// Set the speed and move to the target position
stepperZ.runToPosition();
state = RUNX;
}
break;
case RUNX:
{
unsigned long distance = map(ang, 0, 1023, 0, 827);
stepperL.setMaxSpeed(5000);
stepperL.setAcceleration(100000);
stepperR.setMaxSpeed(5000);
stepperR.setAcceleration(100000);
unsigned long steps = ((60 * distance * 800UL) / (3.1415 * 310UL));
Serial.println(steps);
if (FR == 0) {
DirectionX(); // Assuming DirectionX() is defined elsewhere
}
for (unsigned long i = 0; i <= steps; i++) {
stepperL.setSpeed(Speed_Factor * drive_sp);
stepperR.setSpeed(Speed_Factor * drive_sp);
stepperL.moveTo(Step_DirectionX);
stepperR.moveTo(Step_DirectionX);
stepperL.run();
stepperR.run();
}
stepperL.setCurrentPosition(0);
stepperR.setCurrentPosition(0);
state = RUNZ1;
}
break;
case RUNZ1:
if (AM == 0) {
Serial.println("2");
arm_sp = map(A[4], 0, 1023, 0, 40000);
digitalWrite(Relay_Air, HIGH);
stepperZ.moveTo(0);
stepperZ.setSpeed(arm_sp);
stepperZ.runToPosition();
state = RUNX1;
}
break;
case RUNX1:
{
unsigned long distance = map(ang, 0, 1023, 0, 827);
stepperL.setMaxSpeed(5000);
stepperL.setAcceleration(100000);
stepperR.setMaxSpeed(5000);
stepperR.setAcceleration(100000);
unsigned long steps = ((60 * distance * 800UL) / (3.1415 * 310UL));
Serial.println(steps);
if (FR == 0) {
DirectionX(); // Assuming DirectionX() is defined elsewhere
}
for (unsigned long i = 0; i <= steps; i++) {
stepperL.setSpeed(Speed_Factor * drive_sp);
stepperR.setSpeed(Speed_Factor * drive_sp);
stepperL.moveTo(Step_DirectionX);
stepperR.moveTo(Step_DirectionX);
stepperL.run();
stepperR.run();
}
stepperL.setCurrentPosition(0);
stepperR.setCurrentPosition(0);
state = IDLE;
}
break;
}
}
void DirectionX() {
Step_DirectionX = !Step_DirectionX;
}