Problem with down key on sainsmart 16 x 2 LCD shield

Hi there

I am having problems with de-bounce on the down key only, I’m not sure if its hardware or software but I think it’s software what’s why I’m posting here.

I’m trying to write a menu system and with my approach one of the first jobs is to do simple button counting, adding +1 to a counter every UP button press and +1 to a different counter every Right press, then -1 DOWN and -1 LEFT. all the button de-bounce works fine except DOWN which just counts multiple presses, I think there might be either a problem with

  1. My code
  2. The libary
  3. Something else in the electronics
  4. The button
    in that order!

I have tried to check the connection of the buttons but I could not find any probs, any dust/bit shorting something.
I have tried using single case only and a number of other things, delay(200) made it more usable but the problem persits.
It seemed to have fixed itself once or twice when changing code, making me think it might be hardware then carried on coding only for the problem to come back.
I cannot find a similar problem anyone else has come across.
I fear I have reached the edge of my ability and submit my problem to you the forum in hope!

Menu system example using 16 x 2 lcd shield
 written by Mark Haskins

None   - 0
Select - 1
Left   - 2
Up     - 3
Down   - 4
Right  - 5

// add libary
#include <LiquidCrystal.h> // adds lcd libary
#include <DFR_Key.h> // adds key libary

//LCD and keypad section
LiquidCrystal lcd(8, 9, 4, 5, 6, 7); // setup lcd pins

DFR_Key keypad; // setup keypad

int localKey = 0; // setup key variable
String keyString = ""; // ????

// menu system
boolean select = false;
int menu = 0;
int page = 0;
//long int menuPosition;

 void setup(){
 lcd.begin(16, 2);
 lcd.setCursor(2, 0);
 lcd.print("MENU SYSTEM");
 lcd.setCursor(4, 1);
 // setup menu
 menu = 0;
 page = 0;
 menuPosition = 0;
 void loop(){
   lcd.setCursor(0, 0);
   localKey = keypad.getKey();
//this section counts the button presses of pages and menu's
   switch (localKey){ //layout the button presses
     case 1:
       select = true;
       lcd.setCursor(0, 0);
       lcd.setCursor(10, 1);
     case 2:
       menu -= 1;
       lcd.setCursor(0, 0);
     case 3:
       page += 1;
       lcd.setCursor(0, 0);
     case 4:
       page -= 1;
       lcd.setCursor(0, 0);
     case 5:
       menu += 1;
       lcd.setCursor(0, 0);

   lcd.setCursor(0, 1);
   lcd.setCursor(5, 1);


I am surprised that you don’t have problems with all of the keys repeating because the loop() function runs many thousands of times per second. If you have a look at the Key_Counter example included with the library you will see that a key is only acted upon in loop() if it has changed

    if (localKey != prevNonWaitKey)

thus only catching the first press of a key. prevNonWaitKey is set to the current key later in the code when the keypress has been acted upon.

prevNonWaitKey = localKey;

Hi Bob Thanks for the reply, you know I presumed that de-bounce was included in the key library as the other keys work perfectly!, very strange. I can write the de-bounce as i have done it for another project but honestly the other keys work perfectly even if I hold the key down it does not multi push, I will look at writing the keygrab myself as I don't want to make a special case for one key or add de-bounce to keys that don't want it, weird.

thanks again

This is not so much debounce, more a case of the key value being returned continually when a key is held down, hence the need to the test to see whether it has changed.

Anyone else got a sainsmart LCDkeypad shield that they can try this code on? just for a sanity check on the hardware, to see if this is repeatable.

Thanks Bob I understand what you are saying. I will implement code based on your suggestions