#include <Stepper.h>
const int X27StepsPerRevolution = 720;
const int maxSteps = 630; // X27 can only turn 315° => 720 steps * 315° / 360° = 630 steps
int currentPos = 0;
Stepper LSTGauge(X27StepsPerRevolution, 4, 5, 6, 7); // Stepper wired to Arduino pins 4, 5, 6, 7
void setup() {
Serial.begin(9600);
LSTGauge.setSpeed(70);
}
void loop() {
int receivedNb;
boolean moveOrder = false;
if (Serial.available()>0) {
receivedNb = Serial.parseInt();
while (Serial.available()>0) Serial.read();
moveOrder = true;
Serial.print ("Received ");
Serial.print (receivedNb, DEC);
Serial.print (" => ");
}
if (moveOrder) {
if (receivedNb == -1) {
Serial.println ("Homing gauge ...");
delay(1000);
LSTGauge.step (-1 * (maxSteps+10));
currentPos = 0;
}
else if (receivedNb >= 0 and receivedNb <= maxSteps) {
Serial.println ("Moving to " + String(receivedNb) + " steps ... ");
int relativePos = receivedNb - currentPos;
Serial.print ("relativePos = "); Serial.println (relativePos);
Serial.println();
delay(1000);
LSTGauge.step (relativePos);
currentPos = receivedNb;
}
else Serial.println ("Nothing to do !");
}
}