Pages: [1]   Go Down
Author Topic: keypad debouncing  (Read 1519 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 2
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Im not 100% sure this is a debouncing problem or not.  I have a serial 4 digit 7 segment display I purchased from sparkfun that expects 4 bytes to be sent to it; each byte representing 1 digit.  I currently have a 4x4 keypad hooked up to digital pins 2-9 on a USB Boarduino from adafruit.  The display is hooked up to Tx.  I can use Serial.Print("xxxx") to send for bytes and it properly displays those bytes.  I now want to build up a number so if I where to say want to enter the number 250 it would display xxxx xxx2 xx25 x250 as I type. 

The problem I am having is that for each keypress I am getting two digits.  If I set the setDebounceTime() function it just waits for the debounce time to expire before displaying the second digit. (even if I set it really high it will wait several seconds after the button has been released and then send the second digit).  Because of this I am not sure if its a debounce issue or not but its where I am beginning. 

Code:
#include <Keypad.h>

const byte ROWS = 4; //four rows
const byte COLS = 4; //four columns
char keys[ROWS][COLS] = {
  {'1','2','3','A'},
  {'4','5','6','B'},
  {'7','8','9','C'},
  {'*','0','#','D'}
};
byte rowPins[ROWS] = {2,3,4,5}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {6,7,8,9}; //connect to the column pinouts of the keypad
byte myDisplay[4] = {'x','x','x','x'};

Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );

void setup(){
  Serial.begin(9600);
  keypad.addEventListener(keypadEvent);
  keypad.setDebounceTime(250);
  send_display();
}

void loop(){
    keypad.getKey();
}

void build_display(byte num){
  myDisplay[0] = myDisplay[1];
  myDisplay[1] = myDisplay[2];
  myDisplay[2] = myDisplay[3];
  myDisplay[3] = byte(num);
}

void send_display(){
  Serial.print(myDisplay[0]);
  Serial.print(myDisplay[1]);
  Serial.print(myDisplay[2]);
  Serial.print(myDisplay[3]);
}

void keypadEvent(KeypadEvent ekey) {
  switch (ekey) {
    case '0':
      build_display(byte(0));
      send_display();
      break;
    case '1':
      build_display(byte(1));
      send_display();
      break;
    case '2':
      build_display(byte(2));
      send_display();
      break;
    case '3':
      build_display(byte(3));
      send_display();
      break;
    case '4':
      build_display(byte(4));
      send_display();
      break;
    case '5':
      build_display(byte(5));
      send_display();
      break;
    case '6':
      build_display(byte(6));
      send_display();
      break;
    case '7':
      build_display(byte(7));
      send_display();
      break;
    case '8':
      build_display(byte(8));
      send_display();
      break;
    case '9':
      build_display(byte(9));
      send_display();
      break;
    case 'A':                         // Set Brightness to High
      Serial.print('z');
      Serial.print(byte(0));
      break;
    case 'B':                         // Set Brightness to Medium High
      Serial.print('z');
      Serial.print(byte(40));
      break;
    case 'C':                         // Set Brightness to Medium Low
      Serial.print('z');           
      Serial.print(byte(120));
      break;
    case 'D':                         // Set Brightness to LOW
      Serial.print('z'); 
      Serial.print(byte(254));
      break;
    case KEY_RELEASED:
      break;
    default:
      break;
  }
}
Logged

Norway@Oslo
Offline Offline
Edison Member
*
Karma: 12
Posts: 2033
loveArduino(true);
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

I think the event is fired twice, one for press, one for release.
Try this:
Code:
void keypadEvent(KeypadEvent ekey) {
  if (keypad.getState()==PRESSED) {
    switch (ekey) {
      //..
    }
  }
}
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 2
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

That fixed it.  Thanks! 

I thought that having the empty case for KEY_RELEASED would have prevented that but I guess not.

Code:
case KEY_RELEASED:
break;
Logged

North Wales
Offline Offline
Jr. Member
**
Karma: 1
Posts: 51
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

That fixed it.  Thanks! 

I thought that having the empty case for KEY_RELEASED would have prevented that but I guess not.

Code:
case KEY_RELEASED:
break;

It didn't, because putting the event value in your switch statement means you are looking for the value of KEY_RELEASED in the key's value (ekey), rather than the event type. Hence the call to keypad.getstate(), as shown by AlphaBeta.
Logged

Pages: [1]   Go Up
Jump to: