Can't get rotary encoder direction with RotaryEncoder.h library

Hello everyone,
I've been trying to get the direction of my rotary encoder and print it in the serial monitor using the RotaryEncoder.h library and this code:

#include <RotaryEncoder.h>

RotaryEncoder encoder(2,3);

void setup() {
 
Serial.begin(9600);

}

void loop() {
  
encoder.tick();
int8_t dir = encoder.getDirection();
Serial.println(dir);
}

when I compile it I get this error: "exit status 1
cannot convert 'RotaryEncoder::Direction' to 'int8_t {aka signed char}' in initialization"

What have I done wrong?
Thank you!

getDirection returns a value of type Direction, not int8_t

from https://github.com/mathertel/RotaryEncoder/blob/master/RotaryEncoder.h

enum class Direction { NOROTATION = 0, CLOCKWISE = 1, COUNTERCLOCKWISE = -1};

Direction getDirection();

you need to use the values: NOROTATION, CLOCKWISE, COUNTERCLOCKWISE

Thank you for your reply! :wink:
So how should I declare the variable (dir) that I'm using for the direction in my sketch?

Direction dir = encoder.getDirection();

by using Direction dir = encoder.getDirection(); I get: " 'Direction' was not declared in this scope ".

If I understood correctly, "Direction" should be a variable type for the library, but it isn't recognized by the IDE right?

The Direction enum is part of the RotaryEncoder class. For this reason, it should be:

RotaryEncoder::Direction dir = encoder.getDirection();
1 Like

Thank you for your answer
By using this instruction I don't get any error anymore! But if I want to print the value on the monitor I get the error: " no matching function for call to 'HardwareSerial::println(RotaryEncoder::Direction&)' ".
If I try to put

if(dir==1)
{Serial.println("ok")}

I get the error: "no match for 'operator==' (operand types are 'RotaryEncoder::Direction' and 'int')"
So what I think it means is that the output of "dir" can't be compared with an int number since it's a library own "variable type". Can't I get a direction variable that is an int or some other Arduino language type variable? Hope I haven't made any stupid error :smiley:

if (dir == RotaryEncoder::Direction::CLOCKWISE) {
.
.
}

Thanks a lot to all of you for troubleshooting! :wink: :smiley: