This circuit is designed to function as an Arduino-based calculator with an LCD display and a 4x4 membrane matrix keypad. The Arduino UNO serves as the central processing unit, interfacing with the keypad for user input and the LCD display for output. The keypad allows users to input numbers and arithmetic operations, while the LCD displays the input and the calculated results.
/**
Arduino Calculator
Copyright (C) 2020, Uri Shaked.
Released under the MIT License.
*/
#include <LiquidCrystal.h>
#include <Keypad.h>
/* Display */
LiquidCrystal lcd(12, 11, 10, 9, 8, 7);
/* Keypad setup */
const byte KEYPAD_ROWS = 4;
const byte KEYPAD_COLS = 4;
byte rowPins[KEYPAD_ROWS] = {5, 4, 3, 2}; // Incorrect pin mapping, should be D13, D12, D11, D10
byte colPins[KEYPAD_COLS] = {A3, A2, A1, A0}; // Incorrect pin mapping, should be D9, D8, D7, D6
char keys[KEYPAD_ROWS][KEYPAD_COLS] = {
{'1', '2', '3', '+'},
{'4', '5', '6', '-'},
{'7', '8', '9', '*'},
{'.', '0', '=', '/'}
};
Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, KEYPAD_ROWS, KEYPAD_COLS);
uint64_t value = 0;
void showSpalshScreen() {
lcd.print("GoodArduinoCode");
lcd.setCursor(3, 1);
String message = "Calculator";
for (byte i = 0; i < message.length(); i++) {
lcd.print(message[i]);
delay(50);
}
delay(500);
}
void updateCursor() {
if (millis() / 250 % 2 == 0 ) {
lcd.cursor();
} else {
lcd.noCursor();
}
}
void setup() {
Serial.begin(115200);
lcd.begin(16, 2);
showSpalshScreen();
lcd.clear();
lcd.cursor();
lcd.setCursor(1, 0);
}
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);
}
void loop() {
updateCursor();
char key = keypad.getKey();
if (key) {
processInput(key);
}
}
Note: The code provided has incorrect pin mappings for the keypad. The rowPins
and colPins
arrays should be updated to match the actual wiring as specified in the wiring details section. The corrected pin mappings should be:
byte rowPins[KEYPAD_ROWS] = {13, 12, 11, 10}; // Corrected pin mapping for rows
byte colPins[KEYPAD_COLS] = {9, 8, 7, 6}; // Corrected pin mapping for columns
The rest of the code handles the initialization of the LCD and keypad, the splash screen display, cursor blinking, input processing, and calculation logic. The loop()
function continuously checks for key presses and processes them accordingly.