#include <Stepper.h>
#include <Toggle.h>
const int stepsPerRevolution = 200; // à adapter
Stepper moteur(stepsPerRevolution, 8, 9, 10, 11);
const byte pinFdcDroite = 2;
const byte pinFdcGauche = 12;
const byte pinBoutonStop = 3;
enum Etat {ARRET, CW, CCW};
Etat etatMoteur = ARRET;
Etat ancienEtat = CW;
const byte potPin = A0;
unsigned long attente, dernierPas;
unsigned long maxAttente = 200;
unsigned long minAttente = 2;
Toggle fdcDroite, fdcGauche, boutonStop;
void setup() {
Serial.begin(115200);
fdcDroite.begin(pinFdcDroite);
fdcGauche.begin(pinFdcGauche);
boutonStop.begin(pinBoutonStop);
}
void loop() {
unsigned long nouvelleAttente = map(analogRead(potPin), 0, 1023, maxAttente, minAttente);
if (nouvelleAttente != attente) {
attente = nouvelleAttente;
Serial.print("∆t = "); Serial.println(attente);
}
boutonStop.poll();
if (boutonStop.onPress()) {
if (etatMoteur == ARRET) etatMoteur = ancienEtat;
else {
ancienEtat = etatMoteur;
etatMoteur = ARRET;
}
}
switch (etatMoteur) {
case ARRET:
break;
case CW:
if (attente < maxAttente && millis() - dernierPas >= attente) {
moteur.step(1);
dernierPas = millis();
}
break;
case CCW:
if (attente < maxAttente && millis() - dernierPas >= attente) {
moteur.step(-1);
dernierPas = millis();
}
break;
}
fdcDroite.poll();
fdcGauche.poll();
if (fdcDroite.onPress() || fdcGauche.onPress()) {
switch (etatMoteur) {
case ARRET: if (ancienEtat == CW) ancienEtat = CCW; else ancienEtat = CW; break;
case CW: etatMoteur = CCW; break;
case CCW: etatMoteur = CW; break;
}
}
}