LCD keypad shield with arduino uno

hi... ]:slight_smile:

I have a lcd keypad shield http://www.robotshop.com/dfrobot-lcd-keypad-shield-arduino-1.html and Arduino uno. I want lcd keypad shield displays an integer value ranging from 0 to 100. UP and DOWN push button function to add and reduce the value of the integer. below is an example of coding that I do. my problem now is the push button on the lcd keypad shield is not function and lcd displays continuous count from 0-100 and looping back. how do I solve this problem?? I'm new in Arduino. Which reference language should I choose to enable my programming function as i desired?.any help is greatly appreciated :D.

#include <LiquidCrystal.h>
LiquidCrystal lcd(8, 9, 4, 5, 6, 7);

#define RG 0
#define UP 1
#define DW 2
#define LF 3
#define SEL 4

int adc_key_in;
int NUM_KEYS = 5;
int adc_key_val[5] ={30, 150, 360, 535, 760};

void setup ()
{

lcd.begin(16, 2);
lcd.clear();

}

void loop()
{

int value;
for(value = 0 ; value <= 100; value++)

if(adc_key_in == UP)
{
value++;
}

else if(adc_key_in == DW)
{
value=-1;
}

else
{
lcd.setCursor(0,0);
lcd.print("COUNT:");
lcd.setCursor(0,1);
lcd.print(value);
delay(1000);

}
}

int get_key(unsigned int input)
{
int k;

for (k = 0; k < NUM_KEYS; k++)
{
if (input < adc_key_val[k])
{
return k;
}
}

if (k >= NUM_KEYS)
k = -1; // No valid key pressed

return k;
}

You forgot to call get_key():

void loop()
{
    int value;
    adc_key_in = get_key(analogRead(A0));   // Add this line

Even with John's suggestion, your program will not do what you want it to do. What is the business of the "for" loop?

johnwasser:
You forgot to call get_key():

void loop()

{
    int value;
    adc_key_in = get_key(analogRead(A0));  // Add this line

thank you..it helped. but my purpose is to enable push button increase and decrease the integer value. my coding only showing the count. any other suggestion?. sorry im new in this arduino.

liudr:
Even with John's suggestion, your program will not do what you want it to do. What is the business of the "for" loop?

i just want to set the integer value..., i think the control structure "for" might help.. :D. is it wrong??

void loop()
{
    static int value = 0;   // If it isn't 'static' it gets created each time through loop().

    adc_key_in = get_key(analogRead(A0));   // Read the keypad

    if(adc_key_in == UP)                  
    {                                      
     value = constrain(value+1,0,100);
    }
    else if(adc_key_in == DW)            
    {       
     value = constrain(value-1,0,100);
    }
                                
      lcd.setCursor(0,0);                        
      lcd.print("COUNT:"); 
      lcd.setCursor(0,1);
      lcd.print(value); 

      delay(250);   // wait 1/4 second before checking the keypad again
}

thank you so much sir for helping me...these coding helped me a lot.!!! i really appreciate it. :stuck_out_tongue_closed_eyes:

smith88:
i just want to set the integer value..., i think the control structure "for" might help.. :D. is it wrong??

I think not. The for loop, if you have first read what a for loop does before writing with it, only increments your value from 0 to 101 and that is without looking at what your buttons are doing.

You will not likely use a for loop in this project (if it is just about incrementing numbers on LCD). John's code will get you started, not finished. Read about while loop if you want your project finished.

I had the same issue. I couldn't get my program to count. Thanks to everyone on this thread, I was able to get it to work. I noticed though no one uploaded the complete working code. For this to work you need an Arduino UNO and a LCD keypad shield from DFRobot (DFR0009), at least that what I used. You also have to make sure what version of the LCD keypad you have, and uncomment/comment the code accordingly.

//Using LiquidCrystal library
#include <LiquidCrystal.h>
 
/*******************************************************
 
This program will add a value and display it.  To increment
use the UP and DOWN button.

Please make sure what version of the LCD Keypad Shield you are using and uncomment
the correct threshold area.


//George Ferrao
//george.ferrao@scarabware.com

*******************************************************/
 
// select the pins used on the LCD panel
LiquidCrystal lcd(8, 9, 4, 5, 6, 7);
 
// define some values used by the panel and buttons
int lcd_key     = 0;
int adc_key_in  = 0;

#define btnUP     1
#define btnDOWN   2

 
// declair funtion to read the buttons

int read_LCD_buttons()
{
 adc_key_in = analogRead(0);      // read the value from the sensor
 
 // For V1.1 us this threshold
 /*
 if (adc_key_in < 250)  return btnUP; 
 if (adc_key_in < 450)  return btnDOWN; 
*/ 
 
 // For V1.0 comment the other threshold and use the one below:
  
 if (adc_key_in < 195)  return btnUP; 
 if (adc_key_in < 380)  return btnDOWN;
}
 
void setup()
{
 lcd.begin(16, 2); // start the library
}
  
void loop()
{
 
 lcd_key = read_LCD_buttons();
 static int value = 0;
 if(lcd_key == 1)
  {
    value = constrain(value+1,-30,30);  // I only wanted it to count from (-30)-(+30) and back
  }
 else if (lcd_key == 2)
  {
    value = constrain(value-1,-30,30);  // I only wanted it to count from (-30)-(+30_)and back
  }
  
  lcd.setCursor(4,0);
  lcd.print("COUNT: ");
  lcd.setCursor(11,0);
  lcd.print(value); // displays the count
  lcd.print(" ");  //This space is here to clear the double digit area.
  
  delay(150);
}