Joystick library, multiple button presses

Hello everyone. I'm super new at arduino and electronics. This is my first post, but I'm at my wits end and feel like a tool trying to get something so basic to work.

I have a very simple 3 button setup. Eventually I want to build a button box, but I'm just trying to get 3 buttons to work first. I am not using a matrix for simplicity. I've tried lots of code examples of reading when the buttons are pressed and all seem to fare the same. It seems to be recording multiple button presses for some reason. (seen in the serial monitor) Also when looking at the "game controller" in windows, as soon as I push a button all of them light up and stay lit up. Following that when I push a button it will deactivate and then light back up which seems opposite to me. I have got to be doing something very stupid since I can't find anyone else having this issue. Can anyone help point me in the right direction? I've already watched Dan's tutorials on youtube plus others. I uploaded a snapshot of the code and serial monitor. Also a diagram of the wiring. I'm using a Leonardo even though the diagram shows an Uno.

I thank you all in advance for your time and wisdom.

Also, in case it's pertinent. I've also noticed that if I push several buttons in succession, the Leonardo will reboot... or crash. I don't know what it's doing, but I lose connection with it and windows then has to detect it again.

Thanks!

Hello aivenssar

Post your sketch, well formated, with well-tempered comments and in so called code tags "</>" and schematic to see how we can help.

Have a nice day and enjoy coding in C++.

Hi, @aivenssar
Welcome to the forum.

Please look at this link on how to connect and program for a momentary tactile switch;

Thanks.. Tom.. :smiley: :+1: :coffee: :australia:

Thank you Paul & Tom, I've corrected the wiring mistakes which has stabilized it and fixed the "staying pressed" issues, however I'm still noticing that the code seems to be running twice because I'm seeing "Button X was pressed" twice in the serial monitor for each push. I'm not even sure it's a problem, but since my intent is to build a button box, I don't want to record 2 button presses if it was only pressed once. Also, I'm trying to take advantage of the internal pullup resistor, so I rewired as such. Any ideas?

Thank so much!

#include <Joystick.h>

#define joyButton1 9
#define joyButton2 8
#define joyButton3 7 

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

int lastButton1State = 0;
int lastButton2State = 0;
int lastButton3State = 0;

void setup() {
  pinMode(joyButton1, INPUT_PULLUP);
  pinMode(joyButton2, INPUT_PULLUP);
  pinMode(joyButton3, INPUT_PULLUP);
  Joystick.begin();
}

void loop() {
  int currentButton1State = !digitalRead(joyButton1);
  if(currentButton1State != lastButton1State)
  {
    Joystick.setButton(0, currentButton1State);
    lastButton1State = currentButton1State;
    Serial.println("Button 1 was pressed");
  }

  int currentButton2State = !digitalRead(joyButton2);
  if(currentButton2State != lastButton2State)
  {
    Joystick.setButton(1, currentButton2State);
    lastButton2State = currentButton2State;
    Serial.println("Button 2 was pressed");
  }

  int currentButton3State = !digitalRead(joyButton3);
  if(currentButton3State != lastButton3State)
  {
    Joystick.setButton(2, currentButton3State);
    lastButton3State = currentButton3State;
    Serial.println("Button 3 was pressed");
  }

  delay(50);
}

Can you try this code?

#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] = {9, 8, 7};

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");
    }
  }
}

Fernando, that resolved the double press! Thank you, and thank you to everyone who helped me! Great community here!

1 Like

Glad to hear!

You can remove the line below since this variable is not used.

bool lastButtonState[NUM_BUTTONS] = {false};

Thanks to FernandoGarcia who put me on to this topic.

I tried your code and it works perfectly for me. Even letting me change the pins to whatever I wanted to use which was my problem How do use the pins I want

I too am doing a button box, well it's a truck shift knob to use in ETS2 and ATS, but close enough.

So thank you for your code. I did drop the Serial.printIn lines because they weren't needed.

Admittedly I do wonder if it can be cleaned up, but I'm at point now where I'm not sure I care enough.

Now I get the headache of working on the next thing; a truck driver button box with an ignition switch, buttons, switches and rotaries.

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