What i'm looking to do :
when the potentiometer (which is linked to the step motor via a cogwheel) is inactive for a set amount of time, return to center. This works when rotating the step motor manually to the left, it then rotates to the right to center. But when rotating it to the right, it keeps going right to center
I want to rotate it similarly to when it's rotated to the left, so when it's rotated to the right, center by rotating the opposite way, not the same way
Using my code in particular, I seem to be unable to reverse the step motor's rotation to return to center, here is my code :
#include <Stepper.h>
// Stepper motor configuration
const int stepsPerRevolution = 200; // Change this to match your stepper motor
Stepper myStepper(stepsPerRevolution, 8, 9, 10, 11); // Pins for stepper motor
// Constants for tiller control
const int potPin = A0; // Pin for potentiometer
const int centerValue = 515; // Center position value for the potentiometer
const int inactivityTimeout = 5000; // Time in milliseconds to return to center
const float returnSpeed = 0.3; // Speed of returning to center
const float smoothingFactor = 0.3; // Smoothing factor for gradual movement
unsigned long lastMovementTime = 0; // Timestamp for last movement
float outputAngle = 0; // Current output angle
void setup() {
Serial.begin(9600);
myStepper.setSpeed(60); // Set the speed of the stepper motor
}
void loop() {
// Read potentiometer value
int sensorValue = analogRead(potPin); // Read potentiometer value
Serial.println(sensorValue); // Send value to serial for debugging
// Calculate deviation from center
int deviation = sensorValue - centerValue; // Get difference from center
Serial.print("Deviation: ");
Serial.println(deviation); // Send deviation for debugging
// Apply smoothing
outputAngle += (deviation - outputAngle) * smoothingFactor;
// Determine steps to move based on deviation
int steps = outputAngle / 10; // Adjust divisor for more or fewer steps
// Move the stepper motor
myStepper.step(steps);
// Update the last movement time
lastMovementTime = millis();
// Check for inactivity
if (millis() - lastMovementTime > inactivityTimeout) {
// Gradually bring outputAngle back to center
if (outputAngle != 0) {
outputAngle += (0 - outputAngle) * returnSpeed; // Adjust towards center
// Determine steps to return to center
int returnSteps = outputAngle / 10; // Adjust divisor for more or fewer steps
// Move the stepper motor back to center
myStepper.step(returnSteps);
}
}
delay(10);
}
wiring :
(ULN 2003 Driver) :
IN1 -> digital pin 8
IN2 -> digital pin 9
IN3 -> digital pin 10
IN4 -> digital pin 11
I tried replacing
myStepper.step(steps); with
myStepper.step(-steps);
it did not work.