int EncMA = 4; // Encoder Channel A int EncMB = 3; // Encoder Channel B int motor_direction = 8; // Motor direction int motor_PWM = 10; // Motor PWM int PWM_speed = 0; // PWM speed int PWM_ontime = 5; //PWM steps volatile unsigned long EncCount = 0; // Variable to store Encoder pulses void setup() { pinMode(EncMA, INPUT); //Set Encoder Channel A as input pinMode(EncMB, INPUT); //Set Encoder Channel B as input pinMode(17, INPUT); // Pass Through for Interrupt pinMode(18, INPUT); // Pass Through for Interrupt attachInterrupt(1, EncoderEvent, CHANGE); // initialize interrupt pinMode(motor_direction, OUTPUT); // Set DirectionMOT as output pinMode(motor_PWM, OUTPUT); // Set PWM_MOT as output Serial.begin(9600); } void loop() { digitalWrite(motor_direction, HIGH); while(PWM_speed < 75){ //Set max. duty cycle / 255 for full speed analogWrite(motor_PWM, PWM_speed); PWM_speed = PWM_speed + PWM_ontime; Serial.print("EncCounts: "); // Serial Monitor output Serial.println(EncCount); Serial.println(); delay(100); } while(PWM_speed > 0){ analogWrite(motor_PWM, PWM_speed); PWM_speed = PWM_speed - PWM_ontime; Serial.print("EncCounts: "); // Serial Monitor output Serial.println(EncCount); Serial.println(); delay(100); } digitalWrite(motor_direction, LOW); while(PWM_speed < 75){ //Set max. duty cycle / 255 for full speed analogWrite(motor_PWM, PWM_speed); PWM_speed = PWM_speed + PWM_ontime; Serial.print("EncCounts: "); // Serial Monitor output Serial.println(EncCount); Serial.println(); delay(100); } while(PWM_speed > 0){ analogWrite(motor_PWM, PWM_speed); PWM_speed = PWM_speed - PWM_ontime; Serial.print("EncCounts: "); // Serial Monitor output Serial.println(EncCount); Serial.println(); delay(100); } } void EncoderEvent() { if (digitalRead(EncMA) == HIGH) { if (digitalRead(EncMB) == LOW) { EncCount++; } else { EncCount--; } } else { if (digitalRead(EncMB) == LOW) { EncCount--; } else { EncCount++; } } }