keypad sticking

hey everyone

so here is my problem using the keypad library i created a custom keypad to use as a game controller using some custom files that i found a year or so ago. the website(blog) that i got these off no longer exists what it does is turns a leonardo based board into a game controller with lots of inputs. ive previously used this to rebuild a shifter and pedals from a g27 and it works perfectly however with the number of buttons i want on this button box i cannot do it the same way hence the custom keypad. the main issue is that the outputs seem to be sticky if you press one button it stays pressed (despite the buttons being momentary) untill you press annother button but then that one sticks ive tried a bunch of different versions of the same code with the same issue. please note im not using the latest version of the arduino software due to compatibility. my code is below any help would be apreciated

cheers

edit: im using an arduino pro micro from sparkfun

/* button box for various games
 */
  JoyState_t joySt;

#include <Keypad.h>
const byte ROWS = 4; // Four rows
const byte COLS = 6; // Three columns
// Define the Keymap
char keys[ROWS][COLS] = {
  {'1','2','3','4','5','6'},
  {'7','8','9','10','a','b'},
  {'c','d','e','f','g','h'},
  {'x','xx','i','j','xxx','xxxx'}// xx buttons not in use
};
// Connect keypad ROW0, ROW1, ROW2 and ROW3 to these Arduino pins.
byte rowPins[ROWS] = { 9, 8, 7, 6 };
// Connect keypad COL0, COL1 and COL2 to these Arduino pins.
byte colPins[COLS] = { 10, 16, 14, 15, 18, 19 }; 

// Create the Keypad
Keypad kpd = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );
int RXLED = 17;
void setup ()
{
  pinMode(RXLED, OUTPUT);
  // below is setup for the joystick axis
  joySt.xAxis = 128;
  joySt.yAxis = 128;
  joySt.zAxis = 0;
  joySt.xRotAxis = 0;
  joySt.yRotAxis = 0;
  joySt.zRotAxis = 0;
  joySt.throttle = 0;
  joySt.rudder = 0;
  joySt.hatSw1 = 0;
  joySt.hatSw2 = 0;
  joySt.buttons = 0;
  
}

void loop()
{
   
      switch (kpd.getKey())
      {
        case '1':
          joySt.buttons = 0x1;//button 1
          digitalWrite(RXLED, LOW);
       break;
        case '2':
          joySt.buttons = 0x2;//button 2
           digitalWrite(RXLED, LOW);
        break;
        case '3':
          joySt.buttons = 0x4;//button 3
           
         break;
        case '4':
          joySt.buttons = 0x8;
           
        break;
        case '5':
          joySt.buttons = 0x10;
           
         break;
        case '6':
          joySt.buttons = 0x20;
           
       break;
        case '7':
          joySt.buttons = 0x40;
           
       break;
        case '8':
          joySt.buttons = 0x80;
           
       break;
        case '9':
          joySt.buttons = 0x100;
           
        break;
        case '10':
          joySt.buttons = 0x200;
           
        break;
        case 'a':
          joySt.buttons = 0x400;
           
        break;
        case 'b':
          joySt.buttons = 0x800;
           
           break;
        case 'c':
          joySt.buttons = 0x1000;
           
          break; 
        case 'd':
          joySt.buttons = 0x2000;
           
          break;
        case 'e':
          joySt.buttons = 0x4000;
           
          break;
        case 'f':
          joySt.buttons = 0x8000;
           
           break;
        case 'g':
          joySt.buttons = 0x10000;
           
          break;
        case 'h':
          joySt.buttons = 0x20000;
           
          break;
        case 'i':
          joySt.buttons = 0x40000;
           
          break;
        case 'j':
          joySt.buttons = 0x80000;
          break;
          
      }
   
  
  
  Joystick.setState(&joySt);
  
}

your problem stems from doing

1/ looking at the key being pressed rather than watching for a state change 2/ you have no code to deal with no button being pressed

your library may have a return based on no key pressed (normally return 0)

if so add a case 0: (some code to say joystick= nothing) then break;

As for state change there are good examples on the net.

so i added a case for no key being pressed and it just prevents all keys from firing i found using the wayback machine a copy of the site i found this on.

http://web.archive.org/web/20140807050127/http://www.imaginaryindustries.com/blog/?p=80

to set the buttons as no buttons you can use

joySt.buttons = 0x0;

so if i go

switch (key)
      {
        case 0:
        joySt.buttons = 0x100000; // set button 21 when no buttons being pressed
        break;

        case '1':
          joySt.buttons = 0x1;//button 1
          digitalWrite(RXLED, LOW);
          break;

        case '2':
          joySt.buttons = 0x2;//button 2
          digitalWrite(RXLED, LOW);
          break;

        case '3':
          joySt.buttons = 0x4;//button 3

it just sticks button 21 on and no other buttons do anything. so using "0" as a case works so far as if there's no buttons pressed but it wont let any other button get pressed I still don't understand why

I'm sure that the fine folks at http://snippets-r-us.com could help you with your snippets.

Here, we are just going to assume that the rest of your code is where the problem is.

the rest of the code is in my first post i only posted the currently relevant piece of code but if you like i will post the entire thing agin

but if you like i will post the entire thing agin

The alternative is that we guess what changes you made. I hate guessing games, so I'm going to guess you did something wrong.

Mrninja: the rest of the code is in my first post i only posted the currently relevant piece of code but if you like i will post the entire thing agin

we don't pay for server space so don't be afraid to repost the code after every change.

well in that case here is the updated code

/* button box for various games
 */
  JoyState_t joySt;

#include <Keypad.h>
const byte ROWS = 4; // Four rows
const byte COLS = 6; // Three columns
// Define the Keymap
char keys[ROWS][COLS] = {
  {'1','2','3','4','5','6'},
  {'7','8','9','10','a','b'},
  {'c','d','e','f','g','h'},
  {'x','xx','i','j','xxx','xxxx'}// xx buttons not in use
};
// Connect keypad ROW0, ROW1, ROW2 and ROW3 to these Arduino pins.
byte rowPins[ROWS] = { 9, 8, 7, 6 };
// Connect keypad COL0, COL1 and COL2 to these Arduino pins.
byte colPins[COLS] = { 10, 16, 14, 15, 18, 19 }; 

// Create the Keypad
Keypad kpd = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );
int RXLED = 17;
void setup ()
{
  pinMode(RXLED, OUTPUT);
  // below is setup for the joystick axis
  joySt.xAxis = 128;
  joySt.yAxis = 128;
  joySt.zAxis = 0;
  joySt.xRotAxis = 0;
  joySt.yRotAxis = 0;
  joySt.zRotAxis = 0;
  joySt.throttle = 0;
  joySt.rudder = 0;
  joySt.hatSw1 = 0;
  joySt.hatSw2 = 0;
  joySt.buttons = 0;
  
}

void loop()
{
    char key = kpd.getKey();
    
 
    
      switch (key)
      {
        case 0:
        joySt.buttons = 0x100000;
        break;
        case '1':
          joySt.buttons = 0x1;//button 1
          digitalWrite(RXLED, LOW);
       break;
        case '2':
          joySt.buttons = 0x2;//button 2
           digitalWrite(RXLED, LOW);
        break;
        case '3':
          joySt.buttons = 0x4;//button 3
           
         break;
        case '4':
          joySt.buttons = 0x8;
           
        break;
        case '5':
          joySt.buttons = 0x10;
           
         break;
        case '6':
          joySt.buttons = 0x20;
           
       break;
        case '7':
          joySt.buttons = 0x40;
           
       break;
        case '8':
          joySt.buttons = 0x80;
           
       break;
        case '9':
          joySt.buttons = 0x100;
           
        break;
        case '10':
          joySt.buttons = 0x200;
           
        break;
        case 'a':
          joySt.buttons = 0x400;
           
        break;
        case 'b':
          joySt.buttons = 0x800;
           
           break;
        case 'c':
          joySt.buttons = 0x1000;
           
          break; 
        case 'd':
          joySt.buttons = 0x2000;
           
          break;
        case 'e':
          joySt.buttons = 0x4000;
           
          break;
        case 'f':
          joySt.buttons = 0x8000;
           
           break;
        case 'g':
          joySt.buttons = 0x10000;
           
          break;
        case 'h':
          joySt.buttons = 0x20000;
           
          break;
        case 'i':
          joySt.buttons = 0x40000;
           
          break;
        case 'j':
          joySt.buttons = 0x80000;
          break;

      }
    
   
  
  
  Joystick.setState(&joySt);
  
}

as i said before using case 0: does result in the selected button (in this case 21) to be activated when no button is pressed however nothing stops this pressing other buttons does nothing. i only used button 21 as my example to see if it was working in reality it should be no buttons. I looked up state change but i didnt quite understand how i can implement that with the keypad library.

as i said before using case 0: does result in the selected button (in this case 21) to be activated when no button is pressed

Please read this statement, and see if it makes any sense to you. When no key on the keypad is pressed, case 0 results in the selected button being activated. Since nothing was selected, what the heck does that mean?

however nothing stops this pressing other buttons does nothing

Some punctuation seems to be in order.

Since the default case happens most of the time, perhaps you are flooding some buffer by continually pretending that button 21 (whatever the hell that means) is pressed.

Okay let me try to explain a little better. In one of my previous posts i posted a link to a page that explains how to turn any leonardo based board into a usb game controller with 32 buttons, 8 axis and 2 hatswitches. When i said that case0: results in the selected button being pressed i meant in the code so for example:

JoySt.buttons =0x8

Means activate button 4 i have case 0: set to button 21 (for testing) i have established that it works as no buttons pushed however, this means it is always active. If i press button 1, button 21 stays on and button 1 remains off. If case 0: is set to no buttons, no buttons work at all. If i remove case 0: from the code the most recently pressed button stays on untill annother is pressed.

You don't seem to understand that the code you have posted executes over and over. Most times, no switch on the keypad is pressed, so the "no key pressed" action happens. Then, you press a switch on the keypad, and that action happens. As soon as you release that key, the "no key pressed" action happens over and over.

The code is doing exactly what it is supposed to do. That it doesn't do what you want seems to stem from the fact that you can't describe exactly what it is you want.

when i press a button nothing happens the “no key pressed action” stays on and nothing clears it. i want it to work the same as the other controller i built does when a button is pressed on the controller its pressed in the software if theres no buttons pressed then no buttons are active this doesnt work using they keypad library. i think i need to just get annother board with more pins so i can do it without the switch. thanks for your help guys

when i press a button nothing happens the "no key pressed action" stays on and nothing clears it.

What do your Serial.print()s tell you is happening?

Im not using serial prints i know what is happening though the case 0: is blocking any other case statement from activating

i know what is happening though the case 0: is blocking any other case statement from activating

Great. Then, fix it.

That really makes no sense. Now are you sure that when no key is pressed the keypad is returning a zero. the keypad code seems to show that its returning a char. if you serial print before the switch what does key show when no key is pressed. What does it show when a key is pressed and held down.

If the state doesn't change then go back to your original code and test that. It wouldn't be the first time a wire has got knocked loose after a code change and all the focus is on the code change rather than the problem

Now are you sure that when no key is pressed the keypad is returning a zero.

There is a constant, NO_KEY, that is what is returned when no key is pressed. That is what OP should really be testing for, but, since the value of NO_KEY is 0, it doesn't really matter.

It IS a good idea to confirm that the returned value is what is expected. That is what I have been trying to get OP to do. To no avail, so far.