Back Space and Enter operation

I have Mega 2560 interfaced with 20x4 LCD and a 4x4 matrix keypad.

My objective is to store an upper limit value and a lower limit value entered from the keypad and use it for comparison with the value that comes from the serial port.(I have made tx and rx connections to max 3232 and rs232, it works fine)

I have written a piece of code which takes input from the keypad and displays it on LCD.
The inputs from the keypad are stored as characters.

I have to implement Back Space operation so that if someone makes a mistake while using the keypad, using back space key they can correct the value, a key should be assigned for back space.
Similarly an Enter operation also have to be realized.
And also how do i reset the board using software?

How can i implement the back space and enter operation?..Please guide me..

#include <Keypad.h>
#include <LiquidCrystal.h>

const byte ROWS = 4; 
const byte COLS = 4; 
char keys[ROWS][COLS] = {
  {'1','2','3','A'},
  {'4','5','6','B'},
  {'7','8','9','C'},
  {'*','0','#','D'}
};
byte rowPins[ROWS] = {30,31,32,33}; //connect to row pinouts 
byte colPins[COLS] = {34,35,36,37}; //connect to column pinouts

Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );
char UL[6];
char LL[6];
int k = 0;
int l = 0;

// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

void setup(){
  Serial.begin(9600);
// set up the LCD's number of columns and rows: 
  lcd.begin(20,4);
  lcd.cursor();
  // set the cursor to column 0, line 0
  // (note: line 0 is the first row, since counting begins with 0):
  displayMenu();
}

void displayMenu(){
lcd.setCursor(0, 0);
lcd.print("'A'-Enter the Limits");
lcd.setCursor(0, 2);
lcd.print("'B'-Reset");
}
void loop()
{
char key = keypad.getKey();
 
switch(key) {
      
case 'A': 
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Enter Upper Limit");
lcd.setCursor(0, 1);
while (k<5){
     char key = keypad.getKey();
     if (key) {         
     // Print a message to the LCD.  
       lcd.print(key);
       UL[k] = key;
    //Serial.println(key);
   k++;
  }
}
delay(1000);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Enter Lower Limit");
lcd.setCursor(0, 1);
while(l<5) {
     char key = keypad.getKey();   
     if (key != NO_KEY) {         
     // Print a message to the LCD.  
     lcd.print(key);
    LL[l] = key;
// Serial.println(key);
   
   l++;
  }
  }
  delay(1000);
  lcd.clear();
  displayMenu();

break;

case 'B':

break;

case 'C':

break;

case 'D':

break;

//default:
//Serial.println(key);
}

}

Enter:
Just choose a key that you want to use for Enter, and do whatever you want to do with the data you've collected so far.

Backspace:

  k--;
  UL[k] = 0;
// reprint UL to LCD

// or

  k--;
  UL[k] = 0;
// reprint LL to LCD

Reset:
You can't really reset the Arduino from a program without external circuitry. What were you wanting to do that reset seemed like the thing to do?

You need to designate physical keys for the 'backspace' and 'enter' operations and then carry out the corresponding operation when that key is pressed. For example you could implement 'backspace' by undoing the last digit entry and redisplaying the current accumulated value.

After reset it has to start from the display Menu..I will assign a key for reset which is B.. just call display menu function..
I guess it will do..

If i assign the key 'D' for the backspace option..what modifications are required in program so that wenever it detects D it has to do only backspace operation..
RIght now what happens is even D will get stored as a character in UL or LL..

I am a newbie so i don't have that much idea :slight_smile:
Should i use else if inside case statements?

This here is a function I made for my password library that allows the person to clear what they entered, one entry at a time.

The parts that are lined out are not important for you to know what they are at this point.

char * NewPasswordV2::EnterData(char data)
{
if(data > 0 && data != _CLR && data != _ENT) //look at incoming char, see what it is. _CLR = 'C' on my keypad, _ENT = 'E'.
** {**
** nt[cnt] = data; // If incoming char is not 'C' or 'E', store it.**
** cnt++; // increment counter (index) for next char**
** }**
** if(data == _CLR) // if incoming char is 'C'**
** {**
** nt[cnt] = NULL; // clear the entry at the current index**
** cnt > 0? cnt-- : cnt = 0; // if cnt > 0, meaning there still is something in the array, decrement the index for the next new char.**
** }**
if( (_MODE? data == _ENT : cnt ==Lenght_of_Passwords - 1) )
~~ {

~~ checkFlag = true;

~~ cnt = 0;~~
~~ return nt;~~
~~ }~~
}

What you can do is pass in your arrays, UL and LL as pointers or the whole array and basically do what I did here.

vathsa:
If i assign the key 'D' for the backspace option..what modifications are required in program so that wenever it detects D it has to do only backspace operation..
RIght now what happens is even D will get stored as a character in UL or LL..

You need to understand what happens when you enter an input character:

   UL[k] = key;
   k++;

This code appends the new character to a local array which holds the accumulated value.

To delete a character, you just need to decrement k to remove the previous character from the accumulated value and then also remove it from the display. Removing from the display could be done either by moving the cursor left and printing a space and then moving it left again (so the next character overwrites the space) or by refreshing the whole display.

vathsa:
If i assign the key 'D' for the backspace option..what modifications are required in program so that wenever it detects D it has to do only backspace operation..
RIght now what happens is even D will get stored as a character in UL or LL..

It only gets stored as a character because you explicitly store it in UL[k]. So the answer is to jusrt don't store it. Check to see what the character is, and deal with it, either storing it in UL or LL, or doing some operation that performs whatever function you want it to do.

Again... Backspace

  k--;       //decrement to point at the last character entered
  UL[k] = 0;  // make that character 0 (removes the character, terminates the string, and leaves k pointing at the right place for the next character)
// reprint UL to LCD

// or

  k--;
  UL[k] = 0;
// reprint LL to LCD

OP,

I usually delete Personal Messages from members other than those I know for a while. But I made a promise that if someone answers my question on a component, I'll help you out. So thank CrossRoads!

http://forum.arduino.cc/index.php?topic=223156.msg1617901#msg1617901

Here is the code I have in all my projects. I adapted it to use keypad library. I use phi-panels.

void get_text(char in_char[], int max_length) // Multitap is turned off in this version
{
  int i=0; // Input buffer pointer
  char a_key;
  while(1) 
  {
    if ((a_key=keypad.getKey())!=NO_KEY) 
    {
      in_char[i]=a_key; // Read in one character
      lcdPanel.write(in_char[i]); // Echo key press back to the panel.
      if ((in_char[i]=='\b')&&(i>0)) i-=2; // Handles back space.
      if ((in_char[i]=='\n')||(i==max_length-1)) 
      { // The \n represents enter key.
        in_char[i]=0; // Terminate the string with 0.
        break; // This breaks out of the while(1) loop.
      }
      i++;
      if (i<0) i=0;
    }
  }
}

You need to give it the array that will buffer the user's input and its maximal length. It fills it with the user input.

One catch: your display doesn't handle back space like mine does:

http://www.inmojo.com/store/liudr-arduino-and-physics-gadgets/item/serial-lcd-back-pack---phi-panel/

So you will see a strange character when you press your back space. That you need a whole different function to buffer and update display. You are just beginning so don't worry about it now.

I want you to define your keypad this way:

char keys[ROWS][COLS] = {
  {'1','2','3','-'},
  {'4','5','6',' '},
  {'7','8','9','\b'},
  {'\n','0','\e','.'}
};

Of course you can pick where back space \b and enter \n goes on your keypad, but insisting on using \b instead of a letter A or D makes your code that much more readable, same for \n.

Once you get the array, you will need to use sscanf or atoi or atol to convert into integer:

http://www.nongnu.org/avr-libc/user-manual/group__avr__stdlib.html#ga3a1fe00c1327bbabc76688a7a1d73370

I have float input functions too so if you want to try my code, stick to my keypad definitions. You will almost certainly need negative sign, decimal point, and space.

BTW, your code doesn't terminate the UL or LL array so it will never work.

Thank u all for the reply..

@liudr

I have made the changes as u said..I have included \b while defining the keypad characters.
But the cursor doesn't come back to the previous position.
How do i get the cursor back to previous position?

I have realized the backspace function \b ..the only problem was it was displaying junk character..
I used lcd.rightToLeft(); lcd.leftToRight(); functions to move the cursor to previous position..
now the program works as expected.. :slight_smile:

vathsa:
I have realized the backspace function \b ..the only problem was it was displaying junk character..
I used lcd.rightToLeft(); lcd.leftToRight(); functions to move the cursor to previous position..
now the program works as expected.. :slight_smile:

That's cool. Glad you made everything work. You can always buy a phi-panel that understands how to display \b \n though.

@vathsa How is that backspace possible using left to right and right to left functions ?