I am currently using the following code that rotates a Nema 17 Stepper motor with a Quadrature Optical Encoder.
Is it possible to have another loop that is triggered by a button press to rotate the Stepper Motor clockwise at a certain speed for 10 full rotations or until another button is pressed to put it back in the original mode?
Here is the code I am currently using.
#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);
}
}