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;
}
Here's another version of the code based on jsotola's suggestions:
#include <Joystick.h>
#define outputA 2
#define outputB 3
int counter = 0;
int aState;
int bState;
void setup() {
Joystick.begin();
pinMode(2, INPUT);
pinMode(3, INPUT);
}
void loop() {
aState = digitalRead(outputA)
bState = digitalRead(outputA)
if (aState == HIGH) {
if (bState == HIGH) {
counter -= 1;
}
if (bState == LOW) {
counter -= 1;
}
}
}