ENTERING FLOAT VALUE THROUGH KEYPAD

Hii ALL, I did job of printing the keypressed value on LCD and stores it to a variable..but i want to enter float value through keypad.. can anyone suggest how can do that???

Presumably your existing code handles decimal integers.
Simply extend it to handle decimal fractions

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

LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

int v1 = 0;
int v2 = 0;
int y = 0;
int v3 = 0;
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] = {
0, 1, 6, 7}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {
8, 9, 10,13}; // connect to the column pinouts of the keypad
Keypad kpd = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );

void setup()
{
lcd.begin(16, 2);
lcd.clear();
Serial.begin(9600);
lcd.print(β€œEnter the key…”);
delay(10000);
lcd.clear();
v1 = GetNumber();
}

void loop()
{
v2 = GetNumber();
y=v1+v2;
//Serial.println ();
Serial.print (y);

// v3 = GetNumber();

}

int GetNumber()
{
int num = 0;
char key = kpd.getKey();
while(key != β€˜#’)
{
switch (key)
{
case NO_KEY:
break;

case β€˜0’: case β€˜1’: case β€˜2’: case β€˜3’: case β€˜4’:
case β€˜5’: case β€˜6’: case β€˜7’: case β€˜8’: case β€˜9’:
case β€˜A’: case β€˜B’: case β€˜C’: case β€˜D’:
lcd.print(key);
num = num * 10 + (key - β€˜0’);
break;

case β€˜*’:
num = 0;
lcd.clear();
break;
}

key = kpd.getKey();
}

return num;
}
this is the code…i am new to arduino…and how i put point through keypad…THANK YOU :slight_smile:

Change your "keys" character table to include a decimal point instead of one of the hex digits. Use correction fluid on the keypad itself to change the character there.

And code tags. Yeah, we love code tags.

 Β case 'A': case 'B': case 'C': case 'D': 
 Β  Β  Β  Β  Β  lcd.print(key);
 Β  Β  Β  Β  Β  num = num * 10 + (key - '0');
 Β  Β  Β  Β  Β  break;

This doesn't work for hex digits.

please make corrections in my code..and post it ...i am not getting what u try to say...sorry for idiotism..:(

1 Like
char keys[ROWS][COLS] = Β  Β  Β  Β  Β  Β  Β  Β  Β  Β  
{{'1','2','3','A'},
 {'4','5','6','B'},
 {'7','8','9','C'},
 {'*','0','#','.'}};

I Did corrections As you said sir…it showing decimal point on lcd while pressing β€œD”…but when i saved two float variables like v1 =1.8 and v2=1.8 it is showing 176 on serial monitor…why is it so…the code is as follows:

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

LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

float v1 = 0;
float v2 = 0;
int y = 0;
int v3 = 0;
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’,’#’,’.’}};

byte rowPins[ROWS] = {
0, 1, 6, 7}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {
8, 9, 10,13}; // connect to the column pinouts of the keypad
Keypad kpd = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );

void setup()
{
lcd.begin(16, 2);
lcd.clear();
Serial.begin(9600);
lcd.print(β€œEnter the key…”);
delay(10000);
lcd.clear();
v1 = GetNumber();
}

void loop()
{
v2 = GetNumber();
y=v1+v2;
//Serial.println ();
Serial.print (y);

// v3 = GetNumber();

}

int GetNumber()
{
float num = 0;
char key = kpd.getKey();
while(key != β€˜#’)
{
switch (key)
{
case NO_KEY:
break;

case β€˜0’: case β€˜1’: case β€˜2’: case β€˜3’: case β€˜4’:
case β€˜5’: case β€˜6’: case β€˜7’: case β€˜8’: case β€˜9’:
case β€˜A’: case β€˜B’: case β€˜C’: case β€˜.’:
lcd.print(key);
num = num * 10 + (key - β€˜0’);
break;

case β€˜*’:
num = 0;
lcd.clear();
break;
}

key = kpd.getKey();
}

return num;
}

You still need to change the logic of key entry and conversion to decimal.

Β  Β  num = num * 10 + (key - '0');

Subtracting 48 from the ASCII code for the decimal point character makes no more sense than it does for subtracting 48 from the characters 'A', 'B', 'C' or 'D'. I've already mentioned this.

Get a slip of paper, and work out how you would convert a stream of digits and possibly a decimal point into a number.

Please use code tags when posting code It maybe sufficient to accumulate the value into a "long" variable, noting when the decimal point was detected, then dividing the value of "num" by the appropriate power of ten.

please help me i also phase this problem right now…

this is my code

/*
|| @version 1.0
|| @author Andrew Mascolo
|| @date May 15, 2013
||
|| @description
|| Simple use of keypad and LCD
*/
#include <Keypad.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>

LiquidCrystal_I2C lcd(0x3f,16,2);

long first = 0;
long second = 0;
double total = 0;

char customKey;
const byte ROWS = 4;
const byte COLS = 4;

char keys[ROWS][COLS] = {
{β€˜1’,β€˜2’,β€˜3’,’+’},
{β€˜4’,β€˜5’,β€˜6’,’-’},
{β€˜7’,β€˜8’,β€˜9’,’*’},
{β€˜C’,β€˜0’,’=’,’/’}
};
byte rowPins[ROWS] = {9,8,7,6}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {5,4,3,2}; //connect to the column pinouts of the keypad

//initialize an instance of class NewKeypad
Keypad customKeypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS);

void setup()
{
lcd.begin(); // initialize the lcd
lcd.backlight();
}

void loop()
{
customKey = customKeypad.getKey();
switch(customKey)
{
case β€˜0’ … β€˜9’: // This keeps collecting the first value until a operator is pressed β€œΒ±*/”
lcd.setCursor(0,1);
first = first * 10 + (customKey - β€˜0’);
lcd.print(first);
break;

case β€˜+’:
first = (total != 0 ? total : first);
lcd.setCursor(0,0);
lcd.print("+");
second = SecondNumber(); // get the collected the second number
total = first + second;
lcd.setCursor(0,1);
lcd.print(total);
first = 0, second = 0; // reset values back to zero for next use
break;

case β€˜-’:
first = (total != 0 ? total : first);
lcd.setCursor(0,0);
lcd.print("-");
second = SecondNumber();
total = first - second;
lcd.setCursor(0,1);
lcd.print(total);
first = 0, second = 0;
break;

case '’:
first = (total != 0 ? total : first);
lcd.setCursor(0,0);
lcd.print("
");
second = SecondNumber();
total = first * second;
lcd.setCursor(0,1);
lcd.print(total);
first = 0, second = 0;
break;

case β€˜/’:
first = (total != 0 ? total : first);
lcd.setCursor(0,0);
lcd.print("/");
second = SecondNumber();
lcd.setCursor(0,1);

second == 0 ? lcd.print(β€œInvalid”) : total = (float)first / (float)second;

lcd.print(total);
first = 0, second = 0;
break;

case β€˜C’:
total = 0;
first = 0;
second = 0;
lcd.clear();
loop();
break;
}
}

long SecondNumber()
{
while( 1 )
{
customKey = customKeypad.getKey();
if(customKey >= β€˜0’ && customKey <= β€˜9’)
{
second = second * 10 + (customKey - β€˜0’);
lcd.setCursor(0,1);
lcd.print(second);
return second;
}

if(customKey == β€˜=’) break; //return second;
}
return second;
}

Please edit your post to add code tags, as described in "How to use this forum".

What help have you wanted from me by PM? Please, post here clearly.

:slight_smile: :slight_smile: Thank you everybody to given me your precious time to me. :slight_smile: :slight_smile:
:slight_smile: i got the solution of my problem…
so final code for "entering float(point) value from keypad"is:

#include <Keypad.h>

const byte ROWS = 4; //four rows
const byte COLS = 4; //four columns
char keys[ROWS][COLS] = {
{β€˜1’, β€˜2’, β€˜3’, β€˜A’},
{β€˜4’, β€˜5’, β€˜6’, β€˜B’},
{β€˜7’, β€˜8’, β€˜9’, β€˜C’},
{’*’, β€˜0’, β€˜#’, β€˜D’}
};
byte rowPins[ROWS] = {9, 8, 7, 6}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {5, 4, 3, 2}; //connect to the column pinouts of the keypad

Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );

void setup()
{
pinMode(13, OUTPUT);
digitalWrite(13, LOW);
Serial.begin(9600);

float myValueFromKeypad1 = getKeypadFloat();
float myValueFromKeypad2 = getKeypadFloat();
Serial.println(" ");
Serial.print("I received β€œ);
Serial.print(myValueFromKeypad1, 2);
Serial.print(”, and then I received ");
Serial.println(myValueFromKeypad2, 3);
}

void loop()
{
}//loop

float getKeypadFloat()
{
int debug = 1;
float beforeDecimal = 0; // the number accumulator
float afterDecimal = 0;
byte howManyDecimals = 0;
float finalValue;
int keyval; // the key press
int isNum;
int isStar;
int isHash;

Serial.println(β€œEnter digits, * as decimal point, then decimals, # to end”);
if (debug)Serial.print("So far: ");

// accumulate the part before the decimal
do
{
keyval = keypad.getKey(); // input the key
isNum = (keyval >= β€˜0’ && keyval <= β€˜9’); // is it a digit?
if (isNum)
{
if (debug)Serial.print(keyval - β€˜0’);
beforeDecimal = beforeDecimal * 10 + keyval - β€˜0’; // accumulate the input number
}

isStar = (keyval == β€˜*’);
if (debug)if (isStar) Serial.print(".");

//display but ignore illegal presses
if (debug)
{
if (keyval == β€˜A’ || keyval == β€˜B’ || keyval == β€˜C’ || keyval == β€˜D’ || keyval == β€˜#’)
Serial.print("?");
}

} while (!isStar || !keyval); // until a * or while no key pressed

// accumulate the part after the decimal

do
{
keyval = keypad.getKey(); // input the key
isNum = (keyval >= β€˜0’ && keyval <= β€˜9’); // is it a digit?
if (isNum)
{
if (debug)Serial.print(keyval - β€˜0’);
afterDecimal = afterDecimal * 10 + keyval - β€˜0’; // accumulate the input number
howManyDecimals++; // increment for later use
}

isHash = (keyval == β€˜#’);

//display but ignore illegal presses
if (debug)
{
if (keyval == β€˜A’ || keyval == β€˜B’ || keyval == β€˜C’ || keyval == β€˜D’ || keyval == β€˜*’)
Serial.print("?");
}

} while (!isHash || !keyval); // until # or while no key pressed

finalValue = beforeDecimal + (afterDecimal / pow(10, howManyDecimals));

if (debug) Serial.println(" ");
if (debug)Serial.print("Returning ");
if (debug)Serial.println(finalValue, howManyDecimals);

return finalValue;

}//getKeypadFloat

OK, first things first, you have ignored the previous request.

You need to go and read the forum instructions so that you can go back and modify your original posts (not re-post them) - using the β€œMore β†’ Modify” option below the right hand corner of your post - to mark up your code as such using the β€œ</>” icon in the posting window. Just highlight each section of code (or output if you need to post that) from the IDE and click the icon.

In fact, the IDE has a β€œcopy for forum” link to put these markings on a highlighted block for you so you then just paste it here in a posting window. But even before doing that, don’t forget to use the β€œAuto-Format” (Ctrl-T) option first to make it easy to read. If you do not post it as β€œcode” it can easily be quite garbled and is always more difficult to read.

It is inappropriate to attach it as a β€œ.ino” file unless it is clearly too long to include in the post proper. People can usually see the mistakes directly and do not want to have to actually load it in their own IDE. And that would also assume they are using a PC and have the IDE running on that PC.

Also tidy up your blank space. Do use blank lines, but only between complete functional blocks.