Help with joystick and rotary encoder

So I"m fairly new to arduino and have been doing tutorials the last couple weeks with the kit I got. Been making some great progress so wanted to actually build something useful. I do a lot of flight simulator flying and wanted to build a box that would work with the Garmin 1000 avionics. Now basically this Garmin 1000 unit has lots of pushbuttons and 6 dual rotary encoders.

So being new I figured I’d get some single rotary encoders, since they are cheap and learn how to make them work. These encoder have a pushbutton on them as well. I found some code that detect the button press and the rotation that the encoder is spinning. So I figured I’d add the Joystick library to it and make it talk to Windows 10. I’m using a Arduino Micro Pro for this although will get a leanardo later.

So for the button press I have it send a joystick signal to press and then release the button when the person lets go. But I’m having no luck with the rotary. Essentially what I need it to do is when its spun in one direction send a signal for one joystick button, and when it’s spun in the other direction, send a signal for another joystick button.

Here’s the code as it is right now:

#include <Joystick.h>

// Create the Joystick
Joystick_ Joystick;

// Rotary Encoder Inputs
#define CLK 6
#define DT 7
#define SW 16

int counter = 0;
int currentStateCLK;
int lastStateCLK;
String currentDir ="";
unsigned long lastButtonPress = 0;

void setup() {

  // Setup Serial Monitor

  // Read the initial state of CLK
  lastStateCLK = digitalRead(CLK);

   // Initialize Joystick Library

void loop() {
  // Read the current state of CLK
  currentStateCLK = digitalRead(CLK);

  // If last and current state of CLK are different, then pulse occurred
  // React to only 1 state change to avoid double count
  if (currentStateCLK != lastStateCLK  && currentStateCLK == 1){

    // If the DT state is different than the CLK state then
    // the encoder is rotating CCW so decrement
    if (digitalRead(DT) != currentStateCLK) {
      currentDir ="CCW";
    } else {
      // Encoder is rotating CW so increment
      currentDir ="CW";

    Serial.print("Direction: ");

  // Remember last CLK state
  lastStateCLK = currentStateCLK;

  // Read the button state
  int btnState = digitalRead(SW);

  //If we detect LOW signal, button is pressed
  if (btnState == LOW) {
    //if 50ms have passed since last LOW pulse, it means that the
    //button has been pressed, released and pressed again
    if (millis() - lastButtonPress > 50) {
      Serial.println("Button pressed!");
    // Remember last button press event
    lastButtonPress = millis();

  // Put in a slight delay to help debounce the reading


I’m trying to keep it simple not to get too advance for me. Hopefully its something simple I’m missing.

I think you might benefit from the work of others as far as the encoders go. I would download Paul's library from here and get one or more of his examples working.

if the micro you are using does not have QEI decoder hardware this is a software equivalent

use Interrupt On Change to detect the state changes on A and B channels