Linking To Calibration Tool in W10

I have set up a Rotary Encoder to activate a trim wheel for Flight Sim. The encoder works fine in Serial Monitor. The Leonardo connects to my PC creating a game pad, when I open joy.cpl it sees the Arduino and opens the test page but when I rotate the trim wheel it is not recognised. I have tried numerous pieces of codes from Libraries but nothing works.

If I set up a potentiometer using alternate codes it works fine but doesn't give me the range I need.

I have been trying for days to sort this out without success.

I am a complete novice but this is the code I am using with no errors at all. I am lost and please don't use techie speak.
CODE


#include <Joystick.h>

#include "Arduino.h"
#include "Bounce2.h"

int16_t readEncoder();

const uint8_t aPin = 2;
const uint8_t bPin = 3;
const uint8_t debounceTime = 5;

Bounce aInput;
Bounce bInput;

void setup() {
  aInput.attach(aPin, INPUT_PULLUP);
  aInput.interval(debounceTime);
  bInput.attach(bPin, INPUT_PULLUP);
  bInput.interval(debounceTime);
}

void loop() {
  static int16_t lastEncoderValue = 0;

  int16_t encoderValue = readEncoder();
  if (encoderValue != lastEncoderValue) {
    Serial.print(F("New Encoder Value = "));
    Serial.println(encoderValue);
    lastEncoderValue = encoderValue;
  }
}

int16_t readEncoder() {
  static int16_t encoderPosition = 0;

  aInput.update();
  bInput.update();

  if (aInput.rose()) {
    if (bInput.read()) {
      encoderPosition--;
    } else {
      encoderPosition++;
    }
  } else if (aInput.fell()) {
    if (bInput.read()) {
      encoderPosition++;
    } else {
      encoderPosition--;
    }
  }
  return encoderPosition;
}

Welcome to the forums. Please take a moment and read the sticky post at the top of the forum to learn how to properly post your code using code tags. It will help others help you.

As for your code, I see nothing in your code that makes your arduino act like a joystick. It is just printing values out to the serial monitor. Look at the examples that come with the Joystick library.

Can you post the version of the code that works?

This is the analogue code that works. For clarity the Digital code write to Serial Monitor, creates a Joystick in Windows Control Panel and sees joy.cpl calibrator but does communicate with it. In short everything the other code does, except last stage.


#include <HID-Project.h>
#include <HID-Settings.h>







/* Sketch for trim controls as USB game controller

   Requires an Arduino Pro Micro or Leonardo if using as a USB game controller

   V1 3rd January 2020 - tom_nl on thingiverse
   released under GPL

 * *** IMPORTANT *****
   1) this requires the NicohoodHID libraries - install from the library manager in the Arduino IDE
   2) I have not been able to test this with the hardware as it is hardwired to another arduino

*/

// load joystick libraries for NicohoodHID

#include <HID-Project.h>
#include <HID-Settings.h>

// set up the analogue pin assignments for the two pots
// amend these depending on what pins you're using on the Arduino
// any of the analog pins should work - whichever are easiest to wire up

const int elevpin = A1;
const int rudderpin = A0;

// set up the variables and constants for the inputs and outputs

// variables for the raw analogue inputs from the pots

int elev_in; // elevator pot raw value raw
int rudder_in; // rudder pot raw value raw

// variables for the analogue outputs  via USB to the sim

long int elev_out; // elevator trim out value
long int rudder_out; // rudder trim out value

// set minimum and maximum elevator raw pot readings
// standard arduino analog minimum and maximum vaues as we're using the pot's full range

const int elevator_min = 0;
const int elevator_max = 1024;

// set maximum and minimum rudder trim raw pot readings
// as we're not using the full pot range, these will depend on how you mount the rudder arm on the pot
// determine these with the test funxtion as described below and read from the serial monitor

const int rudder_min = 206;
const int rudder_max = 519;

// minimum and maximum possible values for the USB out.
// these are the standard joystick 16 bit integer max/min

const int jsmax = 32767;
const int jsmin = -32767;

// 'sensitivity values' - restrict the trim range for finer control
// if the trim is too sensitive, increase these numbers, too insensitive - decrease these numbers

const int elev_sensitivity = 1; // elevator sensitivity
const int rudder_sensitivity = 1; // rudder sensitivity



void setup()
{
  // enable serial if yuo're using the serial testing function
  // otherwise comment this out
  //  Serial.begin(9600);

  // start NicohoodHID joystick library

  Serial.begin(9600); // comment this out if you're using the serial testing function


}

void loop()
{

  // call the function to read the trim controls
  readaxes();

  // call the function to send the computed values to the computer
  sendjs(); // comment this out if you're using the serialtest function below

  // call the function to test the serial values - uncomment if you're using this
  // serialtest();


}

// functions for reading everything

void readaxes() // function for reading elevator and rudder trim
{
  // read elevator axis - read the analog pin twice with a delay to get round the known arduino analog reading bug

  elev_in = analogRead(elevpin);
  delay (1);
  elev_in = analogRead(elevpin);
  delay (1);

  // compute elevator trim out value - map to 0 to 1024 arduino range to joystick -32767 to +32767 range
  // sensitivity restricts this to a finer range as needed for more control

  elev_out = (constrain(map(elev_in, elevator_min, elevator_max, jsmin, jsmax), jsmin, jsmax)) / elev_sensitivity;


  // read rudder  axis - read the analog pin twice with a delay to get round the known arduino analog reading bug

  rudder_in = analogRead(rudderpin);
  delay (1);
  rudder_in = analogRead(rudderpin);
  delay (1);

  // compute rudder trim out value - map to rudder pot minimum and maximum values to joystick -32767 to +32767 range
  // sensitivity restricts this to a finer range as needed for more control

  rudder_out = (constrain(map(rudder_in, rudder_min, rudder_max, jsmin, jsmax), jsmin, jsmax)) / rudder_sensitivity;


}

void sendjs() // function for sending the JS values
{
  // nicohoodHID - send joystick state

  // write analog axes - nocohoodHID library
  // elevator sent as X axis, rudder sent as y axis

  Gamepad.xAxis(elev_out);
  Gamepad.yAxis(rudder_out);
  Gamepad.write();
  Gamepad.releaseAll();
}

/* following are 'testing' functions to read raw values and send to serial for setting up the rudder min and max deflections

   to use this function, do the following to the sketch

   in 'setup':
   - comment out 'Gamepad.begin();'
   - enable 'serial.begin(9600)

   in 'loop'
   - comment out the sendjs() function
   - remove the comment from the serialtest() function below

   upload and run the sketch, monitor the 'rudder value' in the serial monitor
   move the rudder trim bar and note the minimum and maximum values
   then set the values for the 'rudder_min' and rudder_max" constants to the minimum and maximum values noted
*/

/*
  void serialtest()
  {
  // raw values for testing

  Serial.print(rudder_in);
  Serial.print(" rudder value");
  Serial.println();
  // 1 millisecond delay so as to not overwhelm the USB
  delay(1);
  }
*/

I have looked at every joystick variation and tried adding through Sketch library but nothing works. As I said the Sketch enables a Joystick in Windows control Panel, sees the joy.cpl calibration tool but does interact with it.

When I add #include <HID-Project.h>
#include <HID-Settings.h>

As in the analogue code, the first code doesn't compile. I have also tried Joystick.h again it compiles but doesn't alter final status (ie not talking to joy.cpl) Incidently My flight sim calibration tool also sees it but no interaction.

In the working code, it looks like the code in sendjs is the important part. None of that exists in the encoder version.

Is this the bit you mean.

void sendjs() // function for sending the JS values
{
// nicohoodHID - send joystick state

// write analog axes - nocohoodHID library
// elevator sent as X axis, rudder sent as y axis

Gamepad.xAxis(elev_out);
Gamepad.yAxis(rudder_out);
Gamepad.write();
Gamepad.releaseAll();
}

Do I just copy it into the other code then add the HID links again

@ robertfm
Your posts are making it difficult for people who are trying to help you , for free (not a good practice).

You've already been asked once to read the forum guidelines and post your code using code tags. Here's the link again:

All the information is provided in the posts, I stated I am a novice, the only thing I didn't state is it's an Leonardo Board and I am using an Lenovo ideapad. As for the </> I have no idea how to use them. At least one poster seems to grasp what I have posted and what is the issue.

That's why I included the handy-dandy link that shows you how.

I have now reloaded them however whether they should still b have visible they appeared when I copied code using edit copy for forum.

I would take the working code and splice the encoder reader into it and use the encoder value as the number sent for one of the axes in sendjs.

I know you are trying to help but I have no idea what you mean. Could you perhaps do what needs to be done and post the code.