Calculator TinkerCad Contest
by Saim Azfar in Circuits > Arduino
7241 Views, 0 Favorites, 0 Comments
Calculator TinkerCad Contest
Hey, so recently I have been exploring how to implement different types of code into a circuit. I found that making a calculator would be a great way to implement "case" and other forms of code I had found interesting. I have in the past made calculators straight from code, but making a circuit for it gave me interest. Especially during this time of quarantine where I am on my computer almost the whole day. The project is to implement mathematical operations on a LCD screen.
Materials
For Circuit:
- LCD 16 x 2
- Arduino Uno R3
- Keypad 4x4
- Small Breadboard
- Potentiometer (250 kΩ)
- Resistor (1kΩ)
- x26 Jumper wires
Connecting the 4x4 Keypad
Connect the 4 row pins on the 4x4 keypad to the Arduino pins 4-7, and connect the 4 column pins to the Arduino pins 0-3.
Provide Power to the Breadboard and Connect LCD
I used a power voltage of 5 for the breadboard. I connected the power and ground to the breadboard. The LCD is placed on to the breadboard, and placed so all of its pins get connected to the breadboard.
Connect Power and Ground to the LCD.
There will be 3 pins of ground needed to be connected to the LCD. One will be connected the the ground its self of the LCD, another will be connected to the LED of the LCD, and the last one will be connected to the RW. The VCC of the LCD and the led will require power to be connected. However the power for the LED will require a resistor connected in this case I used a 1kΩ resistor.
Connecting the Potentiometer
Connect the potentiometer to the breadboard with 3 free columns. It will have 3 pins, the column which contains the terminal 1 pin will need ground given to it. The column which contains the terminal 2 pin will need power given to it. Then the wiper will have a jumper wire in it's column which connects to VO of the LCD.
Connecting the Arduino to LCD
Pins 8-13 on the Arduino will be connected to the LCD. Pins 8-11 on the Arduino will connect to D8(7-4) respectively. Then pin 12 of the Arduino will connect with the Enable of the LCD, and pin 13 on the Arduino will connect to the register of the LCD.
Implement Code
Code will be needed to use mathematical operations with the keypad and LCD. The following will be the code I used, however multiple changes can still me implemented to make it cleaner and better. So feel free to play with it a little.
#include <LiquidCrystal.h>
#include <Keypad.h>
LiquidCrystal lcd(13, 12, 11, 10, 9, 8);
long first = 0;
long second = 0;
double total = 0;
int posit = 0 ;
char customKey;
const byte ROWS = 4;
const byte COLS = 4;
char keys[ROWS][COLS] = {
{'1','2','3','/'},
{'4','5','6','*'},
{'7','8','9','-'},
{'C','0','=','+'} };
byte rowPins[ROWS] = {7 ,6 ,5 ,4};
byte colPins[COLS] = {3, 2, 1, 0};
Keypad customKeypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS);
void setup(){
lcd.begin(16,2);
lcd.setCursor(5,0);
lcd.clear(); }
void loop() {
customKey = customKeypad.getKey();
switch(customKey) {
case '0' ... '9':
lcd.setCursor(0,0);
first = first * 10 + (customKey - '0');
lcd.print(first);
posit++;
break;
case '+':
first = (total != 0 ? total : first);
lcd.setCursor(posit,0);
lcd.print("+");
posit++;
second = SecondNumber();
total = first + second;
lcd.setCursor(1,1);
lcd.print(total);
first = 0,
second = 0;
posit=0;
break;
case '-':
first = (total != 0 ? total : first);
lcd.setCursor(posit,0);
lcd.print("-");
posit++;
second = SecondNumber();
total = first - second;
lcd.setCursor(1,1);
lcd.print(total);
first = 0,
second = 0;
posit=0;
break;
case '*':
first = (total != 0 ? total : first);
lcd.setCursor(posit,0);
lcd.print("*");
posit++;
second = SecondNumber();
total = first * second;
lcd.setCursor(1,1);
lcd.print(total);
first = 0,
second = 0;
posit=0;
break;
case '/':
first = (total != 0 ? total : first);
lcd.setCursor(posit,0);
lcd.print("/");
posit++;
second = SecondNumber(); lcd.setCursor(1,1);
second == 0 ? lcd.print("Error") : total = (float)first / (float)second;
lcd.print(total);
first = 0,
second = 0;
posit=0;
break;
case 'C':
total = 0;
first = 0;
second = 0;
posit = 0;
lcd.clear();
break; }
}
long SecondNumber() {
while( 1 ) {
customKey = customKeypad.getKey();
if(customKey >= '0' && customKey <= '9') {
second = second * 10 + (customKey - '0');
lcd.setCursor(posit,0);
lcd.print(second); }
if(customKey == 'C') {
total = 0;
first = 0;
second = 0;
posit = 0;
lcd.clear();
break; }
if(customKey == '='){
lcd.setCursor(0,1);
lcd.print("=");
posit = total;
lcd.clear();
lcd.setCursor(0,1);
lcd.print("=");
break; }
}
return second;}
Result
I hope you all enjoyed this instructable. Thank you for reading!
Saim.