Skip to content

GetEncoderValue()

Arnd edited this page Dec 11, 2020 · 2 revisions

GetEncoderValue();

This function will return the current value of the encoder. Each detent ("click") of the encoder will add (when turning clockwise) or subtract (when going counter-clockwise) 4 from the counter. While it can happen that due to noise or other mechanical factors a number that is not divisible by 4 can occur when the encoder is not being moved, the SetEncoderValue() function can (re)set the value. The internal variable is a signed integer number, which should have sufficient scope of values from -32,768 to 32,767 for normal operating conditions before overflowing or underflowing.


Example:

// Instantiate the class using all of the pins defined in constants     //
EncoderClass Encoder(ROTARY_PIN_1, ROTARY_PIN_2, PUSHBUTTON_PIN, RED_PIN, 
                     GREEN_PIN, BLUE_PIN);

void setup() {
  Serial.begin(115200);
  delay(1000);
} // of method setup()
void loop(){
  static int last = Encoder.GetEncoderValue();
  if (Encoder.GetEncoderValue()!=last) { // If the reading has changed  //
    last = Encoder.GetEncoderValue();    // display the new value       //
    Serial.print("Encoder position is ");
    Serial.println(last);
  } // of if-then we have a changed value
} // of method loop()