Pro Micro HID Controller monitored over i2c by Arduino Uno

Hi Community,

Short term:
I need to monitor the state of six buttons and illuminate an LED if one of the buttons is pressed.

Long term:
I am designing a game controller with a particular force feedback feature. I will be using two arduino boards. The master (uno r3) will receive game telemetry and control a motor and two electric solenoids. The slave board (pro micro) will be seen by the PC as a game controller with six buttons. I need to monitor the state of the six buttons and transmit this state over i2c.

Before I delve into the i2c part of the programming I wanted to test what I have so far using an LED to monitor button state.

I’ve completed the code for the slave board and it works as a game controller but the LED is not doing what I expect. The LED illuminates while the button on pin 10 is pressed but for the other buttons the LED just flashes once (very dimly).

Can anyone identify what I have done wrong?

#include <Joystick.h>
//declare joystick
Joystick_ Joystick;
//Led monitoring pin
const int ledPin = 15;
// first pin mapped to a button
const int pinToButtonMap = 5;
// array of last state of the buttons
int lastButtonState[6] = {0,0,0,0,0,0};

void setup() {
 
  pinMode(5, INPUT_PULLUP);
  pinMode(6, INPUT_PULLUP);
  pinMode(7, INPUT_PULLUP);
  pinMode(8, INPUT_PULLUP);
  pinMode(9, INPUT_PULLUP);
  pinMode(10, INPUT_PULLUP);
  pinMode(ledPin, OUTPUT);
  // Initialize Joystick Library
  Joystick.begin();
}

void loop()
{
 
for (int index = 0; index < 6; index++)
  {
    int currentButtonState = !digitalRead(index + pinToButtonMap);
       if (currentButtonState == HIGH){
    digitalWrite(ledPin, HIGH);}
    else{
      digitalWrite(ledPin,LOW);}
   if (currentButtonState != lastButtonState[index]){
      Joystick.setButton(index, currentButtonState);
      lastButtonState[index] = currentButtonState;}
  }
  delay(50);
}

You set the LED by checking the pin state. You're doing that for all 6 buttons, then wait 50ms. If you press the first button, the LED will light up but as soon as the second button is checked the same LED will be turned off again. As the button on pin 10 is the last one checked, it will be on for about 50ms which is enough to be seen.

Thanks pylon,

I think I’ve figured it out! This seems to be working now. The LED lights while any of the six buttons are pressed.

#include <Joystick.h>
//declare joystick
Joystick_ Joystick;
//Led monitoring pin
int ledPin = 15;
// first pin mapped to a button
const int pinToButtonMap = 5;
// array of last state of the buttons
int lastButtonState[6] = {0,0,0,0,0,0};
// used to check the state of each button in the array
int button = 0;
void setup() {
 
  pinMode(5, INPUT_PULLUP);
  pinMode(6, INPUT_PULLUP);
  pinMode(7, INPUT_PULLUP);
  pinMode(8, INPUT_PULLUP);
  pinMode(9, INPUT_PULLUP);
  pinMode(10, INPUT_PULLUP);
  pinMode(ledPin, OUTPUT);
  // Initialize Joystick Library
  Joystick.begin();
}

void loop()
{
  // indication that a button is pressed
  boolean buttonPressed = false;
  for (int i = 0; i < 6; i++){
  if (button != lastButtonState[i]) buttonPressed = true;
  }
  if (buttonPressed == true){
    digitalWrite(ledPin, HIGH);}
    else{
      digitalWrite(ledPin, LOW);
  }
  for (int index = 0; index < 6; index++)
  {
    int currentButtonState = !digitalRead(index + pinToButtonMap);
      
       if (currentButtonState != lastButtonState[index]){
        Joystick.setButton(index, currentButtonState);
        lastButtonState[index] = currentButtonState;}
  }
  delay(50);
  
}