# Problem with data input algorithm

Hi, i’m currrently working on a simple project. The idea is inputting 4 numbers one by one from the keypad. When you hit ’ * ', the lcd will be cleared. When ’ # ’ is hit, the lcd will print “the number is” followed by the 4 numbers inputted before. If the number inputted is more than 4 numbers, the lcd will be cleared up as well. Here is the code I build myself

/

#include <Wire.h>
#include <LiquidCrystal_I2C.h>;
const byte ROWS = 4; //four rows
const byte COLS = 4; //four columns
int count, val, c;
int order[4];
//define the cymbols on the buttons of the keypads
char hexaKeys[ROWS][COLS] = {
{'1','2','3','A'},
{'4','5','6','B'},
{'7','8','9','C'},
{'*','0','#','D'}
};
byte rowPins[ROWS] = {13, 12 , 11, 10}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {9, 8, 7, 6}; //connect to the column pinouts of the keypad

//initialize an instance of class NewKeypad

LiquidCrystal_I2C lcd(0x3F, 2, 1, 0, 4, 5, 6 ,7, 3, POSITIVE);

void setup(){
lcd.begin(16,2);
lcd.clear();
}

void loop(){

if (key != NO_KEY){
if (key != '#'){
order[count]=key -'0';
lcd.setCursor(count,0);
lcd.print(order[count]);
count=count+1;
if (count>4){
lcd.clear();
count=0;
}
}

else if(key = '*'){
count=0;
lcd.clear();
lcd.setCursor(0,0);
}

else if(key= '#'){
for(count=0; count<4;){
val=order[count]+10^(3-count)+c;
c=val;
count=count+1;
}
lcd.clear();
lcd.setCursor(0,0);
lcd.print("value is ");
lcd.setCursor(0,1);
lcd.print(val);
count=0;
}

}
}

I got some problem once i uploaded to the arduino:

1. Instead of being cleared, the LCD prints " -6" when ’ * ’ is hit. When i keep hitting the ’ # ', the number printed on lcd is different and i dont have any idea where the number is coming from
2. When ’ # ’ is hit, the lcd turns blank and show nothing.

If I made any program failure, please tell me. Im a newbie, thanks

• is not #. So it never looks at the * option.

Also remember to use == as the comparison operator.

MorganS: * is not #. So it never looks at the * option.

Also remember to use == as the comparison operator.

My mistake, I mean "I keep hitting ' * ' ", not ' I keep hitting ' # '

Why to use clear() in setup if before that nothing was printed?

surepic: Why to use clear() in setup if before that nothing was printed?

I thought it will be better if I used lcd.clear() to make sure everything's blank, But if it's unnecessary, i'd like to remove it from the code

If I may, I suggest using switch/cases for your program. Implement like this:

void loop()
{

if (key != NO_KEY) {

switch(key) {

case '#': {
//handle key '#' here
break;
}

case '*': {
//handle key '*' here
break;
}

default: {
//handle other keys here
break;
}

}

}

}

This way, it is easier to debug.

I have my problems solved. Thanks everyone for helping me. I think i’d like to sahre my codes in case of having a same problem and working with those data input algorithms. Thanks everyone

|| @version 1.0
|| @author Alexander Brevig
|| @contact alexanderbrevig@gmail.com
||
|| @description
|| | Demonstrates changing the keypad size and key values.
|| #
*/
#include <Wire.h>
#include <LiquidCrystal_I2C.h>;
const byte ROWS = 4; //four rows
const byte COLS = 4; //four columns
int count, val, c = 0 ;
int order[4];
//define the cymbols on the buttons of the keypads
char hexaKeys[ROWS][COLS] = {
{'1','2','3','A'},
{'4','5','6','B'},
{'7','8','9','C'},
{'*','0','#','D'}
};
byte rowPins[ROWS] = {13, 12 , 11, 10}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {9, 8, 7, 6}; //connect to the column pinouts of the keypad

//initialize an instance of class NewKeypad

/* =================keyboard========================= */
LiquidCrystal_I2C lcd(0x3F, 2, 1, 0, 4, 5, 6 ,7, 3, POSITIVE);

void setup(){
lcd.begin(16,2);
lcd.clear();
}

void loop(){

if (key != NO_KEY){
switch(key){

case '#':{
if(count!=4){
lcd.print("REQUIRED");
}

lcd.clear();
lcd.setCursor(0,0);
for(int i=0;i<4;){
val = (order[i]*pow(10,3-i))+c;
c=val;
i=i+1;
}
lcd.print ("INPUT : ");
lcd.print(val+1);
delay(2000);
lcd.clear();
lcd.setCursor(0,0);
val = 0;
count = 0;
c = 0;
break;
}

/*===========================================================*/
case '*':{
count=0;
lcd.clear();
lcd.setCursor(0,0);
break;
}
/*===========================================================*/
default: {
order[count]=key -'0';
lcd.setCursor(count,0);
lcd.print(order[count]);
count=count+1;

if (count>4){
lcd.clear();
lcd.setCursor(0,0);
count=0;

}

}

}
}
}