GithubHelp home page GithubHelp logo

Comments (14)

MHeironimus avatar MHeironimus commented on May 21, 2024

Please post your sketch file and describe how you are wiring up the buttons.

from arduinojoysticklibrary.

jbenam avatar jbenam commented on May 21, 2024

Hi Matthew, thanks for your reply.

I'm just using the GamepadExample sketch you've provided.

The only thing I've changed are the 5 pinModes:

// Initialize Button Pins
pinMode(3, INPUT_PULLUP);
pinMode(4, INPUT_PULLUP);
pinMode(5, INPUT_PULLUP);
pinMode(6, INPUT_PULLUP);
pinMode(7, INPUT_PULLUP);`

The worst part is that the issue happens without anything connected to the Leonardo, as well. Doesn't happen if I just upload the untouched example, though.

Buttons (and the stick) are just wired up with a common ground to GND and the other wire to the various digital pins (I'm usually testing with 2 to 7).

Thanks again.

from arduinojoysticklibrary.

SoylentGraham avatar SoylentGraham commented on May 21, 2024

When nothing is connected to an arduino pin, you cannot guarantee what the voltage/digital reading of that pin will be.

from arduinojoysticklibrary.

jbenam avatar jbenam commented on May 21, 2024

Right, I know as much - but I've tested it with everything connected to it as well and the issue still manifests itself in the same exact way.

from arduinojoysticklibrary.

BleuLlama avatar BleuLlama commented on May 21, 2024

from arduinojoysticklibrary.

MHeironimus avatar MHeironimus commented on May 21, 2024

@jbenam Can you post your entire sketch file? The issue may be in how you are processing the inputs or how you are sending them to the Joystick library.

from arduinojoysticklibrary.

jbenam avatar jbenam commented on May 21, 2024

@MHeironimus
Hi Matthew, it's just your example code with the pin numbers changed. Here it is:

#include <Joystick.h>

Joystick_ Joystick(JOYSTICK_DEFAULT_REPORT_ID,JOYSTICK_TYPE_GAMEPAD,
  1, 0,                  // Button Count, Hat Switch Count
  true, true, 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(3, INPUT_PULLUP);
  pinMode(4, INPUT_PULLUP);
  pinMode(5, INPUT_PULLUP);
  pinMode(6, INPUT_PULLUP);
  pinMode(7, INPUT_PULLUP);

  // Initialize Joystick Library
  Joystick.begin();
  Joystick.setXAxisRange(-1, 1);
  Joystick.setYAxisRange(-1, 1);
}

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

void loop() {

  // Read pin values
  for (int index = 0; index < 5; index++)
  {
    int currentButtonState = !digitalRead(index + 2);
    if (currentButtonState != lastButtonState[index])
    {
      switch (index) {
        case 0: // UP
          if (currentButtonState == 1) {
            Joystick.setYAxis(-1);
          } else {
            Joystick.setYAxis(0);
          }
          break;
        case 1: // RIGHT
          if (currentButtonState == 1) {
            Joystick.setXAxis(1);
          } else {
            Joystick.setXAxis(0);
          }
          break;
        case 2: // DOWN
          if (currentButtonState == 1) {
            Joystick.setYAxis(1);
          } else {
            Joystick.setYAxis(0);
          }
          break;
        case 3: // LEFT
          if (currentButtonState == 1) {
            Joystick.setXAxis(-1);
          } else {
            Joystick.setXAxis(0);
          }
          break;
        case 4: // FIRE
          Joystick.setButton(0, currentButtonState);
          break;
      }
      lastButtonState[index] = currentButtonState;
    }
  }

  delay(10);
}

Just uploaded this to my Leonardo and it's going bonkers - it goes up all by itself randomly.
Thanks again.

from arduinojoysticklibrary.

MHeironimus avatar MHeironimus commented on May 21, 2024

@jbenam The only thing I see wrong with your sketch file is the following line:

    int currentButtonState = !digitalRead(index + 2);

should read

    int currentButtonState = !digitalRead(index + 3);

assuming you want pin 3 to be up, pin 4 to be right, 5 to be down, 6 to be left, and 7 to be fire.

I uploaded your sketch (which the change above) to my Leonardo and it worked without error.

You may want to try to get your hands on another Leonardo or Micro and see if you can reproduce the issue.

from arduinojoysticklibrary.

BleuLlama avatar BleuLlama commented on May 21, 2024

from arduinojoysticklibrary.

BleuLlama avatar BleuLlama commented on May 21, 2024

from arduinojoysticklibrary.

thedrumm avatar thedrumm commented on May 21, 2024

I have to say I got the same issues, but in my case I did some changes for using more buttons.
I uploaded the program into my arduino micro (chinesse) several times; without making any change into the code.
It worked at least really well.

from arduinojoysticklibrary.

Sylverzerom avatar Sylverzerom commented on May 21, 2024

this might help with your problem, i used arrays to store the pins.

// The pins are grounded when they are pressed.
// Pin A0 = UP
// Pin A1 = DOWN
// Pin A2 = LEFT
// Pin A3 = RIGHT
// Pin 2 = A
// Pin 3 = B
// Pin 4 = Start
// Pin 5 = Select
// Pin 6 = L Shoulder
// Pin 7 = R Shoulder
// Pin 8 = Modifier

//reserved pins for later
// Pin 9 = X
// Pin 10 = Y
// Pin 11 = L trigger
// Pin 12 = R trigger
//
// by Sylverzerom
// 05-05-2017
//--------------------------------------------------------------------

#include <Joystick.h>

Joystick_ Joystick(JOYSTICK_DEFAULT_REPORT_ID,JOYSTICK_TYPE_GAMEPAD,
7, 0, // Button Count, Hat Switch Count
true, true, 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

// Last state of the buttons _ number of array is pins used
int lastButtonState[11] = {0,0,0,0,0,0,0,0,0,0,0};
word input_pins[11] = {A0,A1,A2,A3,2,3,4,5,6,7,8};

void setup() {
// Initialize Button Pins

for (int index = 0; index < 11; index++)
{
pinMode(input_pins[index], INPUT_PULLUP);
}
// pinMode(3, INPUT_PULLUP);
//pinMode(4, INPUT_PULLUP);
//pinMode(5, INPUT_PULLUP);
//pinMode(6, INPUT_PULLUP);
//pinMode(A1, INPUT_PULLUP);

// Initialize Joystick Library
Joystick.begin();
Joystick.setXAxisRange(-1, 1);
Joystick.setYAxisRange(-1, 1);
}

void loop() {
RXLED0;
TXLED0;

// Read pin values
for (int index = 0; index < 11; index++)
{
int currentButtonState = !digitalRead(input_pins[index]);
if (currentButtonState != lastButtonState[index])
{
switch (index) {
case 0: // UP
if (currentButtonState == 1) {
Joystick.setYAxis(-1);
} else {
Joystick.setYAxis(0);
}
break;
case 1: // DOWN
if (currentButtonState == 1) {
Joystick.setYAxis(1);
} else {
Joystick.setYAxis(0);
}
break;
case 2: // LEFT
if (currentButtonState == 1) {
Joystick.setXAxis(-1);
} else {
Joystick.setXAxis(0);
}
break;
case 3: // RIGHT
if (currentButtonState == 1) {
Joystick.setXAxis(1);
} else {
Joystick.setXAxis(0);
}
break;
case 4: // A
Joystick.setButton(0, currentButtonState);
break;
case 5: // B
Joystick.setButton(1, currentButtonState);
break;
case 6: // Start
Joystick.setButton(2, currentButtonState);
break;
case 7: // Select
Joystick.setButton(3, currentButtonState);
break;
case 8: // L Shoulder
Joystick.setButton(4, currentButtonState);
break;
case 9: // R Shoulder
Joystick.setButton(5, currentButtonState);
break;
case 10: // Modifier
Joystick.setButton(6, currentButtonState);
break;
}
lastButtonState[index] = currentButtonState;
}
}

}

from arduinojoysticklibrary.

jbenam avatar jbenam commented on May 21, 2024

This is what I ended up doing instead, works pretty well:

#include <Joystick.h>

Joystick_ Joystick(JOYSTICK_DEFAULT_REPORT_ID,JOYSTICK_TYPE_GAMEPAD,
  5, 0,                  // Button Count, Hat Switch Count
  true, true, 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() {
  pinMode(10, INPUT_PULLUP); // UP
  pinMode(11, INPUT_PULLUP); // RIGHT
  pinMode(12, INPUT_PULLUP); // DOWN
  pinMode(13, INPUT_PULLUP); // LEFT
  pinMode(3, INPUT_PULLUP); // 0
  pinMode(4, INPUT_PULLUP); // 1
  pinMode(5, INPUT_PULLUP); // 2
  pinMode(6, INPUT_PULLUP); // 3
  pinMode(7, INPUT_PULLUP); // 4

  // Initialize Joystick Library
  Joystick.begin();
  Joystick.setXAxisRange(-1, 1);
  Joystick.setYAxisRange(-1, 1);
}

int lastStickState[4] = {0,0,0,0};
int lastButtonState[5] = {0,0,0,0,0};

void loop() {

  for (int stickindex = 0; stickindex < 4; stickindex++)
  {
    int currentStickState = !digitalRead(stickindex + 10);
    if (currentStickState != lastStickState[stickindex])
    {
      switch (stickindex) {
        case 0: // UP
          if (currentStickState == 1) {
            Joystick.setYAxis(-1);
          } else {
            Joystick.setYAxis(0);
          }
          break;
        case 1: // RIGHT
          if (currentStickState == 1) {
            Joystick.setXAxis(1);
          } else {
            Joystick.setXAxis(0);
          }
          break;
        case 2: // DOWN
          if (currentStickState == 1) {
            Joystick.setYAxis(1);
          } else {
            Joystick.setYAxis(0);
          }
          break;
        case 3: // LEFT
          if (currentStickState == 1) {
            Joystick.setXAxis(-1);
          } else {
            Joystick.setXAxis(0);
          }
          break;
      }
      lastStickState[stickindex] = currentStickState;
    }
  }

  for (int buttonindex = 0; buttonindex < 5; buttonindex++)
  {
    int currentButtonState = !digitalRead(buttonindex + 3);
    if (currentButtonState != lastButtonState[buttonindex])
    {
      switch (buttonindex) {
        case 0: // FIRE
          Joystick.setButton(0, currentButtonState);
          break;
        case 1: // FIRE 2
          Joystick.setButton(1, currentButtonState);
          break;
        case 2: // FIRE 3
          Joystick.setButton(2, currentButtonState);
          break;
        case 3: // FIRE 4
          Joystick.setButton(3, currentButtonState);
          break;
        case 4: // FIRE 5
          Joystick.setButton(4, currentButtonState);
          break;
      }
      lastButtonState[buttonindex] = currentButtonState;
    }
  }
  delay(10);
}

from arduinojoysticklibrary.

MHeironimus avatar MHeironimus commented on May 21, 2024

@jbenam Glad to hear you found a solution.

from arduinojoysticklibrary.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.