weird behaviour of buttons

Having trouble with your GBZ build? Ask your questions here!
Post Reply
ZEL SK8
Posts: 5
Joined: Sun Jul 30, 2017 11:54 am
Has thanked: 6 times
Been thanked: 1 time

weird behaviour of buttons

Post by ZEL SK8 » Sun Jul 30, 2017 1:51 pm

Hello, I recently completed my first GBZ. And though I'm pretty happy with the outcome, there is a little problem with the buttons.
I used a plain Button PCB with common ground thats wired up to a Teensy++. The buttons worked fine at first but now they often register one button press as multiple button presses. Especially in the retropie menu, when you select emulators and roms, one press to the left or right, often skipps two or more menu selections. The weird part about this is that when I clean the contacts for the buttons on the PCB with alcohol, they work fine again for a small period of time.

I realy hope you guys can help me out with my problem. Much thanks in advance :D

In case it could help, here is the code I uploaded to the teensy. I found this code on this forum:

Code: Select all

/* Modified example code from Teensyduino
   You must select Joystick from the "Tools > USB Type" menu
*/

#include <Bounce.h>

// Create Bounce objects for each button. The Bounce object
// automatically deals with contact chatter or "bounce", and
// it makes detecting changes very simple.
  Bounce GBZ_LEFT = Bounce(0, 10);
  Bounce GBZ_UP = Bounce(1, 10);  // 10 = 10 ms debounce time
  Bounce GBZ_DOWN = Bounce(2, 10);  // which is appropriate for
  Bounce GBZ_RIGHT = Bounce(3, 10);  // most mechanical pushbuttons
  Bounce GBZ_SELECT = Bounce(4, 10);
  Bounce GBZ_START = Bounce(5, 10);
  Bounce GBZ_X = Bounce(6, 10);
  Bounce GBZ_Y = Bounce(7, 10);
  Bounce GBZ_B = Bounce(8, 10);
  Bounce GBZ_A = Bounce(9, 10);
  Bounce GBZ_L = Bounce(10, 10);
  Bounce GBZ_R = Bounce(11, 10);

  const int numButtons = 12;

  uint8_t hatu = 0;
  uint8_t hatr = 0;
  uint8_t hatd = 0;
  uint8_t hatl = 0;

  // ganked this bit from https://gist.github.com/cleure/5820004
  // bitwise operations return 4 possible states of each d-pad axis
  // so that d-pad value can be looked up from 4x4 array
  /*
  
          0
          UP
      315    45
 270 LT   -1   RT 90
      225    135
          DN
          180
  
  */
  int GetHatState() {
    uint8_t x, y;
    const static int16_t dpad_lookup[4][4] = {
      { -1, 270,  90, -1},
      {  0, 315,  45, -1},
      {180, 225, 135, -1},
      { -1,  -1,  -1, -1}
    };
    
    y = hatu | (hatd << 1);
    x = hatl | (hatr << 1);
    
    return dpad_lookup[y][x];
  }

void setup() {
  // Configure the pins for input mode with pullup resistors.
  // The pushbuttons connect from each pin to ground.  When
  // the button is pressed, the pin reads LOW because the button
  // shorts it to ground.  When released, the pin reads HIGH
  // because the pullup resistor connects to +5 volts inside
  // the chip.
  for (int i=0; i<numButtons; i++) {
    pinMode(i, INPUT_PULLUP);
  }
  
  //  Set all axes to defaults
  Joystick.X(512);
  Joystick.Y(512);
  Joystick.Z(512);
  Joystick.Zrotate(512);
  Joystick.sliderLeft(512);
  Joystick.sliderRight(512);
  Joystick.hat(-1);
}

void loop() {
  // Update all the buttons.  There should not be any long
  // delays in loop(), so this runs repetitively at a rate
  // faster than the buttons could be pressed and released.
  GBZ_LEFT.update();   // 0
  GBZ_UP.update();     // 1
  GBZ_DOWN.update();   // 2
  GBZ_RIGHT.update();  // 3
  GBZ_SELECT.update(); // 4
  GBZ_START.update();  // 5
  GBZ_X.update();      // 6
  GBZ_Y.update();      // 7
  GBZ_B.update();      // 8
  GBZ_A.update();      // 9
  GBZ_L.update();      // 10
  GBZ_R.update();      // 11

  // Check each button for "falling" edge.
  // Update the Joystick buttons only upon changes.

  if (GBZ_B.fallingEdge()) {
    Joystick.button(1, 1);
  }
  if (GBZ_A.fallingEdge()) {
    Joystick.button(2, 1);
  }
  if (GBZ_Y.fallingEdge()) {
    Joystick.button(3, 1);
  }
  if (GBZ_X.fallingEdge()) {
    Joystick.button(4, 1);
  }
  if (GBZ_L.fallingEdge()) {
    Joystick.button(5, 1);
  }
  if (GBZ_R.fallingEdge()) {
    Joystick.button(6, 1);
  }
  if (GBZ_SELECT.fallingEdge()) {
    Joystick.button(7, 1);
  }
  if (GBZ_START.fallingEdge()) {
    Joystick.button(8, 1);
  }
  if (GBZ_UP.fallingEdge()) {
    hatu = 1;
    Joystick.hat(GetHatState());
  }
  if (GBZ_RIGHT.fallingEdge()) {
    hatr = 1;
    Joystick.hat(GetHatState());
  }
  if (GBZ_DOWN.fallingEdge()) {
    hatd = 1;
    Joystick.hat(GetHatState());
  }
  if (GBZ_LEFT.fallingEdge()) {
    hatl = 1;
    Joystick.hat(GetHatState());
  }

  // Check each button for "rising" edge
  // Update the Joystick buttons only upon changes.

  if (GBZ_B.risingEdge()) {
    Joystick.button(1, 0);
  }
  if (GBZ_A.risingEdge()) {
    Joystick.button(2, 0);
  }
  if (GBZ_Y.risingEdge()) {
    Joystick.button(3, 0);
  }
  if (GBZ_X.risingEdge()) {
    Joystick.button(4, 0);
  }
  if (GBZ_L.risingEdge()) {
    Joystick.button(5, 0);
  }
  if (GBZ_R.risingEdge()) {
    Joystick.button(6, 0);
  }
  if (GBZ_SELECT.risingEdge()) {
    Joystick.button(7, 0);
  }
  if (GBZ_START.risingEdge()) {
    Joystick.button(8, 0);
  }
  if (GBZ_UP.risingEdge()) {
    hatu = 0;
    Joystick.hat(GetHatState());
  }
  if (GBZ_RIGHT.risingEdge()) {
    hatr = 0;
    Joystick.hat(GetHatState());
  }
  if (GBZ_DOWN.risingEdge()) {
    hatd = 0;
    Joystick.hat(GetHatState());
  }
  if (GBZ_LEFT.risingEdge()) {
    hatl = 0;
    Joystick.hat(GetHatState());
  }
}
I hope I was able to point out my problem since english is not my native language.
20170730_213319.jpg
20170730_213319.jpg (3.82 MiB) Viewed 7285 times
20170730_213305.jpg
20170730_213305.jpg (2.9 MiB) Viewed 7285 times
20170730_213037.jpg
20170730_213037.jpg (3.11 MiB) Viewed 7285 times
20170730_213019.jpg
20170730_213019.jpg (2.6 MiB) Viewed 7285 times

Holliday86
Posts: 34
Joined: Mon May 01, 2017 2:37 am
Has thanked: 8 times
Been thanked: 17 times

Re: weird behaviour of buttons

Post by Holliday86 » Sun Jul 30, 2017 2:28 pm

Looks like this should be in the gbz area of the forum, rather than the minti pi one.

I'm sure one of the mod guys will move it for ya :D

Are all of the buttons and membranes held nice and tightly together with no wiggle room?

User avatar
wermy
Site Admin
Posts: 1346
Joined: Tue May 03, 2016 8:51 pm
Has thanked: 620 times
Been thanked: 1322 times
Contact:

Re: weird behaviour of buttons

Post by wermy » Sun Jul 30, 2017 2:28 pm

Moving this to the GBZ section...

As for your problem, make sure you have a good solid connection between the teensy and your button PCB. Also use some isopropyl alcohol to clean off the traces on your button PCB.
ImageImageImageImage

ZEL SK8
Posts: 5
Joined: Sun Jul 30, 2017 11:54 am
Has thanked: 6 times
Been thanked: 1 time

Re: weird behaviour of buttons

Post by ZEL SK8 » Sun Jul 30, 2017 2:52 pm

soorry, didn't realise I was on the wrong section. Thanks for moving :D

@Holliday86 every button and membrane is sitting perfectly. I checked it multiple times already.

@wermy I checked the connection to the teensy with my computer and it seems to be fine.

could it be some issue with the ground? I got this feeling like the PCB is kind of 'storing' the connection between the ground and the individuel buttons.

User avatar
Lphillimore
Posts: 993
Joined: Sat Jan 07, 2017 7:03 pm
Location: Perth, WA
Has thanked: 796 times
Been thanked: 527 times

Re: weird behaviour of buttons

Post by Lphillimore » Sun Jul 30, 2017 7:28 pm

Your answer is likely in the code you posted. I've had issues here before due to 'bounce' being too sensitive and detecting one press as multiple.

You need to change the bounce value.

See here:

http://www.sudomod.com/forum/viewtopic. ... nce#p31337

I suggest 50 first, retest then 100.

Good luck

User avatar
BadBert
Posts: 377
Joined: Wed Jun 29, 2016 4:14 am
Location: Hoogerheide, Netherlands
Has thanked: 100 times
Been thanked: 61 times
Contact:

Re: weird behaviour of buttons

Post by BadBert » Mon Jul 31, 2017 12:32 am

Like @Lphillimore sayd, up the bounce value!

I had the exact same problem... solder joints we solid.
Upped the bounce value to 80 if i remember correctly, and all was perfectly fine after that :D
My first GBZ build -=HERE=- -> Became a gift to my little brother!
My 2nd GBZ build -=WIP HERE=- -> going with HDMI!

snoek09
Posts: 145
Joined: Sat May 20, 2017 3:06 am
Location: Amsterdam, The Netherlands
Has thanked: 48 times
Been thanked: 43 times

Re: weird behaviour of buttons

Post by snoek09 » Mon Jul 31, 2017 1:00 am

Thanks! This solved my problem where some buttons kept registering multiple key presses. I've set the value to 100.

User avatar
Lphillimore
Posts: 993
Joined: Sat Jan 07, 2017 7:03 pm
Location: Perth, WA
Has thanked: 796 times
Been thanked: 527 times

Re: weird behaviour of buttons

Post by Lphillimore » Mon Jul 31, 2017 1:04 am

snoek09 wrote:
Mon Jul 31, 2017 1:00 am
Thanks! This solved my problem where some buttons kept registering multiple key presses. I've set the value to 100.
Glad to hear it! 8-)

ZEL SK8
Posts: 5
Joined: Sun Jul 30, 2017 11:54 am
Has thanked: 6 times
Been thanked: 1 time

Re: weird behaviour of buttons

Post by ZEL SK8 » Mon Jul 31, 2017 8:08 am

Also just tested with a bounce value of 100. looks like it works now.

I realy didn't think the problem was in the rebounce time. Thanks a lot :D

Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest