1

I have been working on a small switch panel for flight sims, but I wanted to use the CYT1100 Digital Rotary encoders to move instruments in-game. I have the code set up to press a certain joystick button for each pulse. The code seems to work fine, but I might have some issues with hardware.

Components:

  • Arduino Micro
  • CYT1100 Digital Rotary Encoders
  • Acer Aspire E5 (Windows 10)

I get serial monitor outputs when I move the board, but the rotary encoders aren't plugged in. The values are around 2 to -2 but don't change depending on the encoders. The joystick buttons are getting almost randomly pressed with no certain pattern. I was wondering if anyone has had issues like this in the past or if I'm missing something.

Here's my code:

#include <Joystick.h>

#define outputA 2
#define outputB 3


int counter = 0;
int aState;
int aLastState;

void setup() {
  Joystick.begin();
  pinMode(2, INPUT);
  pinMode(3, INPUT);
}

void loop() {
  aState = digitalRead(outputA);
  if (aState != aLastState) {
    if (digitalRead(outputB) != aState) {      // Clockwise
      Joystick.pressButton(1);
      delay(10);
      Joystick.releaseButton(1);
      counter ++;
    } else {                   // Counterclockwise
      Joystick.pressButton(2);
      delay(10);
      Joystick.releaseButton(2);
      counter --;
    }
    Serial.print("Counter:  ");
    Serial.println(counter);
  }
  aLastState = aState;
}
Jacob C
  • 11
  • 4
  • you have it partially backward ... wait for a transition in `outputA` in one direction only, either a rising edge or falling edge, but not both ... when transition occurs, read value of `outputB` and press joystick button depending on the value – jsotola Jan 23 '20 at 00:52
  • Sorry I'm sort of new to C++ but not programming in general, so I the logic. I wait for a change in outputA, and directly after, if outputB is not down, I have a falling edge and vice versa? – Jacob C Jan 23 '20 at 01:12
  • no ... the rotary encoder probably has two outputs that are 90 degrees apart ... read the state of `B` everytime `A` changes from LOW to HIGH .... `A` always changes in middle of `B` ... http://www.bristolwatch.com/ele2/img/Encoder.jpg ........ it has nothing to do with C++ – jsotola Jan 23 '20 at 02:09
  • Okay thanks! I'll try that and it should fix some issues. – Jacob C Jan 23 '20 at 12:51
  • 1
    I updated the code... Does that look about right? – Jacob C Jan 23 '20 at 15:38
  • definitely not ... the code does not check for a transition in the signal ... your previous code did ... it acted on both transitions instead of only one – jsotola Jan 23 '20 at 16:06

0 Answers0