Joystick button stuck on after being pressed

I've been working a small modification to a little button panel for video games. It was working fine, and then I decided to add a switch and a couple buttons. Somehow, being the genius I am, I couldn't find the code I had been using to just modify, so I started fresh.
All the buttons before were working with the previous code. With the new code however, when I press a button it sticks on. I've been going over it for a few hours now, and I have a little bit of the code I had been using, and that works fine. Something about this new code, which as far as I can tell, except for different names, is the same code, just won't work. It just keeps being read as on. And I know my code is probably kinda terrible, but it works alright for me. Well... so I thought.

Here's the working code:

#include "Joystick.h"
Joystick_ Joystick;
const int AH = 7; //3
void setup() {
  // put your setup code here, to run once:
  pinMode(AH, INPUT_PULLUP);
    Joystick.begin();
}

int LBAH = 0;

void loop() {
  // put your main code here, to run repeatedly:

      int currentButtonStateAH = !digitalRead(AH); //3
  if (currentButtonStateAH != LBAH)
  {
    Joystick.setButton(2, currentButtonStateAH);
    LBAH = currentButtonStateAH;
  }

}

And here's the non-working code:

#include "Joystick.h"
Joystick_ Joystick;
const int But1 = 7; //3
void setup() {
  // put your setup code here, to run once:
 pinMode(But1, INPUT_PULLUP);
    Joystick.begin();
}

int LBBut1 = 0;

void loop() {
  // put your main code here, to run repeatedly:

       int currentButtonState = !digitalRead(But1); //3
  if (currentButtonState != LBBut1)
  {
    Joystick.setButton(2, But1);
    LBBut1 = currentButtonState;
  }

}
Joystick.setButton(2, But1);

Do you really want to set button 2 to the pin number of But1?

Steve

slipstick:

Joystick.setButton(2, But1);

Do you really want to set button 2 to the pin number of But1?

Steve

Actually, yes. I have 2 switches prior to the buttons. It made even less sense the first time around.

O.k. obviously a different Joystick.h from the one I know. In that one the second parameter of setButton takes a 0 (open) or 1 (pressed).

Steve

If you're using the same joystick library I am, this is the code for setButton.

void Joystick_::setButton(uint8_t button, uint8_t value)
{
 if (value == 0)
 {
 releaseButton(button);
 }
 else
 {
 pressButton(button);
 }
}

You're sending the function But1 as the parameter for value
But1 = 7
So since value is never equal to 0 it continuously presses the button, so slipstick is correct. You don't want to send But1 to setButton.

Also, I would recommend starting to set it up to use one int to control multiple button states. Assuming in the future you add more than one button to the joystick it'll save memory and it's easier to get that setup and working now than trying to rewrite your code in the future.

Here's a quick example, the buttonArray is just because the pin numbers aren't sequential so it makes it easier to index the buttons with for loops.

#define NUM_BUTTONS 14
int lastButtonStates;
const byte buttonArray[NUM_BUTTONS] = {  DUP_PIN,
                                        DDOWN_PIN,
                                        DLEFT_PIN,
                                        DRIGHT_PIN,
                                        A_PIN,
                                        B_PIN,
                                        C_PIN,
                                        X_PIN,
                                        Y_PIN,
                                        Z_PIN,
                                        L_PIN,
                                        R_PIN,
                                        SELECT_PIN,
                                        START_PIN };

void readButtons() {
  for (byte i=0; i < NUM_BUTTONS; i++)
  {
    //If the button is pressed
    //Note: Buttons return LOW when pressed, and HIGH when not pressed)
    if (digitalRead(buttonArray[i]) == LOW)
    {
//      Serial.print(buttonArray[i]);
//      Serial.println(" LOW");
      if (bitRead(lastButtonStates, i) == 0)
      {
        //since the button is now active, and wasn't previously set to pressed, Press the button using the Joystick_ object
        //set the corresponding bit in lastButtonStates to 1 to show that it's been pressed
        //if the button is pressed, and was previously pressed, do nothing
        Saturn_HID.pressButton(i);
        bitSet(lastButtonStates, i);
      }
    }
    else if (digitalRead(buttonArray[i]) == HIGH)
    {
      if (bitRead(lastButtonStates, i) == 1)
      {
        //since the button is now released, but was previously pressed, Release the button using the Joystick_ object
        //set the corresponding bit in the lastButtonStates to 0 to show that it's been released
        //if the button is released, and was previously released, do nothing
        Saturn_HID.releaseButton(i);
        bitClear(lastButtonStates, i);
      }
    }
  }
}

void loop() {
  readButtons();
}