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!