// Controle d'un moteur pas à pas par un L297 et un L293D // Un potentiomètre doit être branché sur l'entrée A0 // Un bouton poussoir doit être cablé sur l'entrée D11 // Entrées-sorties int moteurHALF = 5; int moteurCLK = 6; int moteurCW = 7; int moteurENABLE = 8; int moteurRESET = 9; int activation_in = 11; // Détection front montant et activation bool val_activation = 0; bool old_val_activation = 0; bool etat_activation = 1; // Valeur lue sur potentiomètre int val_pot; int periode; void pas(int periode_ms){ digitalWrite(moteurCLK, HIGH); delayMicroseconds(periode_ms); digitalWrite(moteurCLK, LOW); delayMicroseconds(periode_ms); } void setup() { Serial.begin(115200); Serial.println("Moteur Pas à Pas et L297"); pinMode(moteurHALF, OUTPUT); pinMode(moteurCLK, OUTPUT); pinMode(moteurCW, OUTPUT); pinMode(moteurENABLE, OUTPUT); pinMode(moteurRESET, OUTPUT); pinMode(activation_in, INPUT); delay(10); digitalWrite(moteurRESET, HIGH); digitalWrite(moteurHALF, HIGH); digitalWrite(moteurENABLE, LOW); } void loop() { val_activation = digitalRead(activation_in); if(val_activation != old_val_activation and val_activation == 1){ etat_activation = !etat_activation; } old_val_activation = val_activation; if(etat_activation == 1){ val_pot = analogRead(A0); periode = map(val_pot, 0, 1023, -1000, 1000); if(periode < 0){ periode = 1000+periode+300; Serial.println(periode); digitalWrite(moteurENABLE, HIGH); digitalWrite(moteurCW, HIGH); pas(periode); } else{ periode = 1000-periode+300; Serial.println(periode); digitalWrite(moteurENABLE, HIGH); digitalWrite(moteurCW, LOW); pas(periode); } } else{ digitalWrite(moteurENABLE, LOW); } }