Keypad Debounce

Hi All,

I'm attempting to implement a debounce algorithm for an old Western Electric Touchtone Phone Keypad. Everything is working properly except the debounce is a bit finicky.

Any thoughts?

// Arduino to keypad row, column
#define ROWA 10
#define ROWB 5
#define ROWC 9
#define ROWD 3

#define COLA 8
#define COLB 11
#define COLC 7

int row = -1;
int col = -1;

long time = 0;
long debounce = 40;

long keyStartTime = 0;
long debounceKeyTime = 200;

char keys[4][3] = {
  {
    '1','2','3'          }
  ,
  {
    '4','5','6'          }
  ,
  {
    '7','8','9'          }
  ,
  {
    '*','0','#'          }
};


void setup() {


  pinMode (11, INPUT); 
  digitalWrite (11, 1);
  pinMode (10, INPUT); 
  digitalWrite (10, 1);
  pinMode (9, INPUT); 
  digitalWrite (9, 1);
  pinMode (8, INPUT); 
  digitalWrite (8, 1);
  pinMode (7, INPUT); 
  digitalWrite (7, 1);
  pinMode (3, INPUT); 
  digitalWrite (3, 1);
  pinMode (5, INPUT); 
  digitalWrite (5, 1);

  // Phone Hook
  pinMode (4, INPUT); 
  digitalWrite (4, 1);

  Serial.begin(9600);

}

int hooksw, last_hooksw;
char last_key = -1;




void loop() {



  // hookswitch debounce
  if (digitalRead (4)) {
    hooksw= 1;
    time= millis() + debounce;
  }
  if (millis() > time) {
    hooksw= 0;
  }



  if (hooksw != last_hooksw) {
    last_hooksw= hooksw;
    Serial.print("hook=");
    Serial.println(hooksw);
  }

  if (hooksw) {    
   if ((keypress() != last_key) && keypress()) {
      // TO DO
      // ADD DEBOUNCE HERE
      if (millis() - keyStartTime > debounceKeyTime) {

        Serial.print("key=");
        Serial.write(keypress());
        Serial.println("");
        keyStartTime = millis();
      }
    }
  }
}


char keypress() {

  int row = -1; 
  int col = -1;

  if (! digitalRead (ROWA)) row = 0;
  if (! digitalRead (ROWB)) row = 1;
  if (! digitalRead (ROWC)) row = 2;
  if (! digitalRead (ROWD)) row = 3;

  if (! digitalRead (COLA)) col = 0;
  if (! digitalRead (COLB)) col = 1;
  if (! digitalRead (COLC)) col = 2;

  if (row == -1 && col == -1) return (0);

  return (keys[row][col]);
}

I'll get readouts like key=9 / key=# / key=9 etc....

Thanks in advance for any input!

  digitalWrite (11, 1);
  pinMode (10, INPUT); 
  digitalWrite (10, 1);
  pinMode (9, INPUT); 
  digitalWrite (9, 1);
  pinMode (8, INPUT); 
  digitalWrite (8, 1);
  pinMode (7, INPUT); 
  digitalWrite (7, 1);
  pinMode (3, INPUT); 
  digitalWrite (3, 1);
  pinMode (5, INPUT); 
  digitalWrite (5, 1);

What's with the numeric values for digitalWrite(), rather than the named HIGH and LOW?

Why are you hard-coding pin numbers? Name them! Oh, wait. You did. So, why are you NOT using the names?

  Serial.begin(9600);

You are aware that the stone age is over, right? Pick up the pace!

   if ((keypress() != last_key) && keypress()) {

Why are you calling keypress() twice?

Hey, for a de-bounce algorithm you could refer to course - YouTube
These are Arduino tutorials form SCIGUY14, in one of the first few tutorials (i guess 2nd or 3rd) he has very well explained an small algorithm which takes the reading twice (ie: again after 5ms).

Hope this Helps :slight_smile:

So it looks like the issue results on key push and key up...this is where things start to get bizarre and random numbers are called. Any thoughts?

They are not random numbers.
they are random disturbances or signal noise which cause debounce, also they can be termed as a mechanical problem, coz the speed at which electrical signals travel is much higher than the speed at which we press a button...(if u understand what i am saying)...
thats y we use a simple debounce algorithm in out coding to solve the issue.

they are random disturbances or signal noise which cause debounce

de signals cause de bounce?

The signals have the effect of causing the switch to appear to bounce. That's why you want to de-bounce the switch.

Sorry, my typing error.
U are correct. :slight_smile: