#include <AccelStepper.h>
// Tipo di motore: 1 = DRIVER (usato per step/dir come A4988)
#define MOTOR_INTERFACE_TYPE 1
// Pin collegati a STEP e DIR
#define STEP_PIN 12
#define DIR_PIN 13
// Creazione oggetto motore
AccelStepper stepper(MOTOR_INTERFACE_TYPE, STEP_PIN, DIR_PIN);
//Lo stepper nema 17 di default ha 1.8 gradi a step, quindi un giro completo sono 200 step
/*
The motor also supports half-stepping (0.9 degrees per step / 400 steps per revolution).
You can even use smaller microsteps (e.g. 1/4 or 1/8 step),
but the simulated motor only displays the angle in half-step resolution.
*/
float posizione_step=200/2;
void setup() {
Serial.begin(115200);
// Impostazioni del motore
stepper.setMaxSpeed(5000); // passi al secondo
stepper.setAcceleration(500); // passi al secondo^2
}
void loop() {
// Imposta la posizione target (in passi)
stepper.moveTo(posizione_step); // ad esempio, muovi avanti di 2000 passi
// Muove il motore verso la posizione target
if (stepper.distanceToGo() != 0) {
stepper.run(); // chiama spesso per far muovere il motore
} else {
Serial.println("Invertiamo");
// Quando arriva a destinazione, inverti la direzione
posizione_step=-posizione_step;
stepper.moveTo(posizione_step);
}
}