mathertel/rotaryencoder

repository·master·Indexed 18 days ago

https://github.com/mathertel/rotaryencoder

An Arduino library for interfacing with rotary encoders, optimized for encoders that trigger phase changes on both input signals during a single notch of rotation. It provides features for tracking position, direction, RPM, and rotation timing, with configurable LatchMode settings for sensitivity and wiring compatibility.

Tokens
1K
Snippets
5
Records
6
Agent score
14%

What's inside rotaryencoder

  1. Overview of the RotaryEncoder library

    master
    The RotaryEncoder library is designed for the Arduino environment to handle rotary encoder inputs. It specifically supports rotary encoders that exhibit a phase change on both input signals during a single notch of rotation. This makes it suitable for encoders where both signals transition within one physical detent.
  2. Initialize the RotaryEncoder class

    master

    You can initialize a RotaryEncoder instance in two ways depending on whether you want to handle hardware pin configuration immediately or manually later.

    1. With Hardware Initialization: Pass the two encoder pins and a LatchMode. This automatically configures the pins, enables internal pull-up resistors, and establishes the initial position.
    2. Without Hardware Initialization: Use the constructor that only takes a LatchMode. This is useful if you want to configure pins manually or use a different initialization strategy.

    Use NO_PIN (defined as -1) to skip hardware configuration for a specific pin.

    // Hardware initialization (recommended)
    RotaryEncoder encoder(2, 3, RotaryEncoder::LatchMode::FOUR0);
    
    // Manual initialization (no hardware setup)
    RotaryEncoder encoder(RotaryEncoder::LatchMode::FOUR0);
  3. Configure LatchMode for encoder sensitivity

    master

    The LatchMode enum defines how the encoder's state changes are interpreted, allowing you to adjust sensitivity or accommodate different wiring configurations.

    • FOUR3: 4 steps, Latch at position 3 only (compatible with older versions).
    • FOUR0: 4 steps, Latch at position 0 (standard for many encoders, handles reverse wirings).
    • TWO03: 2 steps, Latch at position 0 and 3.
    // Example using TWO03 mode
    RotaryEncoder encoder(2, 3, RotaryEncoder::LatchMode::TWO03);
  4. Read encoder position and direction

    master

    Use the following methods to retrieve the current state of the encoder:

    • getPosition(): Returns the current long position.
    • getDirection(): Returns a RotaryEncoder::Direction enum representing the last movement:
      • Direction::NOROTATION (0)
      • Direction::CLOCKWISE (1)
      • Direction::COUNTERCLOCKWISE (-1)
    • setPosition(long newPosition): Manually sets the current position.
    long pos = encoder.getPosition();
    RotaryEncoder::Direction dir = encoder.getDirection();
    
    if (dir == RotaryEncoder::Direction::CLOCKWISE) {
      // Handle clockwise rotation
    }
  5. Update encoder state using tick()

    master

    To process encoder movement, you must call the tick() method regularly (e.g., in your main loop or via an interrupt).

    • Standard tick(): Uses digitalRead() on the pins provided during construction. This is the simplest method but has higher overhead.
    • High-performance tick(int sig1, int sig2): Allows you to pass the raw pin values directly. Use this if you have a faster way to read pins than digitalRead() or are calling it from an interrupt where you've already captured the state.
    // Standard usage in loop
    void loop() {
      encoder.tick();
    }
    
    // High-performance usage (passing values directly)
    void loop() {
      int s1 = digitalRead(2);
      int s2 = digitalRead(3);
      encoder.tick(s1, s2);
    }
  6. Calculate encoder RPM and rotation timing

    master

    The library provides built-in support for measuring the speed of rotation:

    • getRPM(): Returns the current Revolutions Per Minute (RPM) as an unsigned long.
    • getMillisBetweenRotations(): Returns the time in milliseconds between the current and previous observed rotation.
    unsigned long rpm = encoder.getRPM();
    unsigned long msBetween = encoder.getMillisBetweenRotations();