I got this code for my toggle switch and it doesn't work.What is wrong in this code?

I got this code for my toggle switch and it doesn't work.What is wrong in this code?
I connected one pin to ground and another to digital pin no 9.

#include <Joystick.h>

Joystick_ Joystick;

void setup() {
  
  pinMode(9, INPUT_PULLUP);
  

  
  Joystick.begin();
}


const int pinToButtonMap = 9;


int lastButtonState[1] = {0};

void loop() {

  
  for (int index = 0; index < 1; index++)
  {
    int currentButtonState = !digitalRead(index + pinToButtonMap);
    if (currentButtonState != lastButtonState[index])
    {
      Joystick.setButton(index+1, currentButtonState);
      lastButtonState[index] = currentButtonState;
    }
  }

  delay(50);
}

What does one button have to do with any joystick stuff?

What is the program you posted doing that it shouldn't, or not doing that it should?

a7

Hi
for the "for"?

for ...----->>>>> index < 1 = 0

When I turn on the switch nothing happens .

I didn't understand can you elaborate please

int lastButtonState[1] = {0};

You initialize this with one value (which has index 0) and you try to access index 1 in the for loop which is not present. Hope this helps

I can’t see anything in your code that woukd make anything happen.

What do you expect to happen? Why do you think the code you have presented should do that?

Again, what is the roll of the joystick in this undertaking?

a7

I want to add multiple buttons to my simulator.So, I'm starting with one .The one above I showed I copied from the joystick library examples to be honest and it didn't worked .I am new to Arduino programming as well.Let's say I want this button to extend my landing gear how can I make this happen ?

I don't think I am accessing index 1 value in the for loop .I am using from 0 to less than 1 that will be only 0 , isn't it?

If you only want to access one element in the array then x=arrayelement[0]?

My bad, my eyes are joking me tonight