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.