# Stepper motor control speed with potentiometer, I need change direction

Hi all!
This code it’s from arduino examples. I use it but I need more.
I need to change direction after a number of rotations, but using same code with potentiometer.

#include <Stepper.h>
const int stepsPerRevolution = 200; // change this to fit the number of steps per revolution
// initialize the stepper library on pins 8 through 11:
Stepper myStepper(stepsPerRevolution, 8, 9, 10, 11);
int stepCount = 0; // number of steps the motor has taken
void setup() {
// nothing to do inside the setup
}
void loop() {
// map it to a range from 0 to 100:
int motorSpeed = map(sensorReading, 0, 1023, 0, 100);
// set the motor speed:
if (motorSpeed > 0) {
myStepper.setSpeed(motorSpeed);
// step 1/100 of a revolution:
myStepper.step(stepsPerRevolution / 100);
}
}

I have no experience with that library as I prefer the AccelStepper library.
What is missing in your code:
If you want to change direction a certain number of steps in one direction,

1. have a variable to count the desired number if steps
2. when those are reached, have a command (look it up in stepper.h description) to reverse the direction

If you use the AccelStepper library you can tell it to move a specific number of steps and when it gets there you can change direction. Have a look at the AccelStepper examples.

Thank you, this is my solution

#include <Stepper.h>

const int stepsPerRevolution = 200; // change this to fit the number of steps per revolution

// initialize the stepper library on pins 8 through 11:
Stepper myStepper(stepsPerRevolution, 8, 9, 10, 11);

int stepCount = 0; // number of steps the motor has taken

void setup() {
// nothing to do inside the setup
Serial.begin(9600);
}

void loop() {
Serial.println(“clockwise”);
myStepper.step(stepsPerRevolution);
delay(500);

// step one revolution in the other direction:
Serial.println(“counterclockwise”);
myStepper.step(-stepsPerRevolution);
delay(500);

// map it to a range from 0 to 100:
int motorSpeed = map(sensorReading, 0, 1023, 0, 100);
// set the motor speed:
if (motorSpeed > 0) {
myStepper.setSpeed(motorSpeed);
// step 1/100 of a revolution:
myStepper.step(stepsPerRevolution / 100);
}
}

Do you want to tell us, that it ist working this way or not?
I suspect it's not working that well looking at your code.

I see two delays in the main loop and potentially other issues.
But it all depends how you want your sketch to "behave".

And, btw, pls use code tags when posting code.
If you don't know what that is, pls read the forum rules (for your convenience you will find a link in my signature).