Go Down

Topic: keypad debouncing (Read 1 time) previous topic - next topic

digitaladdictions

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: [Select]

#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;
  }
}


AlphaBeta

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

digitaladdictions

That fixed it.  Thanks! 

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

Code: [Select]

case KEY_RELEASED:
break;

Paul J


That fixed it.  Thanks! 

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

Code: [Select]

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.

Go Up
 


Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

Arduino
via Egeo 16
Torino, 10131
Italy