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
// for your motor
// 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() {
// read the sensor value:
int sensorReading = analogRead(A0);
// 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,
have a variable to count the desired number if steps
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.
// step one revolution in the other direction:
Serial.println(“counterclockwise”);
myStepper.step(-stepsPerRevolution);
delay(500);
// read the sensor value:
int sensorReading = analogRead(A0);
// 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).