attempting to make a basic calculator but it just outputs 7’s and 8’s and occasionally 0’s.
i have yet to wire up the keypad i am just testing with bare wires.
#include <Arduino.h>
#include <LiquidCrystal.h>
#include <Keypad.h>
#include <Servo.h>
const byte ROWS = 5;
const byte COLS = 4;
char keys[ROWS][COLS] = {
{'C','/','*','-'},
{'7','8','9','+'},
{'4','5','6','+'},
{'1','2','3','='},
{'0','0','.','='}
};
byte rowPins[ROWS] = { 0, 1, 2, 3, 4 };
byte colPins[COLS] = { 5, 6, 7, 8 };
Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );
const int rs = 13, en = 12, d4 = 11, d5 = 10, d6 = 9, d7 = A0;
void setup() {
lcd.begin(16, 2);
lcd.print("QRWMH-Y10T422");
lcd.setCursor(0, 1);
lcd.print("Starting...");
delay(2000);
lcd.clear();`Preformatted text`
}
void updateCursor() {
if (millis() / 250 % 2 == 0 ) {
lcd.cursor();
} else {
lcd.noCursor();
}
}
char operation = 0;
String memory = "";
String current = "";
uint64_t currentDecimal;
bool decimalPoint = false;
double calculate(char operation, double left, double right)
{switch (operation) {
case '+': return left + right;
case '-': return left - right;
case '*': return left * right;
case '/': return left / right;
}
}
void processInput(char key)
{if ('-' == key && current == "") {
current = "-";
lcd.print("-");
return;
}
switch (key)
{
case '+':
case '-':
case '*':
case '/':
if (!operation) {
memory = current;
current = "";
}
operation = key;
lcd.setCursor(0, 1);
lcd.print(key);
lcd.setCursor(current.length() + 1, 1);
return;
case '=':
float leftNum = memory.toDouble();
float rightNum = current.toDouble();
memory = String(calculate(operation, leftNum, rightNum));
current = "";
lcd.clear();
lcd.setCursor(1, 0);
lcd.print(memory);
lcd.setCursor(0, 1);
lcd.print(operation);
return;
}
if ('.' == key && current.indexOf('.') >= 0)
{
return;
}
if ('.' != key && current == "0")
{
current = String(key);
} else if (key) {
current += String(key);
}
lcd.print(key);
if ('C' == key)
{
operation = 0;
memory = "";
current = "";
lcd.clear();
}
}
void loop() {
updateCursor();
char key = keypad.getKey();
if (key) {
processInput(key);
}
}