And this sketch is your code modified to allow the reading of all buttons.
#include <Encoder.h>
#include <Joystick.h>
Encoder axisX(0, 1);
Encoder axisY(2, 3);
Encoder axisZ(4, 5);
const byte buttonArray[] = {6, 7, 8, 9, 10, 14, 15, 16, 18, 19, 20, 21};
void setup() {
// Initialize Buttons
for(byte x = 0; x < sizeof(buttonArray); x++)
{
pinMode(buttonArray[x], INPUT_PULLUP);
}
// Initialize Joystick Library
Joystick.begin(false);
}
void loop() {
Joystick.setXAxis(0); // Center joystick X
int newX = axisX.read(); // Read X encoder
if (newX > 0) // Has the value increased since last read
{
Joystick.setXAxis(127); // Set joystick X hard over to right
axisX.write(0); // Zero encoder count for next time
}
if (newX < 0) // Has encoder decreased since last read
{
Joystick.setXAxis(-127); // Set joystick hard left
axisX.write(0); // Zero encoder count
}
Joystick.setYAxis(0); // Center joystick Y
int newY = axisY.read();
if (newY > 0)
{
Joystick.setYAxis(127);
axisY.write(0);
}
if (newY < 0)
{
Joystick.setYAxis(-127);
axisY.write(0);
}
Joystick.setZAxis(0); // Center joystick Z
int newZ = axisZ.read();
if (newZ > 0)
{
Joystick.setZAxis(63);
delay(50);
axisZ.write(0);
}
if (newZ < 0)
{
Joystick.setZAxis(-63);
delay(50);
axisZ.write(0);
}
// Read button pin values
for(byte x = 0; x < sizeof(buttonArray); x++)
{
byte currentButtonState = !digitalRead(buttonArray[x]); // Read button state
Joystick.setButton(x, currentButtonState); // Set state in joystick
}
Joystick.sendState(); // Send the joystick data
delay(50); // Wait a bit
}