This repository contains Arduino code for a calculator that accepts mathematical expressions entered through a 4x4 matrix keypad. The calculator converts the infix expression into postfix form using the Shutting Yard Algorithm,and displays both the postfix form and the result on a 2x16 Crystal LCD.
The conversion to postfix form and the evaluation of the expression are performed using a dynamically allocated stack.
See it in action here.
example.mp4
To use the calculator, the user starts by entering the expression (up to 33 characters) on the keypad connected to digital pins 2 through 9. The LCD displays the entered expression in real-time as the user presses each key.
Pressing the button connected to digital pin 13 triggers the conversion and evaluation of the entered expression.
Once triggered, the infix expression disappears from the LCD and is replaced by the postfix form. Shortly after, the postfix form is replaced by the evaluated result of the expression, which can be used for further operations.
In order to remove anything, the Arduino must be manually reset.
I get input from the keypad using "polling". This means that I constantly check if a button has been pressed by checking if the coresponding pins both read digital low. I begin by setting the row pins to the *INPUT_PULLUP* mode (set them as input pins with the pull up resistors activated), so they'll read low only if the button is pressed and will otherwise read as digital high, and set the column pins to digital high. In the loop, I will go trough the columns, setting them as LOW, and checking if any row pin's input reads low as well (if the corresponding button has been pressed). Once the polling function detects that a button's been pressed, it returns the corresponding character from the keypad's map to add to the expression's string.
The 2x16 LCD I use is attached via an i2c interface. To interact with it, I made use of the LiquidCrystal_I2C.h library. Documentation about it alongside a program that helps find the i2c adress of a device connected to an Arduino can be found here.
error.mp4
When the user attempts to write characters after the LCD's limit has been reached, the LCD will display an error message and return to displaying the expression. The character that triggered the error will not be added to it.
This is done by constantly checking the length of the expression.