#include <AccelStepper.h>
const int pinPUL_1 = 40; // impulsion
const int pinDIR_1 = 42; // dir of rotation
const int pinPUL_2 = 44; // impulsion
const int pinDIR_2 = 45; // dir of rotation
AccelStepper moteur1(AccelStepper::DRIVER, pinPUL_1, pinDIR_1);
AccelStepper moteur2(AccelStepper::DRIVER, pinPUL_2, pinDIR_2);
const int pinButon1 = 36; // motor is turning while pushing
const int stepsPerRevolution = 200;
const int rotationsPerSecond = 10; // when this value is <= 5 everything is fine. For higher value I observe the weird behavior
// let the motor slowing down
bool arretEnCours = false;
void setup() {
// Configuration moteur
moteur1.setAcceleration(400); // acceleration in step/s²
moteur2.setAcceleration(400); // accélération in step/s²
// Config bouton
pinMode(pinButon1, INPUT);
}
void loop() {
int bouton1 = digitalRead(pinButon1);
if (bouton1 == 1) {
moteur1.setMaxSpeed(stepsPerRevolution * rotationsPerSecond);
moteur2.setMaxSpeed(stepsPerRevolution * rotationsPerSecond);
moteur1.moveTo(moteur1.currentPosition() - 100000);
moteur2.moveTo(moteur2.currentPosition() + 100000);
arretEnCours = false;
}
else {
if (!arretEnCours) {
// Clean slowing down
moteur1.stop();
moteur2.stop();
arretEnCours = true;
}
}
moteur1.run();
moteur2.run();
}