Laggy buttons (help needed)

General GBZ-related chat goes here. Share ideas, tips and tricks, or ask questions that don't fit into the hardware/software help forums.
Post Reply
User avatar
winnetouch
Posts: 158
Joined: Mon Jul 11, 2016 10:56 am
Has thanked: 2 times
Been thanked: 30 times

Laggy buttons (help needed)

Post by winnetouch » Mon May 22, 2017 10:17 pm

Hy. A couple of months ago I built my first GB zero and it worked but it was sloppily built and I gave it away to a friend. I built another one and used the same code for the buttons on my sparcfun pro micro as with the first build. I'm no programmer so the user "sotasystems" helped me with it. The code is meant for the keys from helders button pcb and a battery LED monitor. The problem is that now the buttons are laggy. Very noticabely laggy. I didn't notice that in my first build but I used it mostly to play puzzle games when I had it. Now in supermario world I noticed it.

Can anyone advise me how to optimise the code to make the buttons less laggy? If that's even possible.

Code: Select all

#include <Keyboard.h>
int BatFlag = 0;
int BatLow = 650;
int LED = 670;
unsigned long previousMillis = 0;
unsigned long currentMillis = 0;
const long Interval = 1000;
int keycode_arr[] =
{
  216,  // LEFT    Pin 0
  218,  // RIGHT   Pin 3
  217,  // DOWN    Pin 2
  215,  // UP      Pin 1
  118,  // 'v'     Pin 10
  119,  // 'w'     Pin A0
  120,  // 'x'     Pin 7
  121,  // 'y'     Pin 6
  46,  // .        Pin 4
  32,  // +        Pin 5
  97,  // 'a'      Pin 9
  98,  // 'b'      Pin 8
};

void setup() {
  pinMode(1, INPUT_PULLUP);
  pinMode(0, INPUT_PULLUP);
  pinMode(2, INPUT_PULLUP);
  pinMode(3, INPUT_PULLUP);
  pinMode(4, INPUT_PULLUP);
  pinMode(5, INPUT_PULLUP);
  pinMode(6, INPUT_PULLUP);
  pinMode(7, INPUT_PULLUP);
  pinMode(8, INPUT_PULLUP);
  pinMode(9, INPUT_PULLUP);
  pinMode(10, INPUT_PULLUP);
  pinMode(18, INPUT_PULLUP);
  // pinMode(16, INPUT_PULLUP); (Not used)
  // pinMode(14, INPUT_PULLUP); (Not used)
  pinMode(15, OUTPUT);
  analogReference(INTERNAL);
  pinMode(A1, INPUT);
  Keyboard.begin();
}

void loop() {
  currentMillis = millis();
  if(currentMillis - previousMillis >= Interval) {
  previousMillis = currentMillis;
    if (analogRead(A1) <= BatLow) {
      BatFlag = 1;
    }
    if (analogRead(A1) >= LED) {
      BatFlag = 0;
    }
    if (BatFlag == 1) {
      digitalWrite(15, LOW);
    }
    else {
      digitalWrite(15, HIGH);
    }
  }
  
  if (digitalRead(0) == LOW) {
    Keyboard.press(keycode_arr[0]);
  }
  else {
    Keyboard.release(keycode_arr[0]);
  }
  
  if (digitalRead(3) == LOW) {
    Keyboard.press(keycode_arr[1]);
  }
  else {
    Keyboard.release(keycode_arr[1]);
  }
  
  if (digitalRead(2) == LOW) {
    Keyboard.press(keycode_arr[2]);
  }
  else {
    Keyboard.release(keycode_arr[2]);
  }
  
  if (digitalRead(1) == LOW) {
    Keyboard.press(keycode_arr[3]);
  }
  else {
    Keyboard.release(keycode_arr[3]);
  }
  
  if (digitalRead(10) == LOW) {
    Keyboard.press(keycode_arr[4]);
  }
  else {
    Keyboard.release(keycode_arr[4]);
  }
  
  if (digitalRead(18) == LOW) {
    Keyboard.press(keycode_arr[5]);
  }
  else {
    Keyboard.release(keycode_arr[5]);
  }
  
  if (digitalRead(7) == LOW) {
    Keyboard.press(keycode_arr[6]);
  }
  else {
    Keyboard.release(keycode_arr[6]);
  }
  
  if (digitalRead(6) == LOW) {
    Keyboard.press(keycode_arr[7]);
  }
  else {
    Keyboard.release(keycode_arr[7]);
  }
  
  if (digitalRead(4) == LOW) {
    Keyboard.press(keycode_arr[8]);
  }
  else {
    Keyboard.release(keycode_arr[8]);
  }
  
  if (digitalRead(5) == LOW) {
    Keyboard.press(keycode_arr[9]);
  }
  else {
    Keyboard.release(keycode_arr[9]);
  }
  
  if (digitalRead(9) == LOW) {
    Keyboard.press(keycode_arr[10]);
  }
  else {
    Keyboard.release(keycode_arr[10]);
  }
  
  if (digitalRead(8) == LOW) {
    Keyboard.press(keycode_arr[11]);
  }
  else {
    Keyboard.release(keycode_arr[11]);
  }
   
}

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: Laggy buttons (help needed)

Post by Lphillimore » Tue May 23, 2017 2:26 am

So to confirm, you're using a Leonardo Pro Micro and not a Teensy? I had some lag issues with Sota's code.

See @IAmOrion code here that I've used with a Leonardo and have had no issues with:

http://www.sudomod.com/forum/viewtopic. ... 6690#p6690

User avatar
winnetouch
Posts: 158
Joined: Mon Jul 11, 2016 10:56 am
Has thanked: 2 times
Been thanked: 30 times

Re: Laggy buttons (help needed)

Post by winnetouch » Tue May 23, 2017 2:43 am

Lphillimore wrote:
Tue May 23, 2017 2:26 am
So to confirm, you're using a Leonardo Pro Micro and not a Teensy? I had some lag issues with Sota's code.

See @IAmOrion code here that I've used with a Leonardo and have had no issues with:

http://www.sudomod.com/forum/viewtopic. ... 6690#p6690
Yes. But my code uses certain pins to monitor the battery life, so just copy pasting the code probably won't work. I really have no idea how to program these things. I tried to learn but could never wrap my head around certain things :P.


User avatar
winnetouch
Posts: 158
Joined: Mon Jul 11, 2016 10:56 am
Has thanked: 2 times
Been thanked: 30 times

Re: Laggy buttons (help needed)

Post by winnetouch » Tue May 23, 2017 3:10 am

Lphillimore wrote:
Tue May 23, 2017 2:54 am
Who wrote the code you're using, Sota?
Yup. I considered contacting him directly but I thought I'd rather post it here if anyone else stumbled on to something similar.

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: Laggy buttons (help needed)

Post by Lphillimore » Tue May 23, 2017 3:25 am

Give him a shout, might be easier. Out of interest​ is your Leonardo the 16MHz version?

User avatar
winnetouch
Posts: 158
Joined: Mon Jul 11, 2016 10:56 am
Has thanked: 2 times
Been thanked: 30 times

Re: Laggy buttons (help needed)

Post by winnetouch » Tue May 23, 2017 3:34 am

Yes it is.

User avatar
HoolyHoo
Posts: 766
Joined: Sat Jul 09, 2016 9:34 pm
Has thanked: 206 times
Been thanked: 741 times

Re: Laggy buttons (help needed)

Post by HoolyHoo » Tue May 23, 2017 6:24 am

I looked at the code and it looks fine to me. I tried it on my leonardo pro and tested it on my computer. Key response seems on point. Is it possible your silicone pads are not making good contact with your PCB?

User avatar
winnetouch
Posts: 158
Joined: Mon Jul 11, 2016 10:56 am
Has thanked: 2 times
Been thanked: 30 times

Re: Laggy buttons (help needed)

Post by winnetouch » Tue May 23, 2017 6:44 am

HoolyHoo wrote:
Tue May 23, 2017 6:24 am
I looked at the code and it looks fine to me. I tried it on my leonardo pro and tested it on my computer. Key response seems on point. Is it possible your silicone pads are not making good contact with your PCB?
That could be one issue but I doubt that's the case, I'll check it anyway. Is it possible that it's an issue with the screen? I'm using one of the tested screens so I doubt I'd be the only one having problems if that were the case.

User avatar
HoolyHoo
Posts: 766
Joined: Sat Jul 09, 2016 9:34 pm
Has thanked: 206 times
Been thanked: 741 times

Re: Laggy buttons (help needed)

Post by HoolyHoo » Tue May 23, 2017 6:50 am

Doubt, screen has anything to do with it. Test the pcb pad by bypassing the silicone pad and let me know on that first. You can close the connection directly and check the response we can then go from there. ;)

Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest