How do use the pins I want

This might be kind of long, so pack a lunch. Also from what I can tell this is the right place to ask this. If not I apologize, please tell me where it would be better placed.

I have a basic understand of programming. I can look at things and figure out what is doing what, but I work better off of examples.

I have an electric truck shift knob that I want to use for sim truck driving.

I'm currently uses a modified version of the JoystickButton example that comes with Joystick Library found here.

Here is what I have that works.

#include <Joystick.h>

Joystick_ Joystick(JOYSTICK_DEFAULT_REPORT_ID, 
  JOYSTICK_TYPE_JOYSTICK, 3, 0,
  false, false, false, false, false, false,
  false, false, false, false, false);

void setup() {
  // Initialize Button Pins
  pinMode(2, INPUT_PULLUP);
  pinMode(4, INPUT_PULLUP);
  pinMode(3, INPUT_PULLUP);


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

// Constant that maps the physical pin to the joystick button.
const int pinToButtonMap = 2;

// Last state of the button
int lastButtonState[3] = {0,0,0};

void loop() {

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

  delay(50);
}

I want to be able to use whatever pins I want. I have my reasons.
If I change 3 to 6 in : ~pinMode(3, INPUT_PULLUP);~
Then when I press button on pin 2, in the control panel buttons 1 and 2 come on but 2 stays on. If I clear that do press button that is on pin 4, then buttons 2 and 3 come on and again 2 stays on. Button on pin 6 does nothing at all. My guess why it does this is because of the index is expecting the pins to be 2, 3, and 4.

I've also tried the example from GamepadExample, part of the same set of examples from the library. I removed the axis stuff. Changed the number of buttons to 3. The difference being:

#include <Joystick.h>

Joystick_ Joystick(JOYSTICK_DEFAULT_REPORT_ID,JOYSTICK_TYPE_GAMEPAD,
  3, 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

void setup() {
  // Initialize Button Pins
  pinMode(2, INPUT_PULLUP);
  pinMode(4, INPUT_PULLUP);
  pinMode(6, INPUT_PULLUP);

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

// Last state of the buttons
int lastButtonState[3] = {0,0,0};

void loop() {

  // Read pin values
  for (int index = 0; index < 3; index++)
  {
    int currentButtonState = !digitalRead(index + 2);
    if (currentButtonState != lastButtonState[index])
    {
      switch (index) {
        case 0: // Range
          Joystick.setButton(0, currentButtonState);
          break;
	case 1: // Split
          Joystick.setButton(1, currentButtonState);
          break;
	case 2: // Engine Brake
          Joystick.setButton(1, currentButtonState);
          break;
      }
      lastButtonState[index] = currentButtonState;
    }
  }

  delay(10);
}

I removed the stuff for the axis.
I get the same result, which again I think is for the same reason.
It looks like this above code is just a cleaner version of this.

I just need to be able to tell the code to look for a signal coming from a pin and then do activate this button in the Game Controls. Once I know that then I can play with it some more for another project I have, but one thing at a time.

There's a video where the person was doing this same project but using a Teensy, he was able to use any pins he wanted. I'll link to that if need be. The problem with the Teensy the control panel is filled with almost everything you'd expect to see. Now if I can get that to show just the 3 buttons that will be used in control panel then I would use that. Well, that is if I can also figure out how to rename the device, which after very long while I found someone that had something I could download and easily do it for the Arduino Leonardo and Mirco. Since I've finally figured out how to get the Teensyduino added to IDE 2.0.3.

Side question that I think I know the answer to: Can I use set the Board to be Teensy (whatever) and use the Teensy code on an Arduino Pro Micro? Prediction is yes, but I don't think I was able to find a clear answer one way or the other but I didn't look to hard into it.

Look this:

I looked at and tried it. First I tried it as is. Then I changed the pins to what I wanted to use (2,4,6). Then I removed the Serial.print lines. They all resulted in the same way.
When I pressed the physical button, nothing would happen in the control panel until I released the button. The next problem was that button in the control panel would stay lit. Even if I pressed the button again there was no change.

What I have is when I press the button it comes on and stays on until I release it. Two of the controls on the shift knob are switches, then there is the one button. And all cases I need them to work when switched on or pressed, and then turn off when switched off or released.

It has solved the problem of being able to use whatever pins I want, so thank you for that, but now it introduces a new problem.

Also it's the little things that get me some times. In this case I like that the order in which they are entered here

const byte buttonPin[NUM_BUTTONS] = {9, 8, 7};

Is the order which are they in the control panel. 9=1, 8=2 and 7=3 so if I screwed up the wiring I can fix it in that line.

Show me your modified version of the code.

Like I said it didn't matter if I changed anything or not but here you go.

#include <Joystick.h>
#include <Bounce2.h> // https://github.com/thomasfredericks/Bounce2/archive/refs/heads/master.zip

#define NUM_BUTTONS 3

const byte buttonPin[NUM_BUTTONS] = {2, 4, 6};

Bounce2::Button button[NUM_BUTTONS] = {Bounce2::Button()};

Joystick_ Joystick(JOYSTICK_DEFAULT_REPORT_ID, JOYSTICK_TYPE_JOYSTICK, 3, 0, false, false, false, false,
                   false, false, false, false, false, false, false);

void setup()
{
  for(int i = 0; i < NUM_BUTTONS; i++)
  {
    button[i].attach(buttonPin[i], INPUT_PULLUP);
    button[i].interval(5);
  }

  Joystick.begin();
}

void loop()
{
  for(int i = 0; i < NUM_BUTTONS; i++)
  {
    button[i].update();
    if (button[i].pressed())
    {
      Joystick.setButton(i, 1);
    }
  }
}

Oddly enough reading through the rest of that topic, the code that aivenssar last posted worked perfectly for me. While he said it was running as if he pressed the button twice, that doesn't happen for me.

The double press is due the lack of debounce on button, for this reason I have added the library to make it easy.

Using the code above you can send zero to joystick when the button is not pressed.

Like this:

#include <Joystick.h>
#include <Bounce2.h> // https://github.com/thomasfredericks/Bounce2/archive/refs/heads/master.zip

#define NUM_BUTTONS 3

const byte buttonPin[NUM_BUTTONS] = {2, 4, 6};

Bounce2::Button button[NUM_BUTTONS] = {Bounce2::Button()};

bool buttonChanged[NUM_BUTTONS] = {false};

Joystick_ Joystick(JOYSTICK_DEFAULT_REPORT_ID, JOYSTICK_TYPE_JOYSTICK, 3, 0, false, false, false, false,
                   false, false, false, false, false, false, false);

void setup()
{
  for(int i = 0; i < NUM_BUTTONS; i++)
  {
    button[i].attach(buttonPin[i], INPUT_PULLUP);
    button[i].interval(5);
  }

  Joystick.begin();
}

void loop()
{
  for(int i = 0; i < NUM_BUTTONS; i++)
  {
    button[i].update();
    if (button[i].pressed())
    {
      buttonChanged[i] = true;
      Joystick.setButton(i, 1);
    }
    else if(buttonChanged[i] == true)
    {
      buttonChanged[i] = false;
      Joystick.setButton(i, 0);
    }
  }
}

It might have been because I was using pretty cheap buttons I got from amazon.

Check it:

Yeah that'll do it.

The game control isn't responding. There's a led on the board that flashes on when I release the button, which was also happening with the previous code I used from you. With the code I posted before and now with aivenssar's code the led flashes on when the button pressed.
The control panel isn't showing any input at all.

Show me the circuit.

Not much to see. It's very simple. A very ghetto setup. Didn't even bother with bread boarding. Originally used just a wire looped on the ground then touched the other end to the pins in turn. Now it's just a momentary push button wired to ground and pin 6 in this picture. https://i.imgur.com/eTlEuzf.jpg

Seems a joke.

How reliable can be this connections?

How this "electric truck shift knob" is connected to this USB port?

This is just for testing. I already have the knob wired up for a temp solution using a zero-delay USB controller (I have video I'm still figuring out how to edit and will have on YT at some point). I didn't want to undo that work just to wire up the Arduino board and have code that doesn't work. I've been at this for a few days now. I want to make sure it works the way I want it to before I put it all together.

Plus I'm going to make a video on building this with the Pro Micro. So in the mean time I use that for testing. When I take a break from the frustrations of the coding, I can still relax by driving and still have working knob.

The only reason I mentioned that this was for a shift knob is because in the distance past I've been vague about what I was doing and was trying to accomplish. It resulted in confusing or less than useful information. I've learned to paint as full a picture as I can so there's less confusion. You're right, with that picture I should made it more clear that this is just a test set up, but then I did say I didn't bother to bread board it. In the end there will be 2 switches and 1 momentary button. This one button can perform the same result as a switch for the purpose of testing.

There's nothing I can do here.

Your problem can be the number of buttons declared on code. There's only 1 button connected but the code has 3 declared.

Also you can use the example JoystickTest to check the communication with your OS.

Are you saying your code can make the Arduino detect how many buttons are connected, even if they aren't active?

If it'll satisfy your curiosity I can set up the same sort of test with three buttons wired the same sort of way to the board and test it. I don't think it would be any different than just one button.

No.

Take this code then open the serial monitor and touch your finger on pins 8 and 9 to see if it will be detected as button press.

#include <Joystick.h>
#include <Bounce2.h> // https://github.com/thomasfredericks/Bounce2/archive/refs/heads/master.zip

#define NUM_BUTTONS 3

const byte buttonPin[NUM_BUTTONS] = {6, 8, 9};

Bounce2::Button button[NUM_BUTTONS] = {Bounce2::Button()};

Joystick_ Joystick(JOYSTICK_DEFAULT_REPORT_ID, JOYSTICK_TYPE_JOYSTICK, 3, 0, false, false, false, false,
                   false, false, false, false, false, false, false);

void setup()
{
  Serial.begin(9600);

  for(int i = 0; i < NUM_BUTTONS; i++)
  {
    button[i].attach(buttonPin[i], INPUT_PULLUP);
    button[i].interval(5);
  }

  Joystick.begin();
}

void loop()
{
  for(int i = 0; i < NUM_BUTTONS; i++)
  {
    button[i].update();
    if (button[i].pressed())
    {
      Joystick.setButton(i, 1);
      Serial.print("Button ");
      Serial.print(i);
      Serial.println(" was pressed");
    }
  }
}

Ok so here's what I did.

I retried the first code you pointed me to and this one. Both with the same results. The Serial monitor, which I didn't know was a thing until just now, does say that button x was pressed but only after it's released. And the button stays lit in the Game Controllers control panel.

I also tried aivenssar's code and I now see why he was getting the print out twice. It's printing it once when pressed and printing again when released. However the important result is the same; in the Games Controllers control panel the button lights up when pressed and turns off when released. I'm not to pretend to know why it prints it twice.

Hi!

You can try the function fell instead pressed.

Change:

if (button[i].pressed())

To:

if (button[i].fell())

Best regards.