I need a little help switching libraries from stepper.h to accelStepper.h
I need a little help switching from "delay()" to "MIllis()"
My code is below for my "accelSteppr and Millis" attempt. My code using the standard libraries worked fine, except I have to do each action in a sequence, so it ends up being relatively slow. I would like to implement milis() and accelStepper to combine actions of the components.
What I am trying to accomplish:
-
actuate the Servos from 180 to 80
-
hold for one second
-
run 2 steppers (one rotates and the other drives linear) -- one at 300 steps/minute and the other at 600 steps per minute (with an acceleration and deceleration). one will step 1000 steps, the other only 500 steps.
-
power a relay to run a "dumb" motor (not shown in the current sketch).
-
WAIT for 6 seconds. This time may be as little as 2 seconds or as much as 10, but that will be "hard written" in the code, not a sensor or other programming "function." Basically just the MIllis() version of "delay(6000)."
-
return the steppers AND servos to their original positions (servos can return at ANY time after Step #2, but I want to combine the action while )
-
restart the loop to do everything again.
Right now in this sketch, my servos work fine, but my steppers are exceptionally slow and erratic - they seem to ONLY move 1-2 steps (over a 1-2 second period) then stop for a moment, then move 1-2 steps with no real pattern - always the same direction.
The stepper set-up works perfectly at the speeds I am asking - I even got it to "MoveTo(1000)" using accelStepper.h in a "test sketch" (and they run the examples fine), so I don't have a "mechanical problem.
This is my stepper motors and drivers (with 10amp 24v power supply):
I am running an Arduino Nano.
It's something in the coding and I just cannot crack it. After I wasted a whole day Saturday, I finally just decided to ask the Pros!
Here's my code:
#include <AccelStepper.h>
#include <Servo.h>
AccelStepper stepper2(AccelStepper::DRIVER, 2, 3, 4);
AccelStepper stepper3(AccelStepper::DRIVER, 8, 9, 10);
Servo loaderServo1;
Servo loaderServo2;
unsigned long previousMillis = 0;
unsigned long currentMillis;
const int loadDelay = 1000;
const int drillDelay = 5000;
void setup()
{
loaderServo1.attach(7);
loaderServo2.attach(6);
loaderServo1.write(0);
loaderServo2.write(0);
delay(1000);
loaderServo1.write(180);
loaderServo2.write(180);
stepper2.setMaxSpeed(600.0);
stepper2.setAcceleration(100.0);
stepper3.setMaxSpeed(300.0);
stepper3.setAcceleration(100.0);
}
void For() {
stepper2.moveTo(1000);
stepper3.moveTo(500);
stepper2.run();
stepper3.run();
}
void Rev() {
stepper2.moveTo(-1000);
stepper3.moveTo(-500);
stepper2.run();
stepper3.run();
}
void loadRod() {
unsigned long currentMillis = millis();
int interval = 1000;
if ((unsigned long)(currentMillis - previousMillis) >= interval) {
previousMillis = currentMillis;
loaderServo1.write(80);
loaderServo2.write(80);
}
if ((unsigned long)(currentMillis - previousMillis) >= interval) {
previousMillis = currentMillis;
loaderServo1.write(180);
loaderServo2.write(180);
previousMillis = millis();
}
}
void loop()
{
loadRod();
currentMillis = millis();
if (currentMillis - previousMillis >= loadDelay) {
For();
previousMillis = millis();
}
if (currentMillis - previousMillis >= drillDelay) {
Rev();
previousMillis = 0;
}
}