u/aksndjkhui

I want to use a rotary encoder in a Teensy 2.0++ environment, but the recognition is strange.

I want to use a rotary encoder in a Teensy 2.0++ environment, but the recognition is strange.

https://reddit.com/link/1srlwz4/video/kssbozefbjwg1/player

I want to use a rotary encoder in a Teensy 2.0++ environment, but the recognition is strange.

In the video, the encoder was rotated in only one direction.

This is my first time working on a project with Teensy, so there might be some parts I don't know well yet.
The joystick input input on the right is the rotary encoder input input.

What I want is for the input to be entered correctly in the direction I rotate the encoder.

I soldered all the wires to the Teensy and am using a direct connection.

I'm not good at English, so I'm using a translator. If there are any parts you don't understand, please leave a comment.

I am using the GHS38-06G50BSCP526 rotary encoder.
https://caltsensor.com/product/shaft-rotary-encoder-ghs38-series/

#include <Encoder.h>


// ===== Pins =====
const int BT_a = 1;
const int BT_b = 2;
const int BT_c = 3;
const int BT_d = 4;


const int start = 9;
const int start_led = 10;


const int FX_a = 13;
const int FX_b = 14;


const int FX_a_led = 15;
const int FX_b_led = 16;


const int vol_a_a = 44;
const int vol_a_b = 45;


const int vol_b_a = 42;
const int vol_b_b = 43;


Encoder nobl(vol_a_a, vol_a_b);
Encoder nobr(vol_b_a, vol_b_b);


// ===== State =====
uint8_t prevStateL = 0;
uint8_t prevStateR = 0;


bool keyborded = false;
bool prevToggle = false;


long rawR = 0;
float filteredR = 0;


long rawL = 0;
float filteredL = 0;


const int maxStep = 20; // Maximum allowed change per step (tuning point)


long prevRawR = 0;
long prevRawL = 0;


const float alpha = 0.1;   // 0~1 (lower = smoother)
const int deadzone = 5;    // Ignore changes below this value


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


  pinMode(BT_a, INPUT_PULLUP);
  pinMode(BT_b, INPUT_PULLUP);
  pinMode(BT_c, INPUT_PULLUP);
  pinMode(BT_d, INPUT_PULLUP);
  pinMode(start, INPUT_PULLUP);
  pinMode(FX_a, INPUT_PULLUP);
  pinMode(FX_b, INPUT_PULLUP);


  pinMode(start_led, OUTPUT);
  pinMode(FX_a_led, OUTPUT);
  pinMode(FX_b_led, OUTPUT);


  pinMode(vol_a_a, INPUT_PULLUP);
  pinMode(vol_a_b, INPUT_PULLUP);
  pinMode(vol_b_a, INPUT_PULLUP);
  pinMode(vol_b_b, INPUT_PULLUP);


  Joystick.X(512);
  Joystick.Y(512);
}


void loop() {


  // Debug encoder signals
  // Serial.print("A:");
  // Serial.print(digitalRead(vol_a_a));
  // Serial.print("      ");
  // Serial.print("B:");
  // Serial.println(digitalRead(vol_a_b));


  // =========================
  // Mode toggle (edge detection)
  // =========================
  bool nowToggle = (digitalRead(start) == LOW &&
                    digitalRead(FX_a) == LOW &&
                    digitalRead(FX_b) == LOW &&
                    digitalRead(BT_a) == HIGH);


  if (nowToggle && !prevToggle) {
    keyborded = !keyborded;


    // Reset inputs (important)
    Keyboard.releaseAll();
    for (int i = 1; i <= 7; i++) {
      Joystick.button(i, 0);
    }


    Serial.println(keyborded ? "Keyboard Mode" : "Joystick Mode");
    delay(200); // debounce
  }
  prevToggle = nowToggle;


  // =========================
  // Button handling
  // =========================
  if (!keyborded) {
    // Joystick mode
    Joystick.button(1, digitalRead(BT_a) == HIGH);
    Joystick.button(2, digitalRead(BT_b) == HIGH);
    Joystick.button(3, digitalRead(BT_c) == HIGH);
    Joystick.button(4, digitalRead(BT_d) == HIGH);
    Joystick.button(5, digitalRead(FX_a) == LOW);
    Joystick.button(6, digitalRead(FX_b) == LOW);
    Joystick.button(7, digitalRead(start) == LOW);
  } else {
    // Keyboard mode
    if (digitalRead(BT_a) == HIGH) Keyboard.press(KEY_S);
    else Keyboard.release(KEY_S);


    if (digitalRead(BT_b) == HIGH) Keyboard.press(KEY_D);
    else Keyboard.release(KEY_D);


    if (digitalRead(BT_c) == HIGH) Keyboard.press(KEY_L);
    else Keyboard.release(KEY_L);


    if (digitalRead(BT_d)==HIGH) Keyboard.press(';');
    else Keyboard.release(';');


    if (digitalRead(FX_a) == LOW) Keyboard.press(KEY_LEFT_SHIFT);
    else Keyboard.release(KEY_LEFT_SHIFT);


    if (digitalRead(FX_b) == LOW) Keyboard.press(KEY_RIGHT_SHIFT);
    else Keyboard.release(KEY_RIGHT_SHIFT);


    if (digitalRead(start) == LOW) Keyboard.press(KEY_ENTER);
    else Keyboard.release(KEY_ENTER);
  }


  int hat = -1;


  // Right encoder
  rawR = nobr.read();
  long deltaR = rawR - prevRawR;


  // Left encoder
  rawL = nobl.read();
  long deltaL = rawL - prevRawL;


  if (deltaL > 0 && deltaR == 0) hat = 0;
  else if (deltaL < 0 && deltaR == 0) hat = 180;
  else if (deltaR > 0 && deltaL == 0) hat = 90;
  else if (deltaR < 0 && deltaL == 0) hat = 270;
  else if (deltaL > 0 && deltaR > 0) hat = 45;
  else if (deltaL > 0 && deltaR < 0) hat = 315;
  else if (deltaL < 0 && deltaR > 0) hat = 135;
  else if (deltaL < 0 && deltaR < 0) hat = 225;


  Joystick.hat(hat);


  prevRawR = rawR;
  prevRawL = rawL;
}
reddit.com
u/aksndjkhui — 14 hours ago