JACK
Published

Arduino interfacing with Calculator

In this article we will learn how to Interface Calculator using Arduino Uno

ExpertFull instructions provided2 hours295
Arduino interfacing with Calculator

Things used in this project

Hardware components

Arduino UNO
Arduino UNO
×1
Standard LCD - 16x2 White on Blue
Adafruit Standard LCD - 16x2 White on Blue
×1
Grove - 12-Channel Capacitive Touch Keypad (ATtiny1616)
Seeed Studio Grove - 12-Channel Capacitive Touch Keypad (ATtiny1616)
×1
Pushbutton switch 12mm
SparkFun Pushbutton switch 12mm
×1

Software apps and online services

Arduino IDE
Arduino IDE
Proteus

Hand tools and fabrication machines

Soldering iron (generic)
Soldering iron (generic)
Solder Wire, Lead Free
Solder Wire, Lead Free
Wire Stripper & Cutter, 32-20 AWG / 0.05-0.5mm² Solid & Stranded Wires
Wire Stripper & Cutter, 32-20 AWG / 0.05-0.5mm² Solid & Stranded Wires
Solder Paste, Rework
Solder Paste, Rework

Story

Read more

Schematics

Arduino Interfacing with Calculator

Code

Arduino Interfacing with Calculator

C#
#include <Keypad.h>
#include <LiquidCrystal.h>

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, A2, A3};

// Created instances
Keypad myKeypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);
LiquidCrystal lcd(13, 12, 11, 10, 9, 8); // Initialize the LCD on these pins

boolean firstNumState = true;
String firstNum = "";
String secondNum = "";
float result = 0.0;
char operatr = ' ';

const int squareButtonPin = A0; // Connect 'Square' button to analog pin A0
const int cubeButtonPin = A1;  // Connect 'Cube' button to analog pin A1

void setup() {
  lcd.begin(16, 2);
  lcd.setCursor(0, 0);
  lcd.print("Arduino Calculator");
  lcd.setCursor(0, 1);
  lcd.print("by Your Name");
  delay(1000);
  lcd.clear();
  clr();

  pinMode(squareButtonPin, INPUT_PULLUP);
  pinMode(cubeButtonPin, INPUT_PULLUP);
}

void loop() {
  char newKey = myKeypad.getKey();

  if (newKey != NO_KEY && (newKey == '1' || newKey == '2' || newKey == '3' || newKey == '4' || newKey == '5' || newKey == '6' || newKey == '7' || newKey == '8' || newKey == '9' || newKey == '0')) {

    if (firstNumState == true) {
      firstNum = firstNum + newKey;
      lcd.print(newKey);
    }
    else {
      secondNum = secondNum + newKey;
      lcd.print(newKey);
    }
  }

  if (newKey != NO_KEY && (newKey == '+' || newKey == '-' || newKey == '*' || newKey == '/')) {
    if (firstNumState == true) {
      operatr = newKey;
      firstNumState = false;
      lcd.setCursor(15, 0);
      lcd.print(operatr);
      lcd.setCursor(5, 1);
    }
  }

  if (newKey != NO_KEY && newKey == '=') {
    if (operatr == '+') {
      result = firstNum.toFloat() + secondNum.toFloat();
    }

    if (operatr == '-') {
      result = firstNum.toFloat() - secondNum.toFloat();
    }

    if (operatr == '*') {
      result = firstNum.toFloat() * secondNum.toFloat();
    }

    if (operatr == '/') {
      result = firstNum.toFloat() / secondNum.toFloat();
    }

    lcd.clear();
    lcd.setCursor(0, 0);
    lcd.print(firstNum);
    lcd.print(operatr);
    lcd.print(secondNum);
    lcd.setCursor(0, 1);
    lcd.print("=");
    lcd.print(result);
    firstNumState = true;
  }

  if (newKey != NO_KEY && newKey == 'C') {
    clr();
  }

  if (digitalRead(squareButtonPin) == LOW) {
    // 'Square' button is pressed
    float num = firstNum.toFloat();
    float square = num * num;
    lcd.clear();
    lcd.setCursor(0, 0);
    lcd.print("Square of ");
    lcd.print(num);
    lcd.setCursor(0, 1);
    lcd.print("is");
    lcd.setCursor(5, 1);
    lcd.print(square);
    delay(2000);
    clr();
  }

  if (digitalRead(cubeButtonPin) == LOW) {
    // 'Cube' button is pressed
    float num = firstNum.toFloat();
    float cube = num * num * num;
    lcd.clear();
    lcd.setCursor(0, 0);
    lcd.print("Cube of ");
    lcd.print(num);
    lcd.setCursor(0, 1);
    lcd.print("is");
    lcd.setCursor(5, 1);
    lcd.print(cube);
    delay(2000);
    clr();
  }
}

void clr() {
  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print("1st: ");
  lcd.setCursor(12, 0);
  lcd.print("op ");
  lcd.setCursor(0, 1);
  lcd.print("2nd: ");
  lcd.setCursor(5, 0);
  firstNum = "";
  secondNum = "";
  result = 0;
  operatr = ' ';
}

Credits

JACK

JACK

21 projects • 1 follower

Comments