diff --git a/IOFunctions.cpp b/IOFunctions.cpp index 2907a1a..32bb454 100644 --- a/IOFunctions.cpp +++ b/IOFunctions.cpp @@ -398,7 +398,10 @@ void receiveEvent(int received) { Serial.println(F("DEBUG: Turn accessory pin off")); } setAccessory(LOW); - +#if TURNTABLE_EX_MODE == TURNTABLE + } else if (activity == 18 && !stepper.isRunning() && !calibrating) { + reverseTurntable(activity); +#endif #ifdef USE_RT_EX_TURNTABLE } else if ((activity >= 10) && (activity <= 17)) { setExtra(activity); diff --git a/TurntableFunctions.cpp b/TurntableFunctions.cpp index 0414cd8..e2d608d 100644 --- a/TurntableFunctions.cpp +++ b/TurntableFunctions.cpp @@ -183,6 +183,7 @@ void moveHome() { #endif stepper.setCurrentPosition(0); lastStep = 0; + lastTarget = 0; homed = 1; Serial.println(F("Turntable homed successfully")); if (debug) { @@ -283,6 +284,28 @@ void moveToPosition(long steps, uint8_t phaseSwitch) { } } +void reverseTurntable(uint8_t phaseSwitch){ + // Calculate new target 180 degrees from current logical position. + long baseTarget = lastStep % fullTurnSteps; + if (baseTarget < 0) { + baseTarget += fullTurnSteps; + } + long newTarget = baseTarget + halfTurnSteps; + if (newTarget >= fullTurnSteps) { + newTarget -= fullTurnSteps; + } + // Set phase if needed + setPhase(phaseSwitch); + // Move to new target + stepper.enableOutputs(); + stepper.moveTo(newTarget); + lastTarget = newTarget; + lastStep = newTarget; + + Serial.print(F("Reverseing Turntable: Rotating to ")); + Serial.println(newTarget); +} + // Function to set phase. void setPhase(uint8_t phase) { #if RELAY_ACTIVE_STATE == HIGH diff --git a/TurntableFunctions.h b/TurntableFunctions.h index 4d300ed..18d8836 100644 --- a/TurntableFunctions.h +++ b/TurntableFunctions.h @@ -36,6 +36,7 @@ extern bool calibrating; extern uint8_t homed; extern AccelStepper stepper; extern long fullTurnSteps; +extern long halfTurnSteps; extern long phaseSwitchStartSteps; extern long phaseSwitchStopSteps; extern long lastTarget; @@ -56,6 +57,7 @@ void initiateHoming(); void initiateCalibration(); void setLEDActivity(uint8_t activity); void setAccessory(bool state); +void reverseTurntable(uint8_t phaseSwitch); #ifdef USE_RT_EX_TURNTABLE void setExtra(uint8_t activity);