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);
}
*/