Hello, this is my first post and I am new to the Arduino programming world.
I've read through many forums and can't find anything that matches my problem.
The problem I am having is controlling 2 stepper motors. Stepper 1 needs to run to a target position but, I want it to stop at step intervals along the way to the target. My final target will be approx. 31000 steps but I want it to stop at steps (800 or 2400 for example) along the way for a specific time period (5 minutes for example). I have tried to stay way from the delay command for these longs delays as I have read it will stop your entire program.
Stepper 2 needs to step a certain amount of steps after stepper 1 has reached each interval. For example 5 steps when stepper 1 reaches the first interval and 10 steps when stepper 1 reaches it's second interval.
I have tried many different ways but can't seem to figure out how to get stepper 1 to move to a target position with these intervals in between. The move to () always runs to the absolute target position.
If I just add the move () stepper 1 will keep moving the specified steps over and over without allowing stepper 2 to run.
For the purpose of saving time I have set my move to () at 800 for now and move () at 200.
Sorry, I am new to this but, is there any way to get these intervals with a fixed target position with time delays in between?
I have attached my sketch below. Sorry, there are many notes as I have been trying different things.
Any help would be very appreciated.
#include <AccelStepper.h>
#include <Wire.h>
#include <Adafruit_MotorShield.h>
#include "utility/Adafruit_PWMServoDriver.h"
Adafruit_MotorShield AFMSbot(0x61); // Rightmost jumper closed
Adafruit_MotorShield AFMStop(0x60); // Default address, no jumpers
// Connect two steppers with 200 steps per revolution 1.8 degrees
// to the top shield
Adafruit_StepperMotor *myStepper1 = AFMStop.getStepper(200, 1);
Adafruit_StepperMotor *myStepper2 = AFMStop.getStepper(200, 2);
void forwardstep1() {
myStepper1->onestep(FORWARD, SINGLE);
}
// wrappers for the second motor!
void forwardstep2() {
myStepper2->onestep(FORWARD, SINGLE);
}
// Now we'll wrap the 2 steppers in an AccelStepper object
// running both stepper motors forward to destination point
AccelStepper stepper1(forwardstep1, forwardstep1);
AccelStepper stepper2(forwardstep2, forwardstep2);
// define the start time of the program
long starttime = 0;
long stepper2starttime = 0;
long previousmillis = 0;
long stepper2previousmillis = 0;
long lastStopmillis = 0;
long delayinterval = 10000; // 10 second delay interval
long stepper2delayinterval = 10000; // 30 second delay interval
long stepcount = 0;
void setup()
{
Serial.begin(9600);
AFMStop.begin(); // Start the top shield
stepper1.setMaxSpeed(100.0);
stepper1.setAcceleration(100.0);
// this will rotate stepper 1 (800 steps or 1 inch of travel)
// Note: (200 steps is 360 degrees of rotation of the spindle)
stepper1.moveTo(800);
stepper2.setMaxSpeed(100.00);
stepper2.setAcceleration(100.0);
// this will rotate the stepper 1 revolutions (200 steps is 360 degrees)
//stepper2.moveTo(200);
//starttime = millis();
//stepper2starttime = millis ();
}
void loop()
{
//step stepper 1 to 800 steps (1 inch)
//stepper1.moveTo(800);
//stepper1.move(200);
stepper1.runToPosition(); //blocks other functions until stepper reaches target steps
//Serial.println(stepcount);
if (stepcount <= 800)
stepper1.run();
stepcount = stepcount +1; //increments how far we've stepped by 1
//Serial.println(stepcount);
// while (stepcount == 800)
if (stepcount == 800)
// if (stepper2currentmillis - stepper2previousmillis > stepper2delayinterval)
stepper2.moveTo(200);
stepper2.runToPosition(); //blocks other functions until stepper reaches target steps
stepper2.run();
// unsigned long stepper2currentmillis = millis ();
// stepper2previousmillis = stepper2currentmillis;
//once stepper1 has moved a total of 800 steps (1 inch) set the stepcounter to 0
//so we can repeat the move
// if (stepcount > 800)
//{
// stepcount = 0;
//}
}