Need some help sending commands through Serial without a keyboard

Hi there, I'm trying to make an arduino based calculator that will solve a long division equation for me, and display the answer onto an LCD screen when I press a button on my 4x4 keypad. The code is setup for for a Mega Mini, a 4x4 keypad, and a 16x2 LCD Screen.

The problem is that I can't seem to get the answer to show up on my LCD by using my keypad alone. Only by pressing 'Enter' in the Serial Monitor on my computer keyboard.

Everything other than that works - My equation gives me the answers I want, rounded to the hundredth decimal. Perfect!

I plan on using this calculator in the field, so no (full size) keyboards will be around.

Here's the code (setup to display on both the Serial Monitor and LCD):

#include "Adafruit_LiquidCrystal.h"
#include "Keypad.h"

const byte ROWS = 4; 
const byte COLS = 3; 

const byte ROWS2 = 1; 
const byte COLS2 = 1; 

char hexaKeys[ROWS][COLS] = {
  {'1', '2', '3'},
  {'4', '5', '6'},
  {'7', '8', '9'},
  {'.', '0', ','}
};

char hexaKeys2[ROWS2][COLS2] = {
  {' '}
};


byte rowPins[ROWS] = {17, 19, 21, 23}; 
byte colPins[COLS] = {25, 27, 29}; 

byte rowPins2[ROWS2] = {23}; 
byte colPins2[COLS2] = {31}; 

Keypad customKeypad = Keypad(makeKeymap(hexaKeys), rowPins, colPins, ROWS, COLS); 
Keypad customKeypad2 = Keypad(makeKeymap(hexaKeys2), rowPins2, colPins2, ROWS2, COLS2);


Adafruit_LiquidCrystal lcd (8,9,3,10,11,12,13);

float num1;
float num2;

char calSignal;
float result;
float power = .5;
float r1;
float r2;
float r3;
float r4;
float fc;
float power2 = 6;
float result2 = 10;
#define TWO_PI 6.283185307179586476925286766559

void setup() {
  lcd.begin(16,2);
  Serial.begin(9600);
  Serial.println("- Resonant Peak Calculator -");
  Serial.println(" - - - - - - - - - - - - - -");
  Serial.println();
  Serial.println("Type the Inductance and Capacitance, separated by a \",\" (comma)");
  Serial.println();

  lcd.setCursor(1,0);
  lcd.print(F("Res. Peak Calc."));
  lcd.setCursor(0,1);
  lcd.print(F("Type I C values"));
}

void loop() {
  char customKey = customKeypad.getKey();
  char customKey2 = customKeypad2.getKey();
  
  if (customKey){
    Serial.print(customKey);
  }
  if (customKey2){
    Serial.println(customKey2);
        num1 = Serial.parseFloat();

    calSignal = Serial.read();
    
    num2 = Serial.parseFloat();
    
    resolution();
  }
  while (Serial.available() > 0) {
    num1 = Serial.parseFloat();

    calSignal = Serial.read();
    
    num2 = Serial.parseFloat();
    
    resolution();
  }
}

void resolution() {
 char customKey2 = customKeypad2.getKey();
  switch (calSignal) {
    case '+' :
      result = num1 + num2;
      break;
    case ' ' :
    Serial.println(" ");
      Serial.println("\r\n");
      Serial.println(F("<PWR=1>"));  
      Serial.println(customKey2);
      break;
    case ',' :
      result = num1 * num2;
      Serial.println("Inductance =");
      Serial.println(num1, 4);
      Serial.println("Capacitance =");
      Serial.println(num2, 2);

      lcd.clear();
      lcd.setCursor(0,0);
      lcd.print(F("Ind. ="));
      lcd.setCursor(7,0);
      lcd.print(num1,4);
      lcd.setCursor(0,1);
      lcd.print(F("Cap. ="));
      lcd.setCursor(7,1);
      lcd.print(num2, 2);
      
      break;
    case '/' :
      result = num1 / num2;
      break;
    default :
      r1 = pow(result, power);
      r2 = r1 * TWO_PI;
      r3 = 1 / r2;
      r4 = pow(result2, power2);
      fc = r3 * r4;
      Serial.println();
      Serial.println("Resonant Peak =");
      Serial.println(fc);
      Serial.println();
      Serial.println("- - - - - - - - ");
      Serial.println();

      lcd.clear();
      lcd.setCursor(0,0);
      lcd.print(F("Res. Peak ="));
      lcd.setCursor(0,1);
      lcd.print(fc);
      lcd.setCursor(9,1);
      lcd.print(F("(Hz)"));
  }
}

Thanks for any help/info!

Your code is confusing. You read your keypad but then parse the numbers from the Serial port. If you want your values to come from the keypad, you have to have to read them in and build up your number, char by char.

So I have to parse the numbers from the keypad. I'll look into how to do that.
In the mean time, I added some comments to explain the code, for those who would prefer them.

// Values must be written into the Serial Monitor as follows:  2.2,44
// i.e. The 'Inductance' or 'A' value is equal to 2.2, and the 'Capacitance' or 'B' value is equal to 44


// Equation:
//            1
// _______________________    x  (10 ^6)   =  Answer
// ((2 x pi)x((A x B)^.5))



#include "Adafruit_LiquidCrystal.h"
#include "Keypad.h"

// Setup using a 4x4 keypad that is split into 2 sections (1 to transmit a line of numbers into the Serial Monitor, and one to 'Submit' those numbers, using a new line.

const byte ROWS = 4; // 4 rows of numbers
const byte COLS = 3; // 3 columns of numbers

const byte ROWS2 = 1; // 1 Row/Column that will be the 'Enter' key on a calculator (this will be the 'D' key on a 4x4 keypad)
const byte COLS2 = 1; //

char hexaKeys[ROWS][COLS] = { // Number Pads
  {'1', '2', '3'},
  {'4', '5', '6'},
  {'7', '8', '9'},
  {'.', '0', ','}
};

char hexaKeys2[ROWS2][COLS2] = { // 'D' Pad
  {' '}
};


byte rowPins[ROWS] = {17, 19, 21, 23}; // The pins that the keypad is connected to on my Arduino Mega. These will be the 'Number' keys - Set yours accordingly
byte colPins[COLS] = {25, 27, 29};

byte rowPins2[ROWS2] = {23}; // This will be the pin that is connected to the 'D' or 'Enter' Pin on a 4x4 keypad.
byte colPins2[COLS2] = {31};

Keypad customKeypad = Keypad(makeKeymap(hexaKeys), rowPins, colPins, ROWS, COLS); // Create a keypad for the numbers
Keypad customKeypad2 = Keypad(makeKeymap(hexaKeys2), rowPins2, colPins2, ROWS2, COLS2); // Create a keypad for the 'D' Pad


Adafruit_LiquidCrystal lcd (8, 9, 3, 10, 11, 12, 13); // Setup to my LCD screen and Arduino Mega - Set yours accordingly

float num1; // Create an integer for the first value (the 'A' or 'Inductance' value)
float num2; // Create an integer for the second value (the 'B' or 'Capacitance' value)

char calSignal; // Not entirely sure what this is for - Other than giving the Serial Monitor a notice that info is being sent
float result; // Create an integer for the result of 'A' and 'B' (in this case, it will be the 'Inductance' x 'Capacitance')
float power = .5; // Used in the equation
float r1; // Result 1
float r2; // Result 2
float r3; // Result 3
float r4; // Result 4
float fc; // Result 5
float power2 = 6; // Used in the last part of the equation
float result2 = 10; // Also used in the last part of the equation
#define TWO_PI 6.283185307179586476925286766559 // two Pi (:

void setup() {
  lcd.begin(16, 2); // Setup the LCD
  //lcd.clear(); // Not necessary
  Serial.begin(9600); // Setup the Serial Monitor
  Serial.println("- Resonant Peak Calculator -");
  Serial.println(" - - - - - - - - - - - - - -");
  Serial.println();
  Serial.println("Type the Inductance and Capacitance, separated by a \",\" (comma)");
  Serial.println();

  lcd.setCursor(1, 0);
  lcd.print(F("Res. Peak Calc."));
  lcd.setCursor(0, 1);
  lcd.print(F("Type I,C values"));
}

void loop() {
  char customKey = customKeypad.getKey(); // Create a character for keypad 1
  char customKey2 = customKeypad2.getKey(); // Create a character for keypad 2

  if (customKey) { // If a number key is pressed,
    Serial.print(customKey); // Display that number in the Serial monitor, and display the next number immediately next to the previous one, without creating a new line.
  }
  if (customKey2) { // If the 'D' or 'Enter' key is pressed on the keypad,
    Serial.println(customKey2); // Display a new line on the Serial Monitor.
    num1 = Serial.parseFloat(); // Assign the value from the first number to the value of num1

    calSignal = Serial.read(); // read if a signal is sent

    num2 = Serial.parseFloat(); // Assign the value from the second number to the value of num2

    resolution(); // Perform the equation, as per 'resolution'
  }
  while (Serial.available() > 0) { // If the 'Enter' key is pressed on a keyboard,
    num1 = Serial.parseFloat(); // Assign the value of the first number to the value of num1

    calSignal = Serial.read(); // read if a signal is sent.

    num2 = Serial.parseFloat(); // Assign the value of the second number to the value of num2

    resolution(); // Perform the equation, as per 'resolution;
  }
}

void resolution() {
  char customKey2 = customKeypad2.getKey(); //Setup a character for the 'D' or 'Enter' keypad
  switch (calSignal) { // Read a calculator symbol
    case '+' :
      result = num1 + num2;
      break;
    case '-' :
      result = num1 - num2;
      break;
    case '*' :
      result = num1 * num2;
      break;
    case '/' :
      result = num1 / num2;
      break;
    case ' ' :  // If a 'space' is typed (i.e. if the 'D' pad is pressed)
      Serial.println(" "); // Print a new line in the Serial Monitor
      Serial.println("\r\n"); // This was put here as a suggestion
      Serial.println(F("<PWR=1>"));  // ^^
      Serial.println(customKey2); // ^^ As were these two
      break;
    case ',' :
      result = num1 * num2; // If a 'comma' key is pressed (in this case, the # key on a 4x4 keypad)
      Serial.println("Inductance ="); // Print 'Inductance =' onto the Serial Monitor
      Serial.println(num1, 4); // Print the number value given to num1
      Serial.println("Capacitance ="); // Print 'Capacitance =' onto the Serial Monitor
      Serial.println(num2, 2); // Print the number value give to num2

      lcd.clear(); // Clear the writing from the LCD
      lcd.setCursor(0, 0); // Set the cursor position to the first block on the first line
      lcd.print(F("Ind. =")); // Display 'Ind. =' onto the LCD Screen
      lcd.setCursor(7, 0); // Set the cursor position to display after the previous text
      lcd.print(num1, 4); // Display the number value given to num1
      lcd.setCursor(0, 1); // Set the cursor for the first block on the second line
      lcd.print(F("Cap. ="));// Display 'Cap. =' onto the LCD Screen
      lcd.setCursor(7, 1); // Set the cursor position to display after the previoust text
      lcd.print(num2, 2); // Display the number value given to num2

      break;
    default : // Perform the equation
      r1 = pow(result, power);
      r2 = r1 * TWO_PI;
      r3 = 1 / r2;
      r4 = pow(result2, power2);
      fc = r3 * r4;
      Serial.println();
      Serial.println("Resonant Peak =");
      Serial.println(fc); // Display the answer onto the Serial Monitor
      Serial.println();
      Serial.println("- - - - - - - - ");
      Serial.println();

      lcd.clear();
      lcd.setCursor(0, 0);
      lcd.print(F("Res. Peak =")); // Display the answer onto the LCD Screen
      lcd.setCursor(0, 1);
      lcd.print(fc);
      lcd.setCursor(9, 1);
      lcd.print(F("(Hz)"));
  }
}

I guess I'm not sure what exactly you mean by 'read them in'.

Could you please provide an example?

Thanks much!

  • Zach

I'll try to remove the Serial commands from my code entirely?

Also, rather than use Serial.print(); for the numbers that are more than one digit, am I supposed to print each new number onto a new line, i.e. Serial.println();?

If the latter is then the case, then do I set those numbers to their own (long) integers?

Here's a link to the tutorial that I started out with:

^^ That works well with basic math, (+, -, *, /) but when it comes to long division, I needed to add more to the code.

I got everything to work, but I still need a computer (or a cell phone with OTG) to get it to show an answer - which in reality DOESN't work for my purposes

Is there a piece of code that I'm missing that will send a command via the Serial Monitor? Right now, I'm baffled as to why the original code isn't working, using Serial.println();

Is it NOT sending a new line to the Serial Monitor, like pressing the 'Send' button with a computer mouse does?

If so, how do I correct this?

"

blh64:
you have to have to read them in and build up your number, char by char.

Thanks for your reply. Would you mind steering me in the right direction to find how to do this? I googled these things, and I wasn't able to find anything that helped me with my own code.

"How to read in a number with keypad into an arduino"
"how to parse numbers from a keypad to arduino"
"how to use Keyboard to send command to arduino"
"how to assign character to keypad arduino"

I would post the new code, but I couldn't spot where I went wrong, so I didn't change anything. I wouldn't know what to change

Also, my apologies for all the questions! I'm motivated to get this project done

I've tried using the 'Keyboard' library to print a new line as well, but I received this error:

'Keyboard' not found. Does your sketch include the line '#include <Keyboard.h>'?

My sketch did include that line, in the correct location.

Which processor are you compiling for?

ATmega2560(Mega 2560)

... and there's your answer

Thanks!

So if using a keyboard won't happen, can I do this with an Arduino Mega, or UNO, or Mega Mini, or Nano?

If not, I'm thinking a Leonardo will actually suit this project better for my purposes anyway. I'm going to get one of those and post back on how keyboard.println(); affects the code

Hopefully it works!

Thanks again to everyone who's posted!

I think you are confusing your "keypad" with a "keyboard". You have a keypad attached to your Arduino. That has absolutely nothing to do with the Serial port. The Serial port is for communicating with your attached PC using the Serial Monitor.

I'm confused - are you trying to make your arduino pretend to be a USB keyboard, so that it can type keys on your computer? If so, that's what the keyboard library is for, and what you need a Leo (or other board with native usb like some of the ARM processor based boards).

Reading the input from a matrix keypad connected to the arduino does not require this, and has nothing to do with the serial port at all.

Reading input from a USB keyboard on the Arduino is hardest of all, because it requires the arduino to act as a USB host, which requires external hardware, and is generally a lot harder to do than people expect (we are used to plugging in USB stuff and having it just work, all the work that went into writing the drivers for the computer that know how to talk to the device was done by someone else. That hasn't been done for you when using the arduino as USB host, except for the simplest cases, and is hard)

It's not clear what exactly you're trying to do - and the impression I get is that you aren't really clear on this either, hence why you're having so much trouble. I think writing a description of what you want it to do, where it should take it's input from (if it's a keypad connected to the arduino - what kind?), where it should put it's output, etc would help clarify things - both for yourself and for the people here trying to help you.

Thanks DrAzzy (and others) for the responses!

To clarify,
What I wanted was calculator that will read 2 numbers as typed on a 4x4 matrix keypad, (the two numbers are separated by hitting the '#' key on the keypad - which is going to print a comma onto the LCD screen)). Then, the code will assign each of those values to a 'float' integer. I'll press the 'D' pad to run the two numbers through a trigometric equation - THEN print out the result on an LCD screen.

Example:

2.4002,42.44

(arduino does the calculation, then prints on an LCD:)

Res. Peak =
15769.17

I think I confused you because I started with a code that did all of this on the Serial Monitor, via a computer screen and with a computer keyboard - my hope was to then convert that code to do the same thing with just a keypad and an LCD screen.

I did get this to work really well with a cell phone, ArduinoDroid (a USB host), and an OTG Cable, though I can't stand the touch-screen, personally - I realize that's using the same components (a computer and a keyboard), so I'm thinking if I'm going to do this with an Arduino at all, then a Leonardo or something with similar capabilities will be what I need.

Thanks for the reply

Do I have to parse a number without using Serial?

If so, then is the only alternative going to be Stream.parseFloat();
or Stream.parseInt();

Thanks again for the info/direction, I could really use it!

  • Zach

Dude. seriously. I've told you a couple of time as well as others - forget about Serial and Stream - they have nothing to do with your program since you aren't talking to your PC.

You need something along these lines (untested)

float readNumber() {
  const byte nChars = 15;
  char input[nChars];
  byte idx = 0;
  int key = 0;

  while( key != '#' ) {
    key = getKey();
    switch (key) {
      case '0' ... '9':
      case '.':
        if ( idx < nChars-1 ) {
          input[idx++] = key;
          lcd.print(key);
        }
        break;

      case '#':
        input[idx] = '\0';
        lcd.print( ',' );
    }
  }
  return atof(input);
}

Looking back at your original code, your keypad isn't even defined as having a '#' key so I'm not sure how you are going to accomplish this. You define 2 keybads, one 3x3 and the other 1x1.

Blh64 and DeltaG - Thanks for that! I just saw you posted this code - I'll take a look at it now.

Here's what I came up with after a day or so of research

#include "Adafruit_LiquidCrystal.h"
#include "Keypad.h"

// Setup using a 4x4 keypad that is split into 2 sections (1 to transmit a line of numbers into the Serial Monitor, and one to 'Submit' those numbers, using a new line.
const byte ROWS = 4; // 4 rows of numbers
const byte COLS = 3; // 3 columns of numbers

const byte ROWS2 = 1; // 1 Row/Column that will be the 'Enter' key on a calculator (this will be the 'D' key on a 4x4 keypad)
const byte COLS2 = 1; //

const byte ROWS3 = 1; // this will be the 'C' key on a 4x4 keypad)
const byte COLS3 = 1; //

const byte ROWS4 = 1; // this will be the 'B' key on a 4x4 keypad)
const byte COLS4 = 1; //

char hexaKeys[ROWS][COLS] = { // Number Pads
  {'1', '2', '3'},
  {'4', '5', '6'},
  {'7', '8', '9'},
  {'.', '0', '#'}
};

char hexaKeys2[ROWS2][COLS2] = { // 'D' Pad
  {' '}
};

char hexaKeys3[ROWS3][COLS3] = { // 'C' Pad
  {'C'}
};

char hexaKeys4[ROWS4][COLS4] = { // 'B' Pad
  {','}
};

byte rowPins[ROWS] = {17, 19, 21, 23}; // The pins that the keypad is connected to on my Arduino Mega. These will be the 'Number' keys - Set yours accordingly
byte colPins[COLS] = {25, 27, 29};

byte rowPins2[ROWS2] = {23}; // This will be the pin that is connected to the 'D' or 'Enter' Pin on a 4x4 keypad.
byte colPins2[COLS2] = {31};

byte rowPins3[ROWS3] = {21}; // This will be the pin that is connected to the 'C' Pin on a 4x4 keypad.
byte colPins3[COLS3] = {31};

byte rowPins4[ROWS4] = {19}; // This will be the pin that is connected to the 'B' Pin on a 4x4 keypad.
byte colPins4[COLS4] = {31};

Keypad customKeypad = Keypad(makeKeymap(hexaKeys), rowPins, colPins, ROWS, COLS); // Create a keypad for the numbers
Keypad customKeypad2 = Keypad(makeKeymap(hexaKeys2), rowPins2, colPins2, ROWS2, COLS2); // Create a keypad for the 'D' Pad
Keypad customKeypad3 = Keypad(makeKeymap(hexaKeys3), rowPins3, colPins3, ROWS3, COLS3); // Create a keypad for the 'C' Pad
Keypad customKeypad4 = Keypad(makeKeymap(hexaKeys4), rowPins4, colPins4, ROWS4, COLS4); // Create a keypad for the 'B' Pad

unsigned int deBounce = 100;
unsigned int deBounce2 = 450;

Adafruit_LiquidCrystal lcd (8, 9, 3, 10, 11, 12, 13); // Setup to my LCD screen and Arduino Mega - Set yours accordingly

float num1; // Create an integer for the first value (the 'A' or 'Inductance' value)
float num2; // Create an integer for the second value (the 'B' or 'Capacitance' value)

char calSignal; // Not entirely sure what this is for - Other than giving the Serial Monitor a notice that info is being sent
float result; // Create an integer for the result of 'A' and 'B' (in this case, it will be the 'Inductance' x 'Capacitance')
float power = .5; // Used in the equation
float r1; // Result 1
float r2; // Result 2
float r3; // Result 3
float r4; // Result 4
float fc; // Result 5
float power2 = 6; // Used in the last part of the equation
float result2 = 10; // Also used in the last part of the equation
#define TWO_PI 6.283185307179586476925286766559 // two Pi (:

void setup() {
  customKeypad.setDebounceTime(deBounce);
  customKeypad2.setDebounceTime(deBounce);
  customKeypad3.setDebounceTime(deBounce2);
  customKeypad4.setDebounceTime(deBounce2);

  lcd.begin(16, 2); // Setup the LCD
  lcd.setCursor(0,0);
  lcd.print(F("Version 7.7"));
  delay(5000);
  lcd.clear();
  lcd.setCursor(1, 0);
  lcd.print(F("Res. Peak Calc."));
  lcd.setCursor(0, 1);
  lcd.print(F("Type I/C values"));
  delay(5000);
  lcd.clear();
  
}

void loop() {
  
  char customKey = customKeypad.getKey(); // Create a character for keypad 1
  char customKey2 = customKeypad2.getKey(); // Create a character for keypad 2
  char customKey3 = customKeypad3.getKey(); // Create a character for keypad 3
  char customKey4 = customKeypad4.getKey(); // Create a character for keypad 4


  if (customKey) { // If a number key is pressed,
   num1 = (customKey-48); // For some reason, the numbers started at 48?
   num2 = customKey; // Haven't gotten to figuring this number out yet
   lcd.print(customKey);   
  }
  
  if (customKey2) { // If the 'D' or 'Enter' key is pressed on the keypad,
    calSignal = customKey2;
    values();
  }

  if (customKey4) { // If the 'B' key is pressed on the keypad, print a ',' on the screen
    lcd.print(customKey4);
  }

}

void values() {
  switch (calSignal) { // Read a calculator symbol
      case ' ' : // Perform the equation

result = num1 * num2; // If a 'comma' key is pressed (in this case, the # key on a 4x4 keypad)

      lcd.clear(); // Clear the writing from the LCD
      lcd.setCursor(0, 0); // Set the cursor position to the first block on the first line
      lcd.print(F("Ind. =")); // Display 'Ind. =' onto the LCD Screen
      lcd.setCursor(7, 0); // Set the cursor position to display after the previous text
      lcd.print(num1, 4); // Display the number value given to num1
      lcd.setCursor(0, 1); // Set the cursor for the first block on the second line
      lcd.print(F("Cap. ="));// Display 'Cap. =' onto the LCD Screen
      lcd.setCursor(7, 1); // Set the cursor position to display after the previoust text
      lcd.print(num2, 2); // Display the number value given to num2
      delay(2500);
      
      r1 = pow(result, power);
      r2 = r1 * TWO_PI;
      r3 = 1 / r2;
      r4 = pow(result2, power2);
      fc = r3 * r4;
      lcd.clear();
      lcd.setCursor(0, 0);
      lcd.print(F("Res. Peak =")); // Display the answer onto the LCD Screen
      lcd.setCursor(0, 1);
      lcd.print(fc);
      lcd.setCursor(9, 1);
      lcd.print(F("(Hz)"));
      break;
  }
}

This was the closest I could get to my original goal.

I'm still not sure how to store whatever value I type into my keypad as a float number ('num1' and 'num2') - Once I get this figured out then I think I'll be good to go!

Thanks again for the tips on digging deeper into the library! That helped a lot

  • Zach

Nope, you define one keypad, not 3. Then when you read the key, you figure out which one. If it is more of the typical type, you define

// Setup using a 4x4 keypad that is split into 2 sections (1 to transmit a line of numbers into the Serial Monitor, and one to 'Submit' those numbers, using a new line.
const byte ROWS = 4; // 4 rows of numbers
const byte COLS = 4; // 3 columns of numbers

char Keys[ROWS][COLS] = { // Number Pads
  {'1', '2', '3', 'A'},
  {'4', '5', '6', 'B'},
  {'7', '8', '9', 'C'},
  {'*', '0', '#'. 'D'}
}

And then when you read a key, you check to make sure it is 0..9 or '#' and ignore 'A'..'D' and '*'

Ok - I'll try it out. After working with this, I'll actually need the 'D' pad to be the pad that runs the equation, instead of the number key - I added that in there (hopefully correctly)
]
Typically, these are the numbers I might enter for the num1 and num2 values

num1=2.4536
num2=212.86

The numbers will be over the place, but the decimal places will remain the same (4 on the first number, 2 on the second)

Right now I took out all of the extra keypads.
Here's that version:

#include "Adafruit_LiquidCrystal.h"
#include "Keypad.h"

const byte ROWS = 4;
const byte COLS = 4;

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

byte rowPins[ROWS] = {17, 19, 21, 23};
byte colPins[COLS] = {25, 27, 29, 31};

Keypad customKeypad = Keypad(makeKeymap(hexaKeys), rowPins, colPins, ROWS, COLS);

unsigned int deBounce = 100;

Adafruit_LiquidCrystal lcd (8, 9, 3, 10, 11, 12, 13);

float num1; // Create an integer for the first value (the 'A' or 'Inductance' value)
float num2; // Create an integer for the second value (the 'B' or 'Capacitance' value)

float result; // Create an integer for the result of 'A' and 'B' (in this case, it will be the 'Inductance' x 'Capacitance')
float power = .5; // Used in the equation
float r1; // Result 1
float r2; // Result 2
float r3; // Result 3
float r4; // Result 4
float fc; // Result 5
float power2 = 6; // Used in the last part of the equation
float result2 = 10; // Also used in the last part of the equation
#define TWO_PI 6.283185307179586476925286766559 // two Pi (:

void setup() {
  customKeypad.setDebounceTime(deBounce);

  lcd.begin(16, 2);
  lcd.setCursor(1, 0);
  lcd.print(F("Res. Peak Calc."));
  lcd.setCursor(0, 1);
  lcd.print(F("Type I/C values"));
  delay(5000);
}

void loop() {

  {
    const byte nChars = 15;
    char input[nChars];
    byte idx = 0;
    int key = 0;

    while ( key != '#' ) {
      key = customKeypad.getKey();
      switch (key) {
        case '0' ... '9':
        case '.':
          if ( idx < nChars - 1 ) {
            input[idx++] = key;
            lcd.clear();
            lcd.print(key - 48);
          }
          break;

        case '#':
          input[idx] = '\0';
          lcd.print( ',' );
          break;

        case 'D':
          values();
      }
    }
    return atof(input);
  }
}

void values() {

  result = num1 * num2; // If a 'space' key is pressed (in this case, the '#' key on a 4x4 keypad)

  lcd.clear(); // Clear the writing from the LCD
  lcd.setCursor(0, 0); // Set the cursor position to the first block on the first line
  lcd.print(F("Ind. =")); // Display 'Ind. =' onto the LCD Screen
  lcd.setCursor(7, 0); // Set the cursor position to display after the previous text
  lcd.print(num1, 4); // Display the number value given to num1
  lcd.setCursor(0, 1); // Set the cursor for the first block on the second line
  lcd.print(F("Cap. ="));// Display 'Cap. =' onto the LCD Screen
  lcd.setCursor(7, 1); // Set the cursor position to display after the previoust text
  lcd.print(num2, 2); // Display the number value given to num2
  delay(2500);

  r1 = pow(result, power);
  r2 = r1 * TWO_PI;
  r3 = 1 / r2;
  r4 = pow(result2, power2);
  fc = r3 * r4;
  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print(F("Res. Peak =")); // Display the answer onto the LCD Screen
  lcd.setCursor(0, 1);
  lcd.print(fc);
  lcd.setCursor(9, 1);
  lcd.print(F("(Hz)"));
}