#include "Arduino.h"
#include "NewEncoder.h"
// Pins 2 and 3 should work for many processors, including Uno.
// explanation:
// NewEncoder myEncoder(2, 3, -20, 20, 0, FULL_PULSE);
// create an encoder-object called "myEncoder"
// "2" IO-pin for first channel of rotary-encoder
// "3" IO-pin for second channel of rotary-encoder
// "-20" lower limit to count down to
// "20" upper limit to count up to
// 0 the value at initialisation
// "FULL_PULSE" type of pulse (other option HALF_PULSE)
// Use FULL_PULSE for encoders that produce one complete quadrature pulse per detnet, such as: https://www.adafruit.com/product/377
// Use HALF_PULSE for endoders that produce one complete quadrature pulse for every two detents, such as: https://www.mouser.com/ProductDetail/alps/ec11e15244g1/?qs=YMSFtX0bdJDiV4LBO61anw==&countrycode=US¤cycode=USD
//
NewEncoder myEncoder(2, 3, -20, 20, 0, FULL_PULSE);
int value = 0;
int lastValue = -1;
void setup() {
Serial.begin(115200);
Serial.println("Starting");
if (!myEncoder.begin()) {
Serial.println("Encoder Failed to Start. Check pin assignments and available interrupts. Aborting.");
while (1) {
yield();
}
} else {
Serial.print("Encoder Successfully Started at value = ");
Serial.println(myEncoder);
}
}
void checkEncoder() {
value = myEncoder;
if(value != lastValue) {
Serial.print("Encoder value= ");
Serial.println(value);
lastValue = value;
}
}
void loop() {
checkEncoder();
}
// the behaviour of the code that you can see in the serial monitor is
// the encoder-vale starts with 0 and depending on the rotating-direction
// counts up until 20 but not higher or counts down to -20 but not lower
// so in the serial monitor you should see
/*
Starting
Encoder Successfully Started at value = 0
Encoder value= 0
Encoder value= 1
Encoder value= 2...counting up
Encoder value= 20
Encoder value= 19
Encoder value= 18...counting down
Encoder value= 2
Encoder value= 1
Encoder value= 0
Encoder value= -1
Encoder value= -2... counting down
Encoder value= -19
Encoder value= -20
*/