Debounce Problem or other issue?

Hi Guys,

I am hoping you can help point me in the right direction. I am new to coding and this has been a steep learning curve. I have build a ‘phone’ that when the correct number is entered it plays a .wav file through the handset. I have eventually got this working (which I am quite proud of!) but I am having the issue that every press of a button is coming up on the serial monitor as 3 presses (so my passcode has to be 111222333444).

I thought this was a debounce issue so used the below code as I thought that would stop it… it hasn’t, it just slows down the speed at which the presses appear on the serial monitor.

case '1': delay(300); break;

If this worked I could add it for every key

Whilst this isnt a major issue, I’d really appreciate it if someone could give me some advice, possibly some example code, or even just being told if it should be possible to fix this issue with no extra hardware (just coding)

Thanks in advance

timdr23

Here is my whole code for reference

#include <ButtonDebounce.h>

//http://www.arduino.cc/playground/uploads/Code/Password.zip
#include <Password.h>
//http://www.arduino.cc/playground/uploads/Code/Keypad.zip
#include <Keypad.h>
#include "SD.h"
#define SD_ChipSelectPin 10
#include "TMRpcm.h"
#include "SPI.h"

TMRpcm tmrpcm;

Password password = Password( "111222333444" );

const byte ROWS = 4; // Four rows
const byte COLS = 3; //  columns
// Define the Keymap
char keys[ROWS][COLS] = {
 {'1','2','3'},
 {'4','5','6'},
 {'7','8','9'},
 {'*','0','#'}
};
// Connect keypad ROW0, ROW1, ROW2 and ROW3 to these Arduino pins.
byte rowPins[ROWS] = { 5, 6, 7, 8 };// Connect keypad COL0, COL1 and COL2 to these Arduino pins.
byte colPins[COLS] = { 2, 3, 4 };


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

#define ledPin 14

void setup(){
 
 digitalWrite(ledPin, LOW);   // sets the LED on
 Serial.begin(9600);
 tmrpcm.speakerPin=9;
if(!SD.begin(SD_ChipSelectPin))
{
  Serial.println("SD fail");
  return;
}
tmrpcm.setVolume(3);
 keypad.addEventListener(keypadEvent); //add an event listener for this keypad

 

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

//take care of some special events
void keypadEvent(KeypadEvent eKey){
if (keypad.getState()==PRESSED)
 Serial.print("Pressed: ");
 Serial.println(eKey);
 switch (eKey){
  case '1': delay(300); break;
   case '9': guessPassword(); break;
   case '0': password.reset(); break;
    default:
          password.append(eKey);
 }
}

void guessPassword(){
    Serial.print("Guessing password... ");
    if (password.evaluate()){
          digitalWrite(ledPin,HIGH);
          Serial.print("correct");
          tmrpcm.setVolume(3);
          tmrpcm.play("test2.wav");
          
    }else{
          digitalWrite(ledPin,LOW);
          Serial.print("incorrect");
    }

No!!! If you think you have a switch problem, use setDebounceTime(). But there may be a different issue.

You have defined a 3x3 key definition with a 4x3 keypad matrix:

const byte ROWS = 4; // Four rows
const byte COLS = 3; //  columns
// Define the Keymap
char keys[ROWS][COLS] = {
 {'1','2','3'},
 {'4','5','6'},
 {'7','8','9'},
 {'*','0','#'}
};

Please explain

Ah, I've been punked. Duplicate thread.

Hi aarg,

I must apologise... I forgot that I had posted on the other forum, I thought I had just thought about it!

Can you please explain what you mean by I've defined a 3x3 matrix. I must admit I got a lot of the keypad code of GitHub but as far as I can see it is for a 3x4 keypad - and all my numbers work as required?

Also should the setDebouncetime bit need to go in setup or loop (where I had put the delay thing) and do I need to put anything in the brackets. If it's not a debounce issue, do you have any recommendations of things to research!

Sorry again!!
Timdr23

My mistake, I misread it. It is correct. Why are you asking me about setDebouncetime() when it is documented in the library? Of course it takes a parameter, how else would it know what value to set the debounce time to?

However, you actually said that the 300ms delay didn't help. I suggest that it is a telling coincidence that you have exactly 3 key responses per press, and you have also 3 columns. Perhaps you have it wired wrong, so that a single key press is actually pulling 3 columns down simultaneously.

Please post a schematic and/or clear photos of your wiring.