#include <AccelStepper.h>
AccelStepper stepper1(1, 9, 8); // (Typeof driver: with 2 pins, STEP, DIR)
AccelStepper stepper2(1, 11);
int rot = 10;
int curRot = 0;
int rotCount = 0;
int curDir = 0;
int lastDir = 0;
void setup()
{
Serial.begin(9600);
pinMode(7, INPUT_PULLUP);
pinMode(6, INPUT_PULLUP);
stepper1.setMaxSpeed(100); // Set maximum speed value for the stepper
stepper1.setAcceleration(500); // Set acceleration value for the stepper
stepper1.setCurrentPosition(0); // Set the current position to 0 steps
stepper2.setMaxSpeed(50);
stepper2.setAcceleration(500);
stepper2.setCurrentPosition(0);
curRot = 200*rot;
digitalWrite(12, LOW);
stepper1.moveTo(curRot);
stepper2.moveTo(curRot);
}
void loop()
{
/*
while (stepper1.currentPosition() <= curRot)
{
if(digitalRead(7) == HIGH && digitalRead(6) == LOW)
{
Serial.println("Right");
if(curDir == 0)
{
digitalWrite(12, HIGH);
}
curDir = 1;
}
else if(digitalRead(6) == HIGH && digitalRead(7) == LOW)
{
Serial.println("Left");
if(curDir == 1)
{
digitalWrite(12, LOW);
}
curDir = 0;
}
stepper1.run();
stepper2.run();
}
*/
if(digitalRead(7) == HIGH && digitalRead(6) == LOW)
{
Serial.println("Right " + String(curDir));
if(curDir == 0)
{
digitalWrite(12, HIGH);
}
curDir = 1;
}
else if(digitalRead(6) == HIGH && digitalRead(7) == LOW)
{
Serial.println("Left " + String(curDir));
if(curDir == 1)
{
digitalWrite(12, LOW);
}
curDir = 0;
}
stepper1.run();
//stepper2.run();
if(stepper1.distanceToGo () > 0)
{
stepper2.run();
}
else
{
Serial.println("Stop");
}
}