Question

In: Computer Science

Build an interactive calculator. Using an Arduino Uno, the OLED Display and matrix keypad to build...

Build an interactive calculator.

Using an Arduino Uno, the OLED Display and matrix keypad to build a simple calculator application. The user presses the operands and operators using the keypad. The calculation output should be displayed on the OLED display. You may make the following simplifying assumptions.

a)Assume 1 digit numbers

b) Assume only two operators ‘+’ and ‘-’

Upload the breadboard diagram and the code

Solutions

Expert Solution

here is the answer of your problem:
Materials required:

__to make a calculator, we will need some kind of Processor, for which we can use Arduino, since this is a small project and needs large memory space for complicated code, we will use Arduino NANO.

__for taking input number, we can use a 4x4 keypad with membrane switches, which has 4 rows and 4 columns, if we open up this sticker from behind, the flexible membrane PCB is visible, which has connections going from ROWS and COLUMNS to connecting pins via flexible PCB. When any membrane switch is pressed, the rows and column pins process the data as a Matrix and identify the location of key pressed.(Remember, left side pins are row pins and right side pins are column pins.)

__for display we can use either OLED or LCD display, but I would like to use OLED, since the resolution of OLED is far better, we can have longer strings of numbers to operate, if we choose OLED.

_.-Connections:

--Now because we discudded above about the materials we needed, so now its the time to make connections now.

...Below will be the steps for making connections:

  1. connect Keypad to any digital pins on Arduino Nano ( preferably D2 to D9).
  2. connect OLEDs SCL to Arduinos A5.
  3. connect OLEDs SDA to Arduinos A4.

  4. connect OLEDs VCC to Arduinos 5v.

  5. connect OLEDs GND to Arduinos GND.

  6. Connect a Switch between Positive of Lithium Polymer Battery.

  7. Connect Lipo to 5V and Gnd pins of Arduino.

Code and libraries requires:

Below are the list of libraries required:

  • SPI
  • Keypad
  • wire
  • Adafruit SSD1306
  • Adafruit GFX

Coding:

#include <Keypad.h>
#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>

#define OLED_RESET 4
Adafruit_SSD1306 display(OLED_RESET);

#if (SSD1306_LCDHEIGHT != 64)
#error("Height incorrect, please fix Adafruit_SSD1306.h!");
#endif

const byte numRows= 4; //number of rows on the keypad
const byte numCols= 4; //number of columns on the keypad

//keymap defines the key pressed according to the row and columns just as appears on the keypad
char customKey;
double first = 0;
long second = 0;
double total = 0;
char Operator;
bool equalPressed=false;
bool showFirst = false;

char keymap[numRows][numCols]=
{
{'1', '2', '3', '+'},
{'4', '5', '6', '-'},
{'7', '8', '9', 'x'},
{'C', '0', '=', '/'}
};

//Code that shows the the keypad connections to the arduino terminals
byte rowPins[numRows] = {9,8,7,6}; //Rows 0 to 3
byte colPins[numCols]= {5,4,3,2}; //Columns 0 to 3

//initializes an instance of the Keypad class
Keypad customKeypad= Keypad(makeKeymap(keymap), rowPins, colPins, numRows, numCols);

void showSplash() {
String splashString="Arduino Calculator by";
display.clearDisplay();
display.setTextSize(1);
display.setTextColor(WHITE);
display.setCursor(0,0);
display.setCursor(64-(splashString.length()*3),0);
display.print(splashString);
display.setTextSize(2);
splashString="Mission";
display.setCursor(64-(splashString.length()*6),16);
display.print(splashString);
display.setTextSize(2);
splashString="Critical";
display.setCursor(64-(splashString.length()*6),40);
display.print(splashString);
display.display();
delay(3000);
}

void setup()
{
Serial.begin(9600);
display.begin(SSD1306_SWITCHCAPVCC, 0x3C); // initialize with the I2C addr 0x3D (for the 128x64)
showSplash();
display.print("Arduino Calulator By Mission Critical");
display.setTextSize(2);
display.clearDisplay();
display.display();
}

void loop(){
customKey = customKeypad.getKey();
switch(customKey)
{
case '0' ... '9': // This keeps collecting the first value until a operator is pressed "+-*/"
showFirst=true;
first = first * 10 + (customKey - '0');
showDisplay();
break;

case '+':
showFirst=true;
Operator='+';
showDisplay();
second = SecondNumber(); // get the collected the second number
total = first + second;
showDisplay();
first = total, // reset values back to zero for next use
second = 0;   
break;

case '-':
showFirst=true;
Operator='-';
showDisplay();
second = SecondNumber();
total = first - second;
showDisplay();
first = total, second = 0;
break;

case 'x':
showFirst=true;
Operator='x';
showDisplay();
second = SecondNumber();
total = first * second;
showDisplay();
first = total, second = 0;
break;

case '/':
showFirst=true;
Operator='/';
showDisplay();
second = SecondNumber();
second == 0 ? display.print("Invalid") : total = (float)first / (float)second;
showDisplay();
first = total, second = 0;
break;

case 'C':
total = 0;
first=0;
second=0;
Operator='\0';
showFirst=false;
equalPressed=false;
display.clearDisplay();
display.display();
break;
}
}

void showDisplay()
{
display.clearDisplay();
display.setCursor(110,0);
display.println(Operator);
if (showFirst) {
Serial.print(first);
display.println(first);
} else
{
display.println("");
}
if (second>0) {
display.println(second);
} else
{
display.println("");
}
if (equalPressed) {
display.println(total);
equalPressed=false;
}
display.display();
}
long SecondNumber()
{
while( 1 )
{
customKey = customKeypad.getKey();
if(customKey >= '0' && customKey <= '9')
{
second = second * 10 + (customKey - '0');
showDisplay();
}

if(customKey == '=') {
equalPressed=true;
break; //return second;
}
}
return second;
}

Let me know if you have any doubts or if you need anything to change.

If you are satisfied with the solution, please leave a +ve feedback : ) Let me know for any help with any other questions.

Thank You!
====================================================================


Related Solutions

Module 14: Memory, OLED and Keypads Homework Build an interactive calculator. Use the OLED Display and...
Module 14: Memory, OLED and Keypads Homework Build an interactive calculator. Use the OLED Display and matrix keypad to build a simple calculator application. The user presses the operands and operators using the keypad. The calculation output should be displayed on the OLED display. You may make the following simplifying assumptions. a) Assume 1 digit numbers b) Assume only two operators ‘+’ and ‘-’ Upload the breadboard diagram the code and a video. Points 6: 2 points each for working...
write the code of 4 digit 7-segment display using arduino uno in assembly language by using...
write the code of 4 digit 7-segment display using arduino uno in assembly language by using software AVR STUDIO 5.1?
Arduino code , using DS3231 library to set time/ date by keypad and print that on...
Arduino code , using DS3231 library to set time/ date by keypad and print that on lcd. i would like to make user to set time and date from the keypad .
With an Arduino Uno: Using 2 external buttons b0 and b1 to represent ‘0’ and ‘1’,...
With an Arduino Uno: Using 2 external buttons b0 and b1 to represent ‘0’ and ‘1’, design a sequence detector to detect a pattern “1101”. An LED lights up once, when the sequence “1101” occurs.
this is code to Temperature and Humidity Monitoring over ThingSpeak using Arduino UNO and ESP8266 i...
this is code to Temperature and Humidity Monitoring over ThingSpeak using Arduino UNO and ESP8266 i need someone explain the steps for me #include <stdlib.h> #include <DHT.h> #define DHTPIN 5         // DHT data pin connected to Arduino pin 5 #define DHTTYPE DHT11     // DHT11 (DHT Sensor Type ) DHT dht(DHTPIN, DHTTYPE); // Initialize the DHT sensor #define SSID "WiFi Name"     // "WiFi Name" #define PASS "WiFi Password"       // "Password" #define IP "184.106.153.149"// thingspeak.com ip String msg = "GET /update?key=Your API...
What would be the wiring setup and coding using an Arduino for a display of the...
What would be the wiring setup and coding using an Arduino for a display of the temperature of the room it is in?
I need simulation on protues of speed control of unipolar stepper motor using uno arduino. Must...
I need simulation on protues of speed control of unipolar stepper motor using uno arduino. Must using protues..
write code using Arduino to display the digits of pi(π) to the 20th decimal point on...
write code using Arduino to display the digits of pi(π) to the 20th decimal point on a 7-segment display. You code should include functions for all the digits, and your loop( ) should only call the functions to display the numbers. Delay 500ms between each number. Use macro definitions for the individual LED's.
Using an arduino, breadboard,  ultrasonic sensor, buzzer, lcd and red led display distance in centimeters, If needed,...
Using an arduino, breadboard,  ultrasonic sensor, buzzer, lcd and red led display distance in centimeters, If needed, use a rotary potentiometer, male to male cable (m-m) female to male cable (f-m) or any resistor provided in the kit. display distance in centimeters to lcd. If the object is in distance, turn on Active buzzer(play sound) and turn on red led. If the object is not in range, display out of range or "Object now close'.  on the lcd, make sure the buzzer...
The purpose of this assignment is to build the business calculator using supporting files built inTopics...
The purpose of this assignment is to build the business calculator using supporting files built inTopics 4 and 5.Create a Java application file named RPN.java containing a main method by using the ForthStack.java and associated files from Topic 5.The application should have one text box for numeric data entry, one text box for numeric display, one text box for error display, and buttons labeled "+", "-", "*", "/","dup", "2dup", "clr", "pop" and "push." The actions of the controls should be...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT