Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 4

//Arduino Joystick 2.0 Library, by MHeironimus (https://github.

com/MHeironimus)
//Beginners Guide, by Daniel Cantore
//Example Code (Oct 2020), with in-depth commenting

//Initial Definitions and Setup


//Libary Inclusion
#include <Wire.h>
#include <Joystick.h>
#include <Adafruit_MCP23X17.h>

Adafruit_MCP23X17 mcp0;
Adafruit_MCP23X17 mcp1;

//Define and Allocate Input Pins to memorable names


//#define joyX A7
//#define joyY A1
//#define joyRZ A3
//#define joyThrottle A2

#define Axis1 A3 //ZAxis // Thumbwheel


#define Axis2 A9 //RxAxis //Throttle R
#define Axis3 A1 //RyAxis // Throttle L
#define Axis4 A7 //RzAxis // Wheel 1
//#define Axis5 A3 //Rudder
#define Axis6 A8 //Throttle //Wheel 2
#define Axis7 A10 //Xaxis //Wheel 3
#define Axis8 A2 //Yaxis //Wheel 4

//Initializing Axis as Integers, at a 0 default value


//int xAxis_ = 0;
//int yAxis_ = 0;
//int rzAxis_ = 0;
//int throttle_ = 0;

int ValAxis1 = 0;
int ValAxis2 = 0;
int ValAxis3 = 0;
int ValAxis4 = 0;
int ValAxis5 = 0;
int ValAxis6 = 0;
int ValAxis7 = 0;
int ValAxis8 = 0;

//Setting up Buttons
//Updating a static variable gives greater stability than reading directly from the
digital pin.
//Giving Default Values to the Buttons for later use
int lastButtonState0[16] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
int lastButtonState1[16] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};

//Defining the Joystick


//The Joystick is defined in the following setup:
//Joystick(Joystick HID ID, Joystick Type, Button Count, Hat Switch Count, Include
X, Include Y, Include Z, Include Rx, Include Ry, Include Rz, Include Rudder,
Include Throttle, Include Accelerator, Include Brake, Include Steering
//Joystick HID ID: A Hex value identifier for HID Device Recognition (default:
0x03). DO NOT USE 0x01 or 0x02
//Joystick type: Define the type of joystick from the types supported. Types:
DEFAULT Joystick (0x04 or JOYSTICK_TYPE_JOYSTICK), Gamepad (0x05 or
JOYSTICK_TYPE_GAMEPAD), Multi-Axis Controller (0x08 or JOYSTICK_TYPE_MULTI_AXIS)
//Button Count: Number of Buttons shown to HID system (default: 32)
//Hat Switch Count: Number of Hat Switches, max 2. (default:2)
//Include X Axis: Determines whether the X axis is avalible for used by the HID
system, defined as a bool value (default:true)
//Include Y Axis: Determines whether the Y axis is avalible for used by the HID
system, defined as a bool value (default:true)
//Include Z Axis: Determines whether the Z axis is avalible for used by the HID
system, defined as a bool value (default:true)
//Include Rx Axis: Determines whether the X Rotational axis is avalible for used by
the HID system, defined as a bool value (default:true)
//Include Ry Axis: Determines whether the Y Rotational axis is avalible for used by
the HID system, defined as a bool value (default:true)
//Include Rz Axis: Determines whether the Z Rotational axis is avalible for used by
the HID system, defined as a bool value (default:true)
//Include Rudder: Determines whether a Rudder axis is avalible for used by the HID
system, defined as a bool value (default:true)
//Include Throttle: Determines whether a Throttle axis is avalible for used by the
HID system, defined as a bool value (default:true)
//Include Accelerator: Determines whether an Accelerator axis is avalible for used
by the HID system, defined as a bool value (default:true)
//Include Brake: Determines whether a Brake axis is avalible for used by the HID
system, defined as a bool value (default:true)
//Include Steering: Determines whether a Steering axis is avalible for used by the
HID system, defined as a bool value (default:true)

//Joystick_ Joystick(0x12, JOYSTICK_TYPE_JOYSTICK, 3,


0,true,true,false,false,false,true,false,true,false,false,false);

Joystick_ Joystick(0x12, JOYSTICK_TYPE_JOYSTICK , 32, 0,


true, // X
true, // Y
true, // Z
true, // Rx
true, // Ry
true, // Rz
false, // Rudder
true, // Throttle
false, // Accelerator
false, // Brake
false); // Steering

//Set Auto Send State


//Enables Auto Sending, allowing the controller to send information to the HID
system, rather than waiting to be asked.
const bool initAutoSendState = true;

void setup() {
Serial.begin(9600);

while(!SerialUSB && millis()<5000) {


//wait for USB serial to connect or 5 seconds to elapse
}
SerialUSB.println("Name Of Sketch");
//Initialize Buttons
if (!mcp0.begin_I2C(0X20)) {
SerialUSB.println("Error MCP0");
while (1);
}
//mcp0.begin_I2C(0); // use address 0X20
for (int index = 0; index < 16; index++)
{
mcp0.pinMode(index, INPUT_PULLUP);
SerialUSB.println("MCP0.PULLUP");
}

if (!mcp1.begin_I2C(0X21)) {
SerialUSB.println("Error MCP1");
while (1);
}
//mcp1.begin_I2C(1); // use address 0X21
for (int index = 0; index < 16; index++)
{
mcp1.pinMode(index, INPUT_PULLUP);
SerialUSB.println("MCP1.PULLUP");
}

//Start Joystick - Needed to start the Joystick function libary


Joystick.begin();
}

void loop() {

//Axis Reading during Runtime


//Setting Read functions for each axis and parsing correctly. The X axis will be
used as an example for explanation

ValAxis1 = analogRead(Axis1);
ValAxis1 = map(ValAxis1,0,1023,0,255);
Joystick.setZAxis(ValAxis1);

ValAxis2 = analogRead(Axis2);
ValAxis2 = map(ValAxis2,0,1023,0,255);
Joystick.setRxAxis(ValAxis2);

ValAxis3 = analogRead(Axis3);
//ValAxis3 = map(ValAxis3,0,1023,0,255);
ValAxis3 = map(ValAxis3,0,1023,255,0);
Joystick.setRyAxis(ValAxis3);

ValAxis4 = analogRead(Axis4);
//ValAxis4 = map(ValAxis4,0,1023,0,255);
ValAxis4 = map(ValAxis4,0,1023,255,0); // Invert
Joystick.setRzAxis(ValAxis4);

// ValAxis5 = analogRead(Axis5);
// ValAxis5 = map(ValAxis5,0,1023,0,255);
// Joystick.setRudder(ValAxis5);
ValAxis6 = analogRead(Axis6);
ValAxis6 = map(ValAxis6,0,1023,0,255);
Joystick.setThrottle(ValAxis6);

ValAxis7 = analogRead(Axis7);
ValAxis7 = map(ValAxis7,0,1023,0,255);
Joystick.setXAxis(ValAxis7);

ValAxis8 = analogRead(Axis8);
ValAxis8 = map(ValAxis8,0,1023,0,255);
Joystick.setYAxis(ValAxis8);

//Button Reading during Runtime


//Setting Read functions for each button, using a state value for memory. Button
1 will be used as an example for explanation

// Read pin values mcp0


for (int index = 0; index < 16; index++)
{
int currentButtonState0 = !mcp0.digitalRead(index);
if (currentButtonState0 != lastButtonState0[index])
{
Joystick.setButton(index, currentButtonState0);
lastButtonState0[index] = currentButtonState0;
SerialUSB.println("MCP0 BUTTON");
}
}

// Read pin values mcp1


for (int index = 0; index < 16; index++)
{
int currentButtonState1 = !mcp1.digitalRead(index);
if (currentButtonState1 != lastButtonState1[index])
{
Joystick.setButton(index+16, currentButtonState1);
lastButtonState1[index] = currentButtonState1;
SerialUSB.println("MCP1 BUTTON");
}
}

//Pole Delay/Debounce
//To reduce unessecary processing, the frequency of the reading loop is delayed.
The value(in ms) can be changed to match requirement
delay(10);
Joystick.sendState();
}

You might also like