#include <Stepper.h>
int stepRevo = 200;
Stepper Step(stepRevo, 8, 9, 10, 11);
int pinA = 4;
int pinB = 3;
int verso;
int counter = -1;
int currentPinA;
int currentPinB;
int lastPinA = LOW ;
void setup() {
Serial.begin(9600); // Setup Serial Monitor
Step.setSpeed(60);
pinMode(pinA, INPUT); // Set encoder pins as inputs
pinMode(pinB, INPUT);
}
void loop() {
currentPinA = digitalRead(pinA); // Read the current state of CLK
if (currentPinA != lastPinA && currentPinA == HIGH) {
currentPinB = digitalRead(pinB);
if (currentPinB != currentPinA ) {
counter --;
verso = HIGH;
} else {
counter ++;
verso = LOW;
}
if(verso == HIGH){
Serial.println("Direzione : orario ");
Step.step(-stepRevo);
delay(500);
}else{
Serial.println("Direzione : Antiorario ");
if(counter != 0){
Step.step(stepRevo);
delay(500);
}
}
Serial.print("Giro : ");
Serial.println(counter);
Serial.println("****************************");
}
lastPinA = currentPinA;
}