/* used as a point of departure by alto777 who reserves nothing */

// https://wokwi.com/projects/353391956123907073
// https://forum.arduino.cc/t/replace-quadrature-output-trackball-with-analog-joystick-using-arduino/1075102

/* ============================================
  code is placed under the MIT license
  Copyright (c) 2023 J-M-L
  For the Arduino Forum : https://forum.arduino.cc/u/j-m-l

  Permission is hereby granted, free of charge, to any person obtaining a copy
  of this software and associated documentation files (the "Software"), to deal
  in the Software without restriction, including without limitation the rights
  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  copies of the Software, and to permit persons to whom the Software is
  furnished to do so, subject to the following conditions:

  The above copyright notice and this permission notice shall be included in
  all copies or substantial portions of the Software.

  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  THE SOFTWARE.
  ===============================================
*/

const byte xAxisPin = A0;
const byte xCLKPin = 2;
const byte xDTPin = 3;

const int xCenter = 512;   // joystick is centered when reading 512
const int xDeadZone = 100; // joystick will be considered still with reading in -xDeadZone and +xDeadZone around the xCenter (so here reading in [312, 712] => no pulse)

// much faster values
//const unsigned long quadraturePeriod = 1;     // in ms
//const unsigned long tickPeriod = 100;         // in ms

// demo mode to see what's going on with the LEDs
const unsigned long quadraturePeriod = 200;   // in ms
// const unsigned long tickPeriod = 100;        // in ms

unsigned long xLastTick;

void setup() {
  Serial.begin(115200);
  Serial.println("rotary hack too\n");

  pinMode(xCLKPin, OUTPUT);
  pinMode(xDTPin, OUTPUT);
  digitalWrite(xCLKPin, HIGH);
  digitalWrite(xDTPin, HIGH);
}

void loop() {

  int potValue = analogRead(xAxisPin);

  byte direction = potValue < 512 ? -1 : 1;
  int distance = abs(512 - potValue);
  int tickPeriod = map(distance, 0, 512, 1000, 50);

  if (distance > xDeadZone) {

    if (millis() - xLastTick >= tickPeriod) {
      if (potValue > (xCenter + xDeadZone)) {
        quadFSM(1);
      }
      else if (potValue < (xCenter - xDeadZone)) {
        quadFSM(-1);
      }
      xLastTick += tickPeriod;
    }
  }
}

byte quadFSM(byte direction)
{
  static byte state;

  switch (state) {
  case 0 :
    digitalWrite(xDTPin, LOW);
    state += direction;

    break;

  case 1 :
    digitalWrite(xCLKPin, LOW);
    state += direction;

    break;
      
  case 2 :
    digitalWrite(xDTPin, HIGH);
    state += direction;

    break;

  case 3 :
    digitalWrite(xCLKPin, HIGH);
    state += direction;

    break;
  }

  state &= 0x3;
}

/*
  if (0) {
    Serial.print("go  ");
    Serial.print(direction);
    Serial.print("  at rate ");
    Serial.print(tickPeriod);
    Serial.print(" to cover ");
    Serial.println(distance);
  }
*/
CLK
DT
X axis
© J-M-L for the arduino forum demo code for generating a quadrature signal
hacked a bit @alto777
<------ dead zone ------>
<- Increment ->
<- Decrement ->