I am trying to get a stepper to move with a sliding potentiometer, with the center of the slider being the off position then left forward, and right reverse. How do I code "if potentiometer increases/decrease by 10, move stepper X steps, then stop moving."? I am using the stepper.h library. I am pretty sure I need to record the baseline pot position into an int and then compare that but I am just learning and do not know how to do that yet. Thanks
EDIT: Changed my mind, I am now going to use a rotary encoder instead of a sliding POT.
Off the top of my head, I think map() may be the easiest solution to doing this. Get the pot position, then map it to how many steps that should represent.
potPosition = analogRead(potPin);
previousStepperPosition = stepperPosition; // Saving where we were so we can set a new position
stepperPosition = map(potPosition, minPot, maxPot, minStepperPosition, maxStepperPosition); // converts pot position to desired stepper position
if(stepperPosition < previousStepperPosition)
{
digitalWrite(stepperDirectionPin, HIGH); // Sets direction for the stepper to turn, it may be backwards
}else { digitalWrite(stepperDirectionPin, LOW); }
stepMotor(abs(previousStepperPosition - stepperPosition)); // A function you define that will cause the motor to take so many steps
I haven't tested that this code even compiles, but it should give you an idea.
It shouldn't require an encoder, just record position by how many steps it has taken. You may experience an issue when you first attach everything - it may be best to set the pot to the lowest value, then attach things to the motor as it should be with the pot at the lowest value. I imagine you will the need to scale your map() values to get it to turn the correct amount.