1 button press triggering two buttons

Hi, I'm very new to Arduino, and I'm trying to program 4 buttons to trigger 4 buttons in the joystick library. I'm not sure if the code is cause of the problem but when I go to the game controller in windows, when I press a button it seem to also trigger the one next to it. I'm not sure why here is the code. Help welcome and thanks,
"

//Arduino Joystick 2.0 Library, by MHeironimus (https://github.com/MHeironimus)

//Initial Definitions and Setup
//Libary Inclusion
#include <Joystick.h>

//Define and Allocate Input Pins to memorable names
//#define joyX A0
//#define joyY A1
//#define joyRZ A3
#define joyThrottle A0
#define joyAccelerator A1
#define joyButton1 7
#define joyButton2 8
#define joyButton3 9
#define joyButton4 10

//Initializing Axis as Integers, at a 0 default value
//int xAxis_ = 0;
//int yAxis_ = 0;
//int rzAxis_ = 0;
int throttle_ = 0;
int accelerator_ = 0;

//Setting up Buttons
//Updating a static variable gives greater stability than reading directly from the digital pin.
//Giving Default Values to the Buttons for later use
  int lastButton1State = 0;
  int lastButton2State = 0;
  int lastButton3State = 0;
  int lastButton4State = 0;

//Defining the Joystick
//The Joystick is defined in the following setup:
//Joystick(Joystick HID ID, Joystick Type, Button Count, Hat Switch Count, Include X, Include Y, Include Z, Include Rx, Include Ry, Include Rz, Include Rudder, Include Throttle, Include Accelerator, Include Brake, Include Steering
//Joystick HID ID: A Hex value identifier for HID Device Recognition (default: 0x03). DO NOT USE 0x01 or 0x02
//Joystick type: Define the type of joystick from the types supported. Types: DEFAULT Joystick (0x04 or JOYSTICK_TYPE_JOYSTICK), Gamepad (0x05 or JOYSTICK_TYPE_GAMEPAD), Multi-Axis Controller (0x08 or JOYSTICK_TYPE_MULTI_AXIS)
//Button Count: Number of Buttons shown to HID system (default: 32)
//Hat Switch Count: Number of Hat Switches, max 2. (default:2)
//Include X Axis: Determines whether the X axis is avalible for used by the HID system, defined as a bool value (default:true)
//Include Y Axis: Determines whether the Y axis is avalible for used by the HID system, defined as a bool value (default:true)
//Include Z Axis: Determines whether the Z axis is avalible for used by the HID system, defined as a bool value (default:true)
//Include Rx Axis: Determines whether the X Rotational axis is avalible for used by the HID system, defined as a bool value (default:true)
//Include Ry Axis: Determines whether the Y Rotational axis is avalible for used by the HID system, defined as a bool value (default:true)
//Include Rz Axis: Determines whether the Z Rotational axis is avalible for used by the HID system, defined as a bool value (default:true)
//Include Rudder: Determines whether a Rudder axis is avalible for used by the HID system, defined as a bool value (default:true)
//Include Throttle: Determines whether a Throttle axis is avalible for used by the HID system, defined as a bool value (default:true)
//Include Accelerator: Determines whether an Accelerator axis is avalible for used by the HID system, defined as a bool value (default:true)
//Include Brake: Determines whether a Brake axis is avalible for used by the HID system, defined as a bool value (default:true)
//Include Steering: Determines whether a Steering axis is avalible for used by the HID system, defined as a bool value (default:true)

Joystick_ Joystick(0x12, JOYSTICK_TYPE_JOYSTICK, 4, 0,false,false,false,false,false,false,false,true,true,false,false);

//Set Auto Send State
//Enables Auto Sending, allowing the controller to send information to the HID system, rather than waiting to be asked.
const bool initAutoSendState = true;

void setup() {
  //Initialize Buttons
  //Buttons set up between Digital Pin and Ground, following pin allocations from earlier on
  pinMode(joyButton1, INPUT_PULLUP);
  pinMode(joyButton2, INPUT_PULLUP);
  pinMode(joyButton3, INPUT_PULLUP);
  pinMode(joyButton4, INPUT_PULLUP);

  //Start Joystick - Needed to start the Joystick function libary
  Joystick.begin();
}

void loop() {
  
  //Axis Reading during Runtime
  //Setting Read functions for each axis and parsing correctly. The X axis will be used as an example for explanation

  //Reading the X Axis analog pin to the xAxis_ variable for processing
  //xAxis_ = analogRead(joyX);
  //Mapping the X Axis data from a 0-1023 to 0-255 range for a smoother action
  //xAxis_ = map(xAxis_,0,1023,0,255);
  //Set the Joystick X Axis value as the new, smoother, value
 // Joystick.setXAxis(xAxis_);

 // yAxis_ = analogRead(joyY);
 // yAxis_ = map(yAxis_,0,1023,0,255);
  //Joystick.setYAxis(yAxis_);

  //rzAxis_ = analogRead(joyRZ);
 // rzAxis_ = map(rzAxis_,0,1023,0,255);
  //Joystick.setRzAxis(rzAxis_);

  throttle_ = analogRead(joyThrottle);
  throttle_ = map(throttle_,0,1023,0,255);
  Joystick.setThrottle(throttle_);

  accelerator_ = analogRead(joyAccelerator);
  accelerator_ = map(accelerator_,0,1023,0,255);
  Joystick.setAccelerator(accelerator_);
  
  
  //Button Reading during Runtime
  //Setting Read functions for each button, using a state value for memory. Button 1 will be used as an example for explanation

  //Reading the current Button digital pin to the Current Button State for processing
  int currentButton1State = !digitalRead(joyButton1);
  //If loop - Check that the button has actually changed.
  if (currentButton1State != lastButton1State){
    //If the button has changed, set the specified HID button to the Current Button State
    Joystick.setButton(0, currentButton1State);
    //Update the Stored Button State
    lastButton1State = currentButton1State;
  }
    int currentButton2State = !digitalRead(joyButton2);
  if (currentButton2State != lastButton2State){
    Joystick.setButton(1, currentButton2State);
    lastButton2State = currentButton2State;
  }
    int currentButton3State = !digitalRead(joyButton3);
  if (currentButton3State != lastButton3State){
    Joystick.setButton(2, currentButton3State);
    lastButton3State = currentButton3State;
  }
    int currentButton4State = !digitalRead(joyButton4);
  if (currentButton4State != lastButton4State){
    Joystick.setButton(3, currentButton4State);
    lastButton4State = currentButton4State;
  }

//Pole Delay/Debounce
//To reduce unessecary processing, the frequency of the reading loop is delayed. The value(in ms) can be changed to match requirement
delay(10);
}
"

At a glance through the tiny window it looks plausible.

You could post a schematic showing how your buttons are wired.

You could… create a small test program that did not involve the joystick but simply echoed what your buttons are doing, to see if your buttons are working the way you think.

You could comment out all but one button's code and see if the other buttons still misbehave. For each button one such experiment.

You could do some more serial printing to see the values of key variables and whether your program is flowing like you think.

You could use serial input of single characters to temporarily take the place of your pushbutton logic to inform the joystick stuff logic.

You could put array variables on your to-learn-about list, four blocks of nearly identical code with variables whose names differ only in an appended digit is a sure sign of that.

And it would not hurt to try bumping up the poor man's debouncing delay to 25 ms.

a7

I'm using a Leonardo.

OK, and at least I can see you are using your buttons in the so-called pulled-down configuration.

For reasons the pulled-up configuration is preferred.

Try wiring it so the switch closes taking the pin to ground, and the input pin is connected to V+ with the resistor.

What value of resistor are you using?

If you use pulled up, you can lose the resistor and get the internal pull-ups to work for you by using pinMode INPUT_PULLUP.

You'd have to use the opposite reading from you digitalRead() statements, but that's no big deal.

Mightn't be your problem. Maybe retiring it will fix something that is wrong in front of you but not in the schematic. It happens. :expressionless:

a7

A Uno's input pullup resistor is between 20 to 50K. What resistor value is the one being used on the breadboard? The input pullup resistor and the resistor on the switch form a V divider which may cause wonky with a digital read.

I'm using 10k resistors.

Am I not using the PULLUP command already?

I'm actually using a Leonardo with 10k resistors

So 10K external and 10K internal. When the button is pressed 5V becomes what at the junction of the 2 10K resistors?

1 Like

As more careful @Idahowalker has observed, you have pull down wiring on you breadboard and pull-up mode selected in your code, not gonna work too well.

Lose the resistors, wire for pulling down the input because they are being pulled up when the switch is open…

Or use the resistors, even with the internal pull-up, but wire them as pull-ups.

1 Like

Thanks I will look into this when I get a chance and post back

should I not use the 10kk resistor in the circuit?

I'm a bit confused between the pullup/pulldown function and the difference the wiring make, could some clarify this please?

Thanks you

When using internal pull-ups, you don't need external components. You wire your button between pin and gnd. A pressed button results in a LOW reading.

Thank you, so will my code still work?

Thanks

Seems there is some fault on the number 2 button causing the issue. I will try to replace the components.

Thanks for all the help

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.