Hi everyone,
I would like my two motors to do the following:
- stepper1 and stepper2 turn simultanously for 6400 steps
- stepper1 stops until stepper2 has completed another 3200 steps
- stepper1 and stepper2 turn again simultaneously for 6400 steps
- stepper1 stops until stepper2 has completed another 3200 steps
- stepper1 and stepper2 turn again simultaneously for 6400 steps.
All of this should happen when I send a "1" from serial communication. I am using the accelstepper library (AccelStepper: AccelStepper library for Arduino).
My code so far, including serial communication and some other important chunks that have to stay in it is below. How and where do I have to modify the code? Will I have to insert another if-loop in the if (letter=='1') loop? I have tried around with different loops and commands but have not been able to figure it out.
Any advice appreciated!
Thanks,
Isa
#include <AccelStepper.h>
// Define two steppers and the pins they will use
AccelStepper stepper1(1, 9, 8);
AccelStepper stepper2(1, 7, 6);
// Define a variable to record WHEN the instruction arrived.
unsigned long instructionTime = 0;
void setup() {
Serial.begin(9600);
stepper1.setMaxSpeed(8000);
stepper2.setMaxSpeed(8000);
stepper1.setAcceleration(100000000);
stepper2.setAcceleration(100000000);
}
void loop()
{
if (Serial.available() > 0)
{
char letter = Serial.read();
if (letter=='1')
{
stepper1.move(-6400);
stepper1.setSpeed(3350);
stepper2.move(6400);
stepper2.setSpeed(3350);
instructionTime = millis();
}
else if (letter=='2')
{
stepper1.move(-6400);
stepper1.setSpeed(3350);
stepper2.move(6400);
stepper2.setSpeed(3350);
instructionTime = millis()+500;
}
// Run if EITHER stepper1 OR stepper2 has some distance left to go.
if (stepper1.distanceToGo() != 0 || stepper2.distanceToGo() != 0)
{
// run stepper1 regardless of the time
stepper1.runSpeedToPosition();
// but only run stepper2 if the time is more than 500ms from when the
// instruction was received
if (millis() >= instructionTime)
{
stepper2.runSpeedToPosition();
}
if (stepper1.distanceToGo() == 0 && stepper2.distanceToGo() == 0)
{
Serial.print('X');
}
}
}
}