- Courtesy of NES Academic Club, University of Science - Ho Chi Minh City, Vietnam.
- This is the "firmware" to turn Arduino into a basic calculator, with the abilities to evaluate basic expressions (sin, cos, tan, sqrt supported);
evaluate approximately value of single variable integral (using Riemann middle sum); and solve simple equations (quadratic equation, two-variable linear system of equations). - Project created from scratch and originally maintained by hungngocphat01, with the exception of
Keypad.h(courtesy of Mark Stanley, Alexander Brevig) andLiquidCrystal_I2C.h(courtesy of Frank de Brabander).
Prototype of an Arduino running this "calculator firmware" at NES Academic Club (2020).
| Device | Device pin | Arduino pin |
|---|---|---|
| Keypad | 1 - 8 | 5 - 12 |
| I2C LCD | VCC | 3.3V |
| GND | GND | |
| SDA | A4 | |
| SCL | A5 | |
| Shift button | 1 | GND |
| 2 | A0 |
Calculator.ino: literallymain.cpp.ExprCalc.h: shunting-yard algorithm implementation.Stack.h: implementations ofsstack(Stringstack) andfstack(floatstack).Screen.h: prototype of LCD printing function.Input.h: functions to examine and process keystrokes.Modes.h: functions to handle supported modes of the calculator.tokenizer.py: creates an array of tokens, for debugging purpose only.
- The
void loop()function calls thevoid menuMode()function. This function acts as the main function and the program execution will always stay in this function. When the function is called, the user have to choose the operating mode of the calculator.- If the user chooses COMP mode,
void compMode()will be called. - The same thing happens with EQN mode (
void eqnMode())and INTERGRAL mode (. More details about these mode are available in 5. Working modes.void intgrlMode()) - If the user presses the
MODEbutton in COMP, EQN orINTEGRAL mode, the function which handles the respective mode will return. The program then goes back tovoid menuMode(), waiting for another mode to be choosen.
- If the user chooses COMP mode,
-
4x4 matrix keypad.
-
Shift button (separate button).
-
Layout:
1 2 3 + 4 5 6 - 7 8 9 * . 0 = / -
Layout when shift button is pressed:
^ sqrt pi sin ( ) e cos LEFT RIGHT Ans tan DEL = MODE
- The calculator receives input from the 4x4 matrix keypad, parses the tokens immediately as soon as they are sent from the keypad, and categorizes them into
numbersandoperators. - The user can press
DEL(Shift+0) key to pop the last operand/operator out of the expression, or=to start evaluating. - Then, the original infix expression is converted into a postfix expression and is evaluated (using shunting-yard algorithm).
- Finally, the result is displayed on the LCD screen if there is no error.
- Evaluation result if there is no error.
- Error message if there is least one error occured:
- Math Error: divided by 0, or sqrt of negative number.
- Syntax Error: for instance
1 * + 5 4.2 - ( * 7 ) etc.
- Capable of solving quadratic equation (ax^2 + bx + c = 0, complex solutions are not supported) and 2-variable system of linear equations (ax + by = c).
- Works similarly to COMP Mode.
- User is prompted to choose the equation type.
- Coefficients are asked. User can also enter an equation to be calculated into a value which substitutes for a coefficient.
- The calculator will solve the equation and the result will be displayed on the screen.
- Not enough memory to implement.
- Keypad, LCD screen.
- COMP mode.
- EQN mode.
- Mode changing.
- Screen scrolling.
- Integral mode.
- Following information will be printed to serial monitor:
- Postfix expression.
- Evaluated result.
- New mode (when mode is changed).
- Coefficients and solutions (EQN mode).
