Pages: [1]   Go Down
Author Topic: Problem with down key on sainsmart 16 x 2 LCD shield  (Read 500 times)
0 Members and 1 Guest are viewing this topic.
wokingham, UK
Offline Offline
Newbie
*
Karma: 0
Posts: 6
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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!

Code:
/*
Menu system example using 16 x 2 lcd shield
 written by Mark Haskins ukmarkthespark@yahoo.co.uk


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.clear();
 lcd.setCursor(2, 0);
 lcd.print("MENU SYSTEM");
 lcd.setCursor(4, 1);
 lcd.print("V_01_01");
 delay(500);
 lcd.clear();
 keypad.setRate(10);
 
 // 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.print(localKey);
       lcd.setCursor(10, 1);
       lcd.print("select");
       delay(1000);
       lcd.clear();
     break;
       
     case 2:
       menu -= 1;
       lcd.setCursor(0, 0);
       lcd.print(localKey);
       //delay(200);
       lcd.clear();
     break;
       
     case 3:
       page += 1;
       lcd.setCursor(0, 0);
       lcd.print(localKey);
       //delay(200);
       lcd.clear();
     break;
       
     case 4:
       page -= 1;
       lcd.setCursor(0, 0);
       lcd.print(localKey);
       //delay(200);
       lcd.clear();
     break;
     
     case 5:
       menu += 1;
       lcd.setCursor(0, 0);
       lcd.print(localKey);
       //delay(200);
       lcd.clear();
     break;
     
   }
//*********************************************************************************

   lcd.setCursor(0, 1);
   lcd.print(page);
   lcd.setCursor(5, 1);
   lcd.print(menu);

 }
Logged

East Anglia (UK)
Offline Offline
Faraday Member
**
Karma: 109
Posts: 4076
May all of your blinks be without delay()
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Code:
    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.
Code:
prevNonWaitKey = localKey;
Logged

Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

wokingham, UK
Offline Offline
Newbie
*
Karma: 0
Posts: 6
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

East Anglia (UK)
Offline Offline
Faraday Member
**
Karma: 109
Posts: 4076
May all of your blinks be without delay()
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

wokingham, UK
Offline Offline
Newbie
*
Karma: 0
Posts: 6
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

Pages: [1]   Go Up
Jump to: