Oh, that's a fantastic point. In case anybody else is looking to do something similar to this, here is the code that is working for me to simulate a dual-encoder industrial trackball with a 2-axis analog joystick. Much thanks to @J-M-L and @alto777 for the heavy lifting on this one (and I appreciate that you made me struggle through adapting it to two-axis myself instead of spoonfeeding--I learned a ton!):
/* Thanks to alto777 and J-M-L for this code */
// https://wokwi.com/projects/353851175863937025
// 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 GeddyT
For the Arduino Forum : https://forum.arduino.cc/u/geddyt
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 yAxisPin = A1;
const byte xCLKPin = 2;
const byte xDTPin = 3;
const byte yCLKPin = 4;
const byte yDTPin = 5;
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)
const int yCenter = 512;
const int yDeadZone = 100;
// 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;
unsigned long yLastTick;
void setup() {
Serial.begin(115200);
pinMode(xCLKPin, OUTPUT);
pinMode(xDTPin, OUTPUT);
pinMode(yCLKPin, OUTPUT);
pinMode(yDTPin, OUTPUT);
digitalWrite(xCLKPin, HIGH);
digitalWrite(xDTPin, HIGH);
digitalWrite(yCLKPin, HIGH);
digitalWrite(yDTPin, HIGH);
}
void loop() {
int xpotValue = analogRead(xAxisPin);
byte xdirection = xpotValue < 512 ? -1 : 1;
int xdistance = abs(512 - xpotValue);
int xtickPeriod = map(xdistance, 0, 512, 1000, 50);
if (xdistance > xDeadZone) {
if (millis() - xLastTick >= xtickPeriod) {
if (xpotValue > (xCenter + xDeadZone)) {
xquadFSM(1);
}
else if (xpotValue < (xCenter - xDeadZone)) {
xquadFSM(-1);
}
xLastTick += xtickPeriod;
}
}
int ypotValue = analogRead(yAxisPin);
byte ydirection = ypotValue < 512 ? -1 : 1;
int ydistance = abs(512 - ypotValue);
int ytickPeriod = map(ydistance, 0, 512, 1000, 50);
if (ydistance > yDeadZone) {
if (millis() - yLastTick >= ytickPeriod) {
if (ypotValue > (yCenter + yDeadZone)) {
yquadFSM(1);
}
else if (ypotValue < (yCenter - yDeadZone)) {
yquadFSM(-1);
}
yLastTick += ytickPeriod;
}
}
}
byte xquadFSM(byte xdirection)
{
static byte state;
switch (state) {
case 0 :
digitalWrite(xDTPin, LOW);
state += xdirection;
break;
case 1 :
digitalWrite(xCLKPin, LOW);
state += xdirection;
break;
case 2 :
digitalWrite(xDTPin, HIGH);
state += xdirection;
break;
case 3 :
digitalWrite(xCLKPin, HIGH);
state += xdirection;
break;
}
state &= 0x3;
}
byte yquadFSM(byte ydirection)
{
static byte state;
switch (state) {
case 0 :
digitalWrite(yDTPin, LOW);
state += ydirection;
break;
case 1 :
digitalWrite(yCLKPin, LOW);
state += ydirection;
break;
case 2 :
digitalWrite(yDTPin, HIGH);
state += ydirection;
break;
case 3 :
digitalWrite(yCLKPin, HIGH);
state += ydirection;
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);
}
*/
The cursor needs sped up (a lot...), so I'm going to change the "const unsigned long quadraturePeriod" value from 200 to first 10 and then 1 if that's still not fast enough (I see the latter is commented out in the code for just this reason). Interestingly, the simulator looks exactly the same no matter what that value is, but I'm guessing that's not how the actual cursor on the machine will behave.
Just a mess of wires, an Uno, a $13 joystick, and a really dirty VMC console, but here's the test setup from a couple of nights ago:
There will be one more picture left in this thread, and that will be a week or so from now, when I have a mounting plate machined up and everything all nice and tidy in the cabinet.
If anyone is interested, the specific machine this is going in is a 2006 Hurco VMX30. The trackball unit was, I believe, a BEI Sensors TBS225. The trackball has a 15-pin gameport connector. Pins 1-4 are the quadrature signals (Xa, Xb, Ya, Yb) and are wired to the Uno's 3-6 digital output pins. Trackball connector pin 15 is +5V and is wired to Uno's Vin. Trackball connector pin 13 is 5V return and is wired to Uno's GND pin.
This will effectively replace the trackballs that are probably failing in every single old Hurco that has an Ultimax control and be a much more robust solution. It's possible that different control generations (Ultimax I, II, III, IV) have different pinouts, but I'm not sure.