Text input with buttons on an LCD (DFRobot 16x2)

Hello again everybody. I’ve recently made a post about my problem but still couldn’t solve it. Here is a better explanation of it with more detailed codes.

I have a DFRobot LCD Shield (16x2) and I’m trying to input text using the five buttons on the lcd; directional buttons and select button. The up and down buttons will be used to cycle through letters (a-z), and the left and right buttons will be used to control which array to cycle letters.

I have found a code and modified it according to my hardware. However, it is not working at the moment. The problem is once I upload it to my Arduino, all I see on the LCD is the letter ‘a’ on the bottom row and that’s it. The buttons do nothing and I’m not sure what the problem is. The code is below, I would really appreciate it if someone could tell me what’s wrong :slight_smile:

#include <LiquidCrystal.h>
#include <LCDKeypad.h>
LCDKeypad lcd;
int X=0;
int Y=1;
char input[] = "a";
void setup(){
lcd.begin(16,2);
lcd.println("Enter Your Text ");
lcd.setCursor(0,1); 
pinMode(KEYPAD_DOWN, INPUT);
 pinMode(KEYPAD_RIGHT, INPUT);
 pinMode(KEYPAD_LEFT, INPUT);
 pinMode(KEYPAD_UP, INPUT);
 pinMode(KEYPAD_SELECT, INPUT);
//delay(1000);
}
void textinput()
{
 int btn;
 int i;
 int lim=16;
 lcd.setCursor(0,1);
 lcd.print(input);
 lcd.blink();
 for (i; i<lim; ) 
 {
   lcd.setCursor(0,1);
   lcd.print(input);
 }
 for(i=0; i<lim ; )
 {
   lcd.setCursor(0,1);
   lcd.print(input);  
   if(btn == KEYPAD_DOWN)
   {
     if(input[i]>'z')
       input[i]='a';
     input[i]=input[i]+1;
     delay(500);
   }
   if(btn == KEYPAD_UP)
   {
     if(input[i]<'a')
       input[i]='z';
     input[i]=input[i]-1;
     delay(500);      
   }
   if(btn = KEYPAD_RIGHT)
   {
     delay(500);    
     i=i+1;
   }
   if(btn == KEYPAD_LEFT && i>0)
   {
     delay(1000);
     i=i-1;
   }
   if(btn == KEYPAD_SELECT)
   {
     lim=i;
     delay(1000);
   }
 }
}
void loop()
{
 lcd.begin(16,2);
 lcd.setCursor(0,1);
// lcd.print("Type text:");
 textinput();
 lcd.clear();
 lcd.setCursor(0,0);
 lcd.print("Your text is:");
 lcd.setCursor(0,1);
 lcd.print(input);
 delay(3000);
}

The code is below, I would really appreciate it if someone could tell me what's wrong

It would help tremendously if you had used code tags properly so that half of your program does not appear to be in italics.

Sorry about that didn't know how to use it, now it's fixed.

  for (i = 0; i < lim ; )

When is this for loop going to end ?
How long will the program stay in the textinput function ?

Hi, the program should always be in textinput function. Should I be adding something else to the loop? Shouldn't the screen at least be showing "Enter Your Text" on the screen when I run it? Sorry if the questions are silly, I have a limited knowledge.

the program should always be in textinput function.

If so, what is the point of the code in the loop() function after the call of textinput() ?

Yes, it should print "Enter your text ". Have you managed to print text on the LCD with simpler programs ?

bgozneli:
Hi, the program should always be in textinput function. Should I be adding something else to the loop?

For an interactive program that has to react quickly on any human input, you’d better forget completely about the delay() function! The delay() function will block the program execution for some time. Blocking the execution of your program for some time is something you surely not want when you want to enter some text quickly and hassle-free.

Usage of the delay() function in the sketch makes the program clumsy and nearly useless for human interaction.

A couple of days ago I developed a button ‘state change’ detection for the type of analog buttons available with the LCD keypad shield. The function will only return the button code when the state of a button changes from ‘no button pressed’ to ‘button pressed’.

Here is the code:

enum {btnNONE, btnSELECT, btnLEFT, btnRIGHT, btnDOWN, btnUP};


int readButtonInput() // read the button state change 'pressed' from analog interface
{
  static int lastValidKey=btnNONE;
  static boolean waitForNONE;
  int currentKey;
  int adcValue[3];
  for (int i=0;i<3;i++) // read pin three times
  {
    adcValue[i] = analogRead(A0);   // read the ADC value from A0 
  }
  if (abs(adcValue[1]-adcValue[0])>1 || abs(adcValue[2]-adcValue[0])>1) 
  {
    return btnNONE; // return because invalid input (reading is too much different)
  }
  if (adcValue[0] > 1000) currentKey= btnNONE;
  else if (adcValue[0] < 70)   currentKey= btnRIGHT; 
  else if (adcValue[0] < 235)  currentKey= btnUP;
  else if (adcValue[0] < 410)  currentKey= btnDOWN;
  else if (adcValue[0] < 620)  currentKey= btnLEFT;
  else if (adcValue[0] < 880)  currentKey= btnSELECT;  
  else return btnNONE;  // when all others fail, return because invalid input
  if (currentKey==btnNONE) 
  {
    waitForNONE=false;
    lastValidKey=btnNONE;
    return btnNONE;
  }
  else if (waitForNONE==false)
  {
    waitForNONE=true;
    lastValidKey=currentKey;
    return currentKey;
  }
  return btnNONE;
}

You can use that to create menu control or text input without any delay().

Please let me know if you need further assistance.

This one is quite fun to play with LCD: http://www.open-drone.org/catchcoins