Using LCD Shield Kit to input text

Hello everybody, I’m quite new to programming Arduino and I have a question that might be actually be very simple to you friends. Nonetheless, it’s quite difficult to me :slight_smile:

As a part of my project, I need to input a text by using an LCD shield kit which has buttons on it. After some research I’ve learned how to assign functions to buttons.

I’m using a 16x2 LCD screen. What I need to do is to choose a letter by cycling through them with the UP and DOWN buttons, and press the RIGHT button to go to the next slot, and do the same so that I can type a word. Well, I can’t do it at the moment and this is the code I found from the shield’s website. I’m pasting it here as it and afterwards I’ll write what I changed in it to shape it according to my problem.

#include <LiquidCrystal.h>

LiquidCrystal lcd(8, 9, 4, 5, 6, 7); // select the pins used on the LCD panel

// define some values used by the panel and buttons
int lcd_key = 0;
int adc_key_in = 0;

#define btnRIGHT 0
#define btnUP 1
#define btnDOWN 2
#define btnLEFT 3
#define btnSELECT 4
#define btnNONE 5

int read_LCD_buttons(){ // read the buttons
adc_key_in = analogRead(0); // read the value from the sensor

// my buttons when read are centered at these valies: 0, 144, 329, 504, 741
// we add approx 50 to those values and check to see if we are close
// We make this the 1st option for speed reasons since it will be the most likely result

if (adc_key_in > 1000) return btnNONE;

if (adc_key_in < 50) return btnRIGHT;
if (adc_key_in < 195) return btnUP;
if (adc_key_in < 380) return btnDOWN;
if (adc_key_in < 555) return btnLEFT;
if (adc_key_in < 790) return btnSELECT;

return btnNONE; // when all others fail, return this.
}

void setup(){
lcd.begin(16, 2); // start the library
lcd.setCursor(0,0); // set the LCD cursor position
lcd.print(“Push the buttons”); // print a simple message on the LCD
}

void loop(){
lcd.setCursor(9,1); // move cursor to second line “1” and 9 spaces over
lcd.print(millis()/1000); // display seconds elapsed since power-up

lcd.setCursor(0,1); // move to the begining of the second line
lcd_key = read_LCD_buttons(); // read the buttons

switch (lcd_key){ // depending on which button was pushed, we perform an action

case btnRIGHT:{ // push button “RIGHT” and show the word on the screen
lcd.print("RIGHT ");
break;
}
case btnLEFT:{
lcd.print("LEFT "); // push button “LEFT” and show the word on the screen
break;
}
case btnUP:{
lcd.print("UP "); // push button “UP” and show the word on the screen
break;
}
case btnDOWN:{
lcd.print("DOWN "); // push button “DOWN” and show the word on the screen
break;
}
case btnSELECT:{
lcd.print(“SELECT”); // push button “SELECT” and show the word on the screen
break;
}
case btnNONE:{
lcd.print("NONE "); // No action will show “None” on the screen
break;
}
}
}

So, as far as I understood, by changing what is below each button’s “case” statement I can change the function. To make the cursor go right when the RIGHT button is pressed, I tried this.
X=0;
Y=0;
lcd.setCursor(X,Y);

case btnRIGHT:
{
X++
lcd.setCursor(X,Y);
break;
}

This didn’t work.
One more thing I can’t grasp is that the buttons in this code function only when I hold them pressed. When I release them it reverts back.

As for the first part of my problem, the text input by cycling through letters, I have thought of creating a string called “ABCDEFGH…Z” and then selecting through its characters with UP and DOWN keys.

Sorry for the long post, and the stuff I wrote might be completely wrong, if so I apologize because I really don’t have much experience with either coding or Arduino. If someone could show me right track of action I would appreciate it a lot :slight_smile:

So, as far as I understood, by changing what is below each button's "case" statement I can change the function

That's right

This didn't work.

What did it do ?
Please post the complete code of the program you tested

That is actually the complete, I've pasted the original code as a reference to what I changed. I've added the

case btnRIGHT:
{
X++
lcd.setCursor(X,Y);
break;
}

line, and all it did was when I held the RIGHT button down, the cursor alternated between the 1st column of top row and bottom row. When I released the button it just stopped.

I think I've found something to solve my problem. If I want to use buttons without needing to hold them down, should I be using ButtonStates?

bgozneli:
Sorry for the long post, and the stuff I wrote might be completely wrong, if so I apologize because I really don't have much experience with either coding or Arduino. If someone could show me right track of action I would appreciate it a lot :slight_smile:

You not only have to evaluate the current 'state' of a button, but the 'state change'.

So you would have to detect the following state changes:
Change from btnNONE to btnRIGHT ==> Right button was pressed
Change from btnNONE to btnLEFT ==> Left button was pressed
Change from btnNONE to btnUP ==> Up button was pressed
and so on.

If your code would have to do something else besides of entering text it would be useful to know about the project and whether if would be possible to do something like a "switch" which makes the sketch:

  • switch on ==> enter some text
  • switch off ==> do some other things
    or if several things have to be done at the same time:
  • entering text and something else going on at the same time

Hi, thanks for the answer. I'm going to be building a communication system, with one Arduino acting as the input source to a transmitter and another as the receiver. This code I've talked about is a part of the transmitter and supposed to take the text input from the LCD and turn each letter into a 5-bit sequence in the end. However, the only thing it should perform at the moment is to allow the user to enter a text input. I'm trying to make that work first before moving into the next step :slight_smile:

bgozneli:
That is actually the complete, I've pasted the original code as a reference to what I changed. I've added the

case btnRIGHT:
{
X++
lcd.setCursor(X,Y);
break;
}

line, and all it did was when I held the RIGHT button down, the cursor alternated between the 1st column of top row and bottom row. When I released the button it just stopped.

OK, so you increment X and move the cursor but at the start of loop() you do this
lcd.setCursor(0,1);            // move to the begining of the second lineSo you put the cursor back where it was.