#define FORWARD true
#define REVERSE false
int directionPin = 4;
byte stepPin = 5;
int stepsPerRevolution = 200;
byte microstepping = 16;
class StepperMotor {
private:
int dirPin;
int stepPin;
int stepsPerRevolution;
int microstepping;
public:
StepperMotor(int dir, int step, int steps, int microstep) {
dirPin = dir;
stepPin = step;
stepsPerRevolution = steps;
microstepping = microstep;
pinMode(dirPin, OUTPUT);
pinMode(stepPin, OUTPUT);
}
void motor(bool direction, int degrees, float speed) {
int steps = map(degrees, 0, 360, 0, stepsPerRevolution * microstepping);
float stepDelay = (100 - (speed*10));
Serial.println(stepDelay);
digitalWrite(dirPin, direction);
long timer = millis();
for (int i = 0; i < steps; i++) {
digitalWrite(stepPin, HIGH);
delayMicroseconds(500);
digitalWrite(stepPin, LOW);
delayMicroseconds(stepDelay*10);
}
int tunrs = steps * microstepping - stepsPerRevelation * microstepping;
Serial.println(millis() - timer);
}
};
StepperMotor motor1(directionPin, stepPin, stepsPerRevolution, microstepping);
void setup(){
delay(5000);
Serial.begin(115200);
}
void loop(){
motor1.motor(FORWARD, 360, 1);
delay(5000);
motor1.motor(REVERSE, 360, 10);
delay(5000);
}