The code posted in the preferred way
/*
$ lsusb
2341:8037 Arduino SA
idVendor 0x2341 Arduino SA
idProduct 0x8037
iManufacturer 1 Arduino LLC
iProduct 2 Arduino Micro
.build.usb_manufacturer
*/
#include <Joystick.h>
#include "Switch.h"
const uint32_t SAMPLE_TIME_MS = 20;
// Normal buttons
const uint8_t TOTAL_BUTTONS = 14; // 6 left, 6 right, 2 shifter
Switch buttons[TOTAL_BUTTONS];
bool previousButtonState[TOTAL_BUTTONS];
const uint8_t BUTTON_PINS[TOTAL_BUTTONS] =
{
2, 3, 4, 5, 6, 7,
21, 20, 19, 18, 15, 14,
8, 16
};
Joystick_ Joystick(
JOYSTICK_DEFAULT_REPORT_ID, JOYSTICK_TYPE_JOYSTICK,
TOTAL_BUTTONS, 0, // Button Count, Hat Switch Count
false, false, false, // no X, Y and Z Axis
false, false, false, // No Rx, Ry, or Rz
false, false, // No rudder or throttle
false, false, false // No accelerator, brake, or steering
);
void setup(void)
{
Joystick.begin(false);
// Serial.begin(115200);
for (uint8_t i = 0; i < TOTAL_BUTTONS; i++)
{
previousButtonState[i] = false;
pinMode(BUTTON_PINS[i], INPUT_PULLUP);
};
// Set shifter GND low
pinMode(9, OUTPUT);
pinMode(10, OUTPUT);
digitalWrite(9, LOW);
digitalWrite(10, LOW);
};
void loop(void)
{
static uint32_t previousTime = millis();
static uint32_t buttonState = 0;
// Map 14 normal buttons
for (uint8_t i = 0; i < TOTAL_BUTTONS; i++)
{
buttons[i].update(!digitalRead(BUTTON_PINS[i]));
if (buttons[i].on() != previousButtonState[i])
{
Joystick.setButton(i, buttons[i].on());
}
previousButtonState[i] = buttons[i].on();
// Serial.print(buttons[i].on());
};
// Serial.println();
// Send new state to the host
if (millis() - previousTime > SAMPLE_TIME_MS)
{
Joystick.sendState();
previousTime = millis();
};
// delay(SAMPLE_TIME_MS);
};
Please note the advice on posting a programming question given in Read this before posting a programming question
In particular note the advice to Auto format code in the IDE and to use code tags when posting code here