Move cursor on LCD with keypad

I am currently using a 4x4 keypad in my project to enter the information to display on my LCD 16x2 my keypad and program with:

  • a button that erases all entries on the screen.
  • another that validates the entries.

Now I would like to program another 3 button that will allow us to:

  • Move the cursor to the left.
  • Move the cursor to the right.
  • Delete a character. Note that the user can move between the previously entered characters and delete one before moving the cursor again to the right to continue his text.

Any help will be appreciated!

Cordially...

that is my code:

#include <SPI.h>
#include <LiquidCrystal_I2C.h>
#include <Wire.h> 
#include <Keypad.h>


LiquidCrystal_I2C lcd(0x27,16,2); 

/*Le Clavier*/
const byte numRows= 4;
const byte numCols= 4;

char keymap[numRows][numCols]= { 
  {'1', '2', '3', 'A'},
  {'4', '5', '6', 'S'},
  {'7', '8', '9', 'C'},
  {'<', '0', '>', 'V'}
 };

byte rowPins[numRows] = {5,4,3,2};
byte colPins[numCols] = {9,8,7,6};

//initialiser le clavier
Keypad myKeypad= Keypad(makeKeymap(keymap), rowPins, colPins, numRows, numCols);
 
void setup() {
  //Initialiser l'ecran LCD
  lcd.init();
  lcd.backlight();
  SPI.begin();

  lcd.print("Entrez le");
  lcd.setCursor(0,1);
  lcd.print("montant");
  delay(3000);
  lcd.clear();
  lcd.blink();
}

void loop() {
  char keypressed = myKeypad.getKey();
  
  if (keypressed != NO_KEY){
    if(keypressed == '*'){
    }
    else if(keypressed == 'C'){
      lcd.clear();
    }
    else if(keypressed == 'V'){
      lcd.clear();
      lcd.print("Passer votre");
      lcd.setCursor(0,1);
      lcd.print("carte!");
      lcd.noBlink();
    }
    else{
    lcd.print(keypressed);
    }
  }
  
}

Now I would like to program another 3 button that will allow us to:

Permission granted. Is there a problem?

Permission granted. Is there a problem?

Yes, the problem is I do not know how to do that.

“Any help will be appreciated!”

Posting similar requests in the forums is a good way to make people upset.

This is rude, wastes our time and is not allowed.

Before going any further, read the posting rules for this web site!

Read the entire thread!
https://forum.arduino.cc/index.php/topic,148850.0.html

Previous request.
https://forum.arduino.cc/index.php?topic=617150.msg4182170#msg4182170

larryd:
“Any help will be appreciated!”

Posting similar requests in the forums is a good way to make people upset.

This is rude, wastes our time and is not allowed.

Before going any further, read the posting rules for this web site!

Read the entire thread!
https://forum.arduino.cc/index.php/topic,148850.0.html

Previous request.
program the buttons on a 4x4 keyboard - Project Guidance - Arduino Forum

I told myself that my previous post was pretty vague and as it is impossible for me to delete it I preferred to create another to be clearer because I really need this feature in my project!

So delete it if it annoys you!

Ask the moderator to combine threads.

Let’s assume the LCD is a 2x16 display.
lcd.setCursor(0, 0); // top left
lcd.setCursor(15, 0); // top right
lcd.setCursor(0, 1); // bottom left
lcd.setCursor(15, 1); // bottom right

example:
byte Col = 8;
byteRow = 1;
. . .
lcd.setCursor(Col, Row); This line will move your cursor to column 8, row 1.

Let us assume the cursor is at 8,1
If you have the following lines of code, tell us what happens when the ‘<‘ key is received?

if(keypressed == '<' )
{
Row = Row + 1; // Edit <———<<<< Should be Col = Col - 1
lcd.setCursor(Col, Row);
}

From the above, it should be obvious how to manipulate the cursor position.

“validates the entries”
You need to explain exactly what you have in mind here.
Do you understand that placing characters on the screen is different from actually having a number of specific characters do a function.
example: If you ask a user for a password you have the option of either displaying or not displaying the entry on your LCD display.
If you want the functionality to unlock a door then you must accumulate the entered characters in an array or create a running number.
Let’s say you press 1234567890.
As characters are entered you can convert those characters into digits and then assemble an overall number.
When for example a ‘*’ is received (your ‘validated’ character?), you then compare the converted number to a predefined password number.
If these are the same, you can send a HIGH to an output pin, which in turn, energizes an electrical lock.
e.g. digitalWrite(myLock , HIGH);

if(keypressed == '*' )
{
if(PW == 1234567890)
{
PWokay = true;
digitalWrite(myLock, HIGH);
}
else
{
PWokay = false;
PW = 0;
}
}

You can use code like this to create a password number:

    //assemble the password number
    if (PWokay == false && key >= '0' && key <= '9')
    {
        PW = PW * 10 + (key - '0');
    }

Why do you have ?
#include <SPI.h
#include <Wire.h>

Thank you for your answer

Why do you have ?
#include <SPI.h
#include <Wire.h>

because I use the RFID module in my project as well as a buzzer and an i2c for connected my lcd!

You can use code like this to create a password number:

    //assemble the password number

if (PWokay == false && key >= '0' && key <= '9')
    {
        PW = PW * 10 + (key - '0');
    }

I do not intend to use a password for my current project maois thank you for the code I will use one of these days!

example:
byte Col = 8;
byteRow = 1;
. . .
lcd.setCursor(Col, Row); This line will move your cursor to column 8, row 1.

Let us assume the cursor is at 8,1
If you have the following lines of code, tell us what happens when the ‘<‘ key is received?

if(keypressed == '<' )
{
Row = Row + 1;
lcd.setCursor(Col, Row);
}

when I wait for it, the thruster goes to the position (0, 8) but nothing happens when I press on '<'

My mistake, I should have had Col = Col + 1; ???

Let us assume the cursor is at 8,1
If you have the following lines of code, tell us what happens when the '<' key is received?

else if(keypressed == '>' )
{
Col = Col + 1; // <———<<<< corrected line
lcd.setCursor(Col, Row);
}

else if(keypressed == '<' )
{
Col = Col - 1;
lcd.setCursor(Col, Row);
}

Try to code a few control keys then show us your sketch.

We can add comments and make changes.

You can play with this sample

#include <SPI.h>
#include <LiquidCrystal_I2C.h>
#include <Wire.h>
#include <Keypad.h>

LiquidCrystal_I2C lcd(0x27, 16, 2);

/*Le Clavier*/
const byte numRows = 4;
const byte numCols = 4;

char keymap[numRows][numCols] =
{
  {'1', '2', '3', 'A'},
  {'4', '5', '6', 'S'},
  {'7', '8', '9', 'C'},
  {'<', '0', '>', 'V'}
};

byte rowPins[numRows] = {5, 4, 3, 2};
byte colPins[numCols] = {9, 8, 7, 6};

//initialiser le clavier
Keypad myKeypad = Keypad(makeKeymap(keymap), rowPins, colPins, numRows, numCols);

char keypressed;
char column;  //need a signed type
byte row;

//numbers from 0 to 4,294,967,295 can be displayed
unsigned long PW;

//******************************************************************
void setup()
{
  //Initialiser l'ecran LCD
  lcd.init();
  lcd.backlight();
  SPI.begin();

  lcd.print("Entrez le");
  lcd.setCursor(0, 1);
  lcd.print("montant");
  delay(3000);

  lcd.clear();
  lcd.blink();
  
  lcd.setCursor(8, 1);
  column = 8;
  row = 1;
  
} //END of setup()

//******************************************************************
void loop()
{
  //**************************
  keypressed = myKeypad.getKey();
  if (keypressed != NO_KEY)
  {
    serviceKey();
  }
  //**************************

} //END of loop()

//******************************************************************
void serviceKey()
{
  switch (keypressed)
  {
    case '1':
    case '2':
    case '3':
    case '4':
    case '5':
    case '6':
    case '7':
    case '8':
    case '9':
    case '0':
      {
        PW = PW * 10 + (keypressed - '0');
        lcd.setCursor(0, 0);
        //clear line
        //                   111111
        //         0123456789012345
        lcd.print("                ");
        lcd.setCursor(0, 0);
        lcd.print(PW);

        //put cursor back where it was
        lcd.setCursor(column, row);
      }

      break;

    //**************************
    case '<':
      {
        column--;

        if (column  < 0)
        {
          //don't go negative
          column = 0;
        }

        lcd.setCursor(column, row);
      }

      break;

    //**************************
    case '>':
      {
        column++;
        if (column > 15)
        {
          //don't go past column 15
          column = 15;
        }

        lcd.setCursor(column, row);
      }

      break;

    //**************************
    case '*':
      {

      }

      break;

    //**************************
    case 'C':
      {
        lcd.clear();
      }

      break;

    //**************************
    case 'V':
      {
        lcd.clear();
        lcd.print("Passer votre");
        lcd.setCursor(0, 1);
        lcd.print("carte!");
        lcd.noBlink();
      }

      break;

    //**************************
    case 'A':
      {

      }

      break;

    //**************************
    case 'S':
      {

      }

      break;

  } //END of switch/case

} //END of serviceKey

//******************************************************************

thank you this code foctionne as a charm!