Hi, I am still trying to get some stuff working and I thought I had it figured out. I divided parts of my project into smaller projects and combined them that way.
I am still using accelstepper btw.
When I click a button, a stepper moves right, another button, stepper moves left.
This works fine.
void runStepperR() { // ove stepper by joystick clockwise until endstop.
stepper1.setMaxSpeed(5000); // motor speed stepper 1
stepper1.moveTo(150000); // Keep moving until endstop.
stepper1.setSpeed(5000);
stepper1.runSpeedToPosition(); // Yup, run Forest
}
void runStepperL() { // Move stepper by joystick counter clockwise until endstop.
stepper1.setMaxSpeed(5000); // motor speed stepper 1
stepper1.moveTo(-150000); // Keep moving until endstop.
stepper1.setSpeed(5000);
stepper1.runSpeedToPosition(); // Yup, run Forest
}
I save the number of steps made so that I can toggle between 0 (my endpoint) and the value that was generated using above code. For instance, 10000 steps.
When I click a button (not hold) I want to move the stepper as fast as I did before from 0 to 10000 and when I click again go from 10000 to 0.
In void loop() I have the following code:
if (StartbuttonState == LOW && flagRUN == 0 && setupActive == 0) { // van start naar einde
flagRUN = 1;
}
if (flagRUN == 1 && flagDIR == 0) { // we zijn op startpunt, nu naar eindpunt!
runMemoryToEnd();
}
if (flagRUN == 1 && flagDIR == 1) { // we zijn op eindpunt, nu naar startpunt!
runMemoryToStart();
}
This sets a flag (flagRUN) to be active and the flagDIR is the direction. If the button is clicked and both flags are 0, runMemoryToStart() is run. if the DIR = 1 than runMemoryToEnd() is run etc.
The code for that is:
void runMemoryToStart() { // move stepper with acceleration to startpoint, so from 1000 > 0
stepper1.setMaxSpeed(7000); // motor speed stepper 1
stepper1.setAcceleration(5000);
stepper1.moveTo(-stepper1Pos); // Keep moving until endstop.
stepper1.run();
if (stepper1.distanceToGo() == 0) { // are we at desired location?
stepper1.setCurrentPosition(-stepper1Pos); // reset speed
// change direction
flagRUN = 0; // stop running
flagDIR = 0;
}
}
void runMemoryToEnd() { // move stepper with acceleration to endpoint, so from 0 > 1000
stepper1.setMaxSpeed(7000); // motor speed stepper 1
stepper1.setAcceleration(5000);
stepper1.moveTo(stepper1Pos); // Keep moving until endstop.
stepper1.run();
if (stepper1.distanceToGo() == 0) { // are we at desired location?
stepper1.setCurrentPosition(stepper1Pos); //reset // reset speed
flagRUN = 0; // stop running
flagDIR = 1; // change direction
}
}
I had some serial prints running to verify that steps were counted etc and they do. The things that I can't get working is the speed. It seems like the stepper runs at a lower speed. Slower than the original speed.
Could it be the 'if' statement in the last code that slows things down?
Is there another way to use 1 click of a button to get the steppers moving for x steps? I need to have a flag of some kind to signal the end of the steps and the direction, don't I?