no match for 'operator[]' (operand types are 'Joystick_' and 'int')

Hey guys,

I’m trying to update a selected number of buttons using the Joystick library, according to the reading this can be done using the following command

Joystick.sendState();

I set up the following

int B3 = 0;

Joystick[B3].sendState();

and it just spits back at me

no match for 'operator[]' (operand types are 'Joystick_' and 'int')

Which has me confused because it’ve clearly declared B3 as a variable.

In the library example it’s use like this and it works fine.

int gJoystickId = 0;

Joystick[gJoystickId].sendState();

Full code here

//include joystick library
#include <Joystick.h>
#include <Encoder.h>

//Define Encoders
Encoder myEnc(2, 3);


//Map Button name alias to pins Syntax= define <Name\Pin>
#define NUM_BUTTONS 14
#define B1 8
#define B2 9
int B3 = 10;
#define B4 11
#define B5 12
#define B6 13
#define B7 18
#define B8 19
#define B9 20
#define B10 21
#define B11 22
#define B12 23

// Set Map Button names/pins to button numbers
const byte buttonPins[12] = {B1, B2, B3, B4, B5, B6, B7, B8, B9, B10, B11, B12};


//Setup joystick functions
Joystick_ Joystick(JOYSTICK_DEFAULT_REPORT_ID,JOYSTICK_TYPE_GAMEPAD,
  NUM_BUTTONS, 0,        // Button Count, Hat Switch Count
  false, false, false,   // X and Y, but no Z Axis
  false, false, false,   // No Rx, Ry, or Rz
  false, false,          // No rudder or throttle
  false, false, false);  // No accelerator, brake, or steering

const bool testAutoSendMode = false;

void setup() {
  // Change pins to input and enable pullup resistors
  pinMode(B1, INPUT_PULLUP);
  pinMode(B2, INPUT_PULLUP);
  pinMode(B3, INPUT_PULLUP);
  pinMode(B4, INPUT_PULLUP);
  pinMode(B5, INPUT_PULLUP);
  pinMode(B6, INPUT_PULLUP);
  pinMode(B7, INPUT_PULLUP);
  pinMode(B8, INPUT_PULLUP);
  pinMode(B9, INPUT_PULLUP);
  pinMode(B10, INPUT_PULLUP);
  pinMode(B11, INPUT_PULLUP);
  pinMode(B12, INPUT_PULLUP);

  

// Initialize Joystick Library
  Joystick.begin(false);
}


long oldPosition  = -999;

void loop() {

  
  // Read Button
  bool buttonVals[NUM_BUTTONS];
  //int B3 = 10;
  for (int index =0; index < NUM_BUTTONS; index++){
      buttonVals[index] = !digitalRead(buttonPins[index]);
  }
  
    for(int i = 0; i < NUM_BUTTONS; i++) {
     // Serial.write(NUM_BUTTONS);
      Joystick.setButton(i, buttonVals[i]);
      Joystick.sendState();
    }

      long newPosition = myEnc.read();
  if (newPosition > oldPosition +2 || newPosition < oldPosition -2) {
     Serial.println("Movement Detected");
    if (newPosition > oldPosition) { 
    Serial.println("Clockwise"); //This is just so I can tell when it's being triggered 
    oldPosition = newPosition;
    Joystick.setButton(12, 1);
    delay(50); 
    Joystick.setButton(12, 0);
    }    
  else if (newPosition < oldPosition) {
    Serial.println("CounterClockwise"); //This is just so I can tell when it's being triggered 
    oldPosition = newPosition;
    Joystick.setButton(13, 1);
    delay(50);
    Joystick.setButton(13, 0);
  }
  }
 else {
  
  delay(50);
  Joystick.setButton(12,0);
  delay(50);
 }
}

You have only declared one joystick.

This construct:

Joystick[B3].sendState();

Requires that you actually have an array of them, which you don't. Hence the error.

So I have this bit of code up above and originally I tried this. I might be totally wrong, but this is an array, right?

const byte buttonPins[12] = {B1, B2, B3, B4, B5, B6, B7, B8, B9, B10, B11, B12};

Joystick[buttonPins].sendState();

and the error I got for that was

no match for 'operator[]' (operand types are 'Joystick_' and 'const byte [12] {aka const unsigned char [12]}')

Your code doesn’t have an array of Joystick_ objects. In fact, the code you posted doesn’t even include the line you’re asking about:

Joystick[B3].sendState();

So, it’s unclear what you’re trying to do or what the problem is.

The example from the library is using multiple joysticks, declared as an array. It's not applicable to your situation where you only have one.