SparkFun Forums 

Where electronics enthusiasts find answers.

For the discussion of Arduino related topics.
By Kulverstukas
#193405
Hello, I am having a really weird issue with ProMicro. The story is that I need to make a custom keypad, each button must do some action by working as a keyboard - it's a GUI control keyboard. It's very simple, just 7 buttons, all laid out on a soldering board and connected properly. No wires are touching, everything is good. The buttons are connected to different pins to keep it simple.

I made a sketch to read the buttons using the internal pull-up resistor with debouncing, and it works, well mostly. The issue rises when I press the middle and up buttons (I call them select and up) (and sometimes it happens with the bottom, or down, button). It appears as if the connections short-circuit and different buttons gets read, however it's not the case. When a select button is pressed, it gets registered as an up button, but the up button registers as normal. Sometimes select/middle button registers as down button...

It might be hard to understand, so here's a lowdown: some buttons get read as different ones, although no connections are touching.

I have made a video to explain it better: https://youtu.be/TbHCbelg3MI

And here's the sketch I'm uploading:
Code: Select all
#include <Keyboard.h>

// define number of pins we'll use
// otherwise it causes funky behaviour
const int numOfPins = 7;
// define pins on ProMicro with
// different delays for each button
int btnPins[numOfPins][2] = {
  {2, 2000}, // exit
  {3, 2000}, // back
  {4, 250},  // right
  {5, 250},  // down
  {6, 2000}, // enter
  {7, 250},  // up
  {8, 250}  // left
};

int val = 0;
long lastDebounceTime = 0;

void setup() {
  for (int i = 0; i < numOfPins; i++) {
    pinMode(btnPins[i][0], INPUT_PULLUP);
  }
}

void loop() { 
  for (int i = 0; i < numOfPins; i++) {
    if ((millis() - lastDebounceTime) > btnPins[i][1]) {
      val = digitalRead(btnPins[i][0]);
      if (val == LOW) {
//        Serial.println(btnPins[i][0]);
        doAction(btnPins[i][0]);
        lastDebounceTime = millis();
      }
    }
  }
}

void doAction(int pin) {
  if (pin == btnPins[0][0]) {
    Serial.println("exit");
//    Keyboard.print("exit ");
  } else if (pin == btnPins[1][0]) {
    Serial.println("back");
//    Keyboard.print("back ");
  } else if (pin == btnPins[2][0]) {
    Serial.println("right");
//    Keyboard.print("right ");
  } else if (pin == btnPins[3][0]) {
    Serial.println("down");
//    Keyboard.print("down ");
  } else if (pin == btnPins[4][0]) {
    Serial.println("enter");
//    Keyboard.print("enter ");
  } else if (pin == btnPins[5][0]) {
    Serial.println("up");
//    Keyboard.print("up ");
  } else if (pin == btnPins[6][0]) {
    Serial.println("left");
//    Keyboard.print("left ");
  }
}
I'm starting to think that I've got a defective unit, haven't tried with different Arduinos though.
User avatar
By DanV
#193413
You're going to need an array of lastDebounceTime[] to match up with your pins.
Currently, you're sharing the single lastDebounceTime among all 7 of your pins.