# Number Input, Calculator Style

Hey guys, first time post, frequent researcher for my project.

I've got a 4D Systems uLCD-43PT and an Arduino Due.
I have a custom keypad on it with 3 LED displays on it (0,1,2).

I'm surprised I'm having to ask this but I can't find it anywhere on the internet in the past several hours:

How do I take the numbers being typed in and put them in order in the LED display (just using #0 right now). I can get the numbers from the keys to the display, but how would I make it so that I could type in, say, 1,325?

Do i take each button press, put them into different variables, and then do the math to sort them into the right order? I've seen some calculators on touch screen displays but I never get any code to see how it works.

Thanks for any help guys.

This might help.

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

LiquidCrystal_I2C lcd(0x20,20,4);

long first = 0;
long second = 0;
long total = 0;
double Ftotal = 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] = {
2,3,4,5}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {
11,10,9,8}; //connect to the column pinouts of the keypad

//initialize an instance of class NewKeypad

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

void loop(){
switch(customKey)
{
case '0' ... '9': // This keeps collecting the first value until a operator is pressed "+-*/"
lcd.setCursor(0,0);
first = (first * 10) + (customKey - '0'); // This method is how you combine all the key presses into one value.
// (customKey - '0') -> converts the keypad character to integer
lcd.print(first);
break;

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

case '-':
lcd.setCursor(0,1);
lcd.print("-");
second = SecondNumber();
total = first - second;
lcd.setCursor(0,3);
lcd.print(total);
first = 0, second = 0;
break;

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

case '/':
lcd.setCursor(0,1);
lcd.print("/");
second = SecondNumber();
lcd.setCursor(0,3);
second == 0 ?
lcd.print("Invalid")
:
Ftotal = (float)first / (float)second;
lcd.print(Ftotal);
first = 0, second = 0;
break;

case 'C':
lcd.clear();
break;
}
}

long SecondNumber(){
while( 1 )
{
if(customKey >= '0' && customKey <= '9')
{
second = (second * 10) + (customKey - '0');
lcd.setCursor(0,2);
lcd.print(second);
}

if(customKey == '=') break;  //return second;
}
return second;
}
``````

modified and works beautifully. thanks!

Did you learn from it or just modify it?