# Using keypad.h to input decimals

I currently am using a method to input numbers like a keyboard, where hitting a key pushes the other keys back, and the clear button clears the last button pushed. How would I modify this code so I can put a decimal in? I’m having a hard time trying to figure out how to put a decimal point in an array, and how to make sure there is only 1 decimal point at a time.

``````float numberEntry(int numbercsr)
{
long entry;
lcd.setCursor(0,2);
lcd.print("NUMBER ENTRY");
lcd.setCursor(0,3);
lcd.print("PRESS # TO CONFIRM");
int counter = {0,0,0,0,0,0,0}; //Array to hold numbers entered
z = 0; //Used to determine how many numbers are currently being used

entryloop:
lcd.setCursor(numbercsr, 1);
lcd.cursor();
key = kp.getKey();

// checks if key is pressed
if(key != NO_KEY){
x = key - '0'; //converts key pressed into an int
if(x >= 0 && x<=9){
lcd.setCursor(numbercsr,1);
switch (z){
case 0:
counter=x;
lcd.print(x);
z++;
break;
case 1:
counter = counter; //Used to push old number back while placing the new number at the end
counter = x;
for(i = 5; i < 7; i++)
{
lcd.print(counter[i]);
}
z++;
break;
case 2:
counter = counter;
counter = counter;
counter = x;
for(i = 4; i<7; i++)
{
lcd.print(counter[i]);
}
z++;
break;
case 3:
counter = counter;
counter = counter;
counter = counter;
counter = x;
for(i = 3; i<7; i++)
{
lcd.print(counter[i]);
}
z++;
break;
case 4:
counter = counter;
counter = counter;
counter = counter;
counter = counter;
counter = x;
for(i = 2; i<7; i++){
lcd.print(counter[i]);
}
z++;
break;
case 5:
counter = counter;
counter = counter;
counter = counter;
counter = counter;
counter = counter;
counter = x;
for(i = 1; i<7; i++)
{
lcd.print(counter[i]);
}
z++;
break;
case 6:
counter = counter;
counter = counter;
counter = counter;
counter = counter;
counter = counter;
counter = counter;
counter = x;
for(i = 0; i<7; i++)
{
lcd.print(counter[i]);
}
z++;
break;
case 7:
counter = counter; //Will push the oldest number off the array to add the newest number at the end
counter = counter;
counter = counter;
counter = counter;
counter = counter;
counter = counter;
counter = x;
for(i = 0; i<7; i++)
{
lcd.print(counter[i]);
}
break;
}
}
}

//Confirm the number
if (key == '#'){
lcd.noCursor();
entry = (counter*1000000) + (counter*100000) + (counter*10000) + (counter*1000) + (counter*100) + (counter*10) + (counter);

lcd.setCursor(0,2);
lcd.print("            "); //Clear the line to overlapping letters

lcd.setCursor(0,2);
lcd.print("NUMBER ACCEPTED");

lcd.setCursor(0,3);
lcd.print(" "); //Clear the line to avoid overlapping letters

lcd.setCursor(0,3);
lcd.print("PRESS S TO CONTINUE");
return entry;
}

if (key == '*'){
lcd.clear();
lcd.print("     Setup Mode");
lcd.setCursor(0,2);
lcd.print("RETURNING ZERO");
lcd.setCursor(0,3);
lcd.print("PRESS B TO RETURN");
return 0;
}

if(key == 'C'){
lcd.print("       ");
lcd.setCursor(numbercsr,1);
switch(z){
case 7:
counter = counter; //Deletes the newest key and pushes everything back to the right, to emulate a keyboard
counter = counter;
counter = counter;
counter = counter;
counter = counter;
counter = counter;
counter = 0;
for(i = 1; i < 7; i++)
{
lcd.print(counter[i]); //Prints remaining keys
}
z--;
break;
case 6:
counter = counter;
counter = counter;
counter = counter;
counter = counter;
counter = counter;
counter = 0;
for(i = 2; i < 7; i++)
{
lcd.print(counter[i]);
}
z--;
break;
case 5:
counter = counter;
counter = counter;
counter = counter;
counter = counter;
counter = 0;
for(i = 3; i < 7; i++)
{
lcd.print(counter[i]);
}
z--;
break;
case 4:
counter = counter;
counter = counter;
counter = counter;
counter = 0;
for(i = 4; i < 7; i++)
{
lcd.print(counter[i]);
}
z--;
break;
case 3:
counter = counter;
counter = counter;
counter = 0;
for(i = 5; i < 7; i++)
{
lcd.print(counter[i]);
}
z--;
break;
case 2:
counter = counter;
counter = 0;
lcd.print(counter);
z--;
break;
case 1:
counter = 0;
z--;
break;
case 0:
counter = 0;
break;
}
}

lcd.noCursor();

goto entryloop;
}
``````

What range of numbers do you want to support and what key do you want to use for a decimal point?

Only a range of 3 numbers, and I want to use 'D' as the decimal point, as my other keys are already being used.

Why not just store the keys(chars) pressed into a char array and use atof()?

I do it by entering the digits directly into a floating point variable as follows

``````void entry()
{
if (DP == 0)
{Entry = Entry * 10;
Entry = Entry + Digit;
//Serial.println(Entry);
}
if (DP > 0)
{
Digit = Digit / pow(10,DP);
Entry = Entry + Digit;
//Serial.println(Entry, DP);
DP = DP + 1;
}
}
``````

Note when the decimal point is pressed DP is set to 1 only if it is 0.
Also DP will contain the number of decimal places +1

HazardsMind:
Why not just store the keys(chars) pressed into a char array and use atof()?

It was the easiest way to check if the key pressed was an actual number instead of writing a bunch of ORs.