#define POS1 80
#define POS2 0
const uint16_t DELAY = 200;
enum stepperstates {STOPPED, RUNTO1, WAIT1, RUNTO2, WAIT2};
[...]
void loop() {
static uint16_t last_millis;
static stepperstates xStepState;
if (PS3.getButtonClick(START)){
xStepState = RUNTO1;
StepperControl.moveTo(POS1); // go to POS1
Serial.print("\n Starting Movement between POS1 and POS2");
}
if (PS3.getButtonClick(CROSS)) {
xStepState = STOPPED;
}
StepperControl.run();
switch (xStepState) {
case STOPPED:
StepperControl.stop();
break;
case RUNTO1:
if (StepperControl.distanceToGo()==0) {
xStepState = WAIT1;
last_millis = millis();
}
break;
case WAIT1:
if (millis() - last_millis >= DELAY) {
xStepState = RUNTO2;
StepperControl.moveTo(POS2);
}
break;
case RUNTO2:
if (StepperControl.distanceToGo()==0) {
xStepState = WAIT2;
last_millis = millis();
}
break;
case WAIT2:
if (millis() - last_millis >= DELAY) {
xStepState = RUNTO1;
StepperControl.moveTo(POS1);
}
break;
}
}
add more explanation of a finite state machine and the states in the example
jose can u c
- 7k
- 2
- 17
- 27