#include <Stepper.h>
// modifiez ceci pour qu'il corresponde au nombre de pas par tour
// pour votre moteur
const int stepsPerRevolution = 200;
// initialiser la bibliothèque stepper sur les broches 8 à 11
Stepper myStepper(stepsPerRevolution, 8, 9, 10, 11);
// La broche numérique 2 est reliée au Speed Sensor Module LM393
const int PinLM393 = 2;
// La broche analogique A0 est reliée au Potentiometre
const int PinPotentiometre = A0;
int RPM; //Variable stock vitesse de rotation par minute
float RPS; //Variable stock vitesse de rotation par seconde
unsigned long last_temps;
volatile float TpsparTour;
volatile unsigned long tempsMillis = 0, last_tempsMillis = 0;
void setup() {
Serial.begin(9600);
pinMode(PinLM393, INPUT);
attachInterrupt(digitalPinToInterrupt(PinLM393), CalculTpsparTour, FALLING);
}
void loop() {
int ValeurPotentiometre = analogRead(PinPotentiometre);
int motorSpeed = map(ValeurPotentiometre, 0, 1023, 0, 200);
if (motorSpeed > 20) {
myStepper.setSpeed(motorSpeed);
myStepper.step(1);
} else {
digitalWrite(8, LOW);
digitalWrite(9, LOW);
digitalWrite(10, LOW);
digitalWrite(11, LOW);
}
if (millis() - last_temps >= 1000) {
if (TpsparTour > 0) {
detachInterrupt(digitalPinToInterrupt(PinLM393));
//Affiche le temps de 1 tour
Serial.print("Tps/Tour : ");
Serial.print(TpsparTour);
Serial.println(" ms");
//Affiche la vitesse de rotation par seconde
RPS = 1000 / TpsparTour;
Serial.print("tr/s : ");
Serial.println(RPS);
//Affiche la vitesse de rotation par minute
RPM = 60000 / TpsparTour;
Serial.print("tr/min : ");
Serial.println(RPM);
Serial.println("");
last_temps = millis();
attachInterrupt(digitalPinToInterrupt(PinLM393), CalculTpsparTour, FALLING);
}
}
}
void CalculTpsparTour() {
tempsMillis = millis();
// Anti-rebond du capteur 100ms
if ((tempsMillis - last_tempsMillis) > 100) {
//Calcul du Temps par tour
TpsparTour = tempsMillis - last_tempsMillis;
last_tempsMillis = tempsMillis;
}
}