Joystick library reading potentiometers irregularly

I can read 6 potentiometers and have the USB control panel in windows display their value.
This is the code that works:

// 
// Controller for dials for a  Flight Simulator 
// using an Arduino Leonardo or Arduino Micro.
// based on code from
// Matthew Heironimus
// 
//------------------------------------------------------------

#include "Joystick.h"

Joystick_ Joystick;
char potPins[] = {
A0,A1,A2,A3,A6,A7
}; 

byte potCount = 6;
int inputPot = 0;

void setup() {
  Joystick.begin();
}

void loop() {
 for (int thisPot = 0; thisPot < potCount; thisPot++) {
    inputPot = analogRead(potPins[thisPot]);
    switch (thisPot) {
     case 0:
      Joystick.setXAxis(inputPot);
     break;
     case 1:
      Joystick.setYAxis(inputPot);
     break;
     case 2:
      Joystick.setZAxis(inputPot);
     break;
     case 3:
      Joystick.setRxAxis(inputPot);
     break;
     case 4:
      Joystick.setRyAxis(inputPot);
     break;
     case 5:
      Joystick.setRzAxis(inputPot);
     break;
     default: 
     break;
    }//end switch
   }//end for
}

I calibrated all 6 potentiometers in the USB control panel settings tab. The raw values range from 0 to 65535. In the calibration dialogs, each potentiometer smoothly moves the values.

However, in the Test tab of the Game Controller control panel the behavior of the X Axis and Y Axis are odd. The slightest rotation from the leftmost position rightwards results in a jump of the X Axis indicator to its midpoint of the excursion. It then moves smoothly to its rightmost limit. The potentiometer mapped to the Y Axis does the same thing. The slightest twist to the right and the indicator on the control panel jumps halfway down. From there it moves smoothly to the bottom.
The Z axis, X Rotation, Y Rotation, and Z Rotation all move smoothly in a linear fashion. However the X Axis and Y Axis behave oddly.

In the original sample code, the joystick library was initialized with a little more complexity like so:

/*
Joystick_ Joystick(JOYSTICK_DEFAULT_REPORT_ID, 
  JOYSTICK_TYPE_MULTI_AXIS, 32, 0,
  true, true, false, false, false, false,
  true, true, false, false, false);
*/

I dont know what that all means but am wondering if windows is expecting the X Axis and Y Axis to go from -32767 to +32767.

I tried changing the relevant lines for those pots to this

  Joystick.begin();
    Joystick.setXAxisRange(-32767,32767);
  Joystick.setYAxisRange(-32767,32767);
}

void loop() {
 for (int thisPot = 0; thisPot < potCount; thisPot++) {
    inputPot = analogRead(potPins[thisPot]);
    switch (thisPot) {
     case 0:
      Joystick.setXAxis(inputPot-32767);
     break;
     case 1:
      Joystick.setYAxis(inputPot-32767);
     break;

Now the X Axis and Y Axis raw values go from 0 to 1023 in the calibration window. Their movement in the calibration window is smooth and corresponds linearly with the twisting of the potentiometer.

In the test window their behavior is still irregular, the indicators jump to 50% with the slightest twist to the right while the remainder of the excursion is linear with the pot.

I know am missing something but need help seeing it.

Why aren't you just reading the AI's directly? You'll get a 0-1023 integer for X and Y on all six pots. You'll need a dozen inputs X/Y x 6 so an UNO won't cut it. Easy-peasy and no mystery #includes or calls needed.

Cheap solution: Replace the (broken, log...?) pots.
You did not happen to attach devices or pullups to the I2C bus?

The pots are linear and not broken. It does not matter which pot I assign to x and y axis- behavior is the same

What are AI s? I am using a pro micro. The issue is with the library I think.

AI = Analog In
AO = Analog Out (something Arduinos simulate with PWM on a DO
DI = Digital In
DO = Digital Out
Since Arduino pins can be either DI or DO some just use the D. If followed by a number it refers to a specific pin rather than generically.

Are these connected to A4 and A5?

The pots are connected to A0,A1,A2,A3,A6,A7

But I did discover that the multiaxis joystick specification is frought with trickiness. Specifying it results in a new device descriptor to windows. This results in the device getting a different com port after its flashed. After a few different changes the number of device descriptors increases and this results in weird behavior. I would get the Arduino in the USB game control panel but it would be totally unresponsive. Sometimes the port numbers would change unpredictably- The pro micro would start out on port 17. During the flash it would change to port 4 and then it would be recognized as port 12. These port numbers were erratic and this happened when I was changing the hidReportId and the joystick type.The documentation states

Default: 0x03 - Indicates the joystick's HID report ID. This value must be unique if you are creating multiple instances of Joystick. Do not use 0x01 or 0x02 as they are used by the built-in Arduino Keyboard and Mouse libraries.

I had to flash an empty sketch to the pro micro. Then go and find all the hidden devices in device manager and delete them and start fresh.

Now it functions well with this

Joystick_ Joystick(JOYSTICK_DEFAULT_REPORT_ID, 
  JOYSTICK_TYPE_MULTI_AXIS, 32, 0,
  true, true, true, true, true, true,
  false, false, false, false, false);

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.