ARDUINO calculator in PROTEUS.



 #include <LiquidCrystal.h>

#include <Keypad.h> 


const byte ROWS = 4; // Number of rows in the keypad matrix

const byte COLS = 4; // Number of columns in the keypad matrix


// Define the Keymap for the keypad

char keys[ROWS][COLS] = {

  {'7', '8', '9', 'D'},

  {'4', '5', '6', 'C'},

  {'1', '2', '3', 'B'},

  {'*', '0', '#', 'A'}

};


byte rowPins[ROWS] = {A0, A1, A2, A3}; // Pins connected to the keypad rows

byte colPins[COLS] = {10, 9, 8, 7};     // Pins connected to the keypad columns


Keypad kpd = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS); // Initialize the keypad object


LiquidCrystal lcd(12, 11, 5, 4, 3, 2); // Initialize the LCD object with pins


long Num1, Num2, Number; // Variables to store numbers and result of calculations

char key, action;         // Variable to store the pressed key and the arithmetic operation

boolean result = false;   // Flag to indicate if the calculation result is ready


void setup() {

  lcd.begin(16, 2);                // Initialize the LCD with 16 columns and 2 rows

  lcd.print(" Uno Calculator");    // Display initial message on LCD

  lcd.setCursor(0, 1);             // Move cursor to the second row

  lcd.print(" HomeMade Tech ");    // Display additional information

  

  delay(2000);                     // Wait for 2 seconds

  lcd.clear();                     // Clear the LCD screen

}


void loop() {

  // Store pressed key value in a char container

  key = kpd.getKey();


  if (key != NO_KEY)

    DetectButtons(); // Call function to handle button presses


  if (result == true)

    CalculateResult(); // Call function to perform calculation


  DisplayResult(); // Call function to display result on LCD

}


void DetectButtons() {

  lcd.clear(); // Clear the LCD screen

  if (key == '*') {

    Number = Num1 = Num2 = 0; // Reset all variables to 0 when '*' is pressed

    result = false;            // Reset the result flag

  }


  if (key == '1') {

    Serial.println("Button 1");

    if (Number == 0)

      Number = 1;

    else

      Number = (Number * 10) + 1; // If pressed twice

  }


  // Similar code for keys '4', '7', '0', '2', '5', '8', '3', '6', '9'


  // Detecting Buttons on Column 4 (Arithmetic operations)

  if (key == 'A' || key == 'B' || key == 'C' || key == 'D') {

    Num1 = Number; // Store the current value in Number as Num1

    Number = 0;    // Reset Number for entering the next number

    // Determine the arithmetic operation based on the pressed key

    if (key == 'A') {

      action = '+';

    }

    if (key == 'B') {

      action = '-';

    }

    if (key == 'C') {

      action = '*';

    }

    if (key == 'D') {

      action = '/';

    }


    delay(100); // Delay for debouncing

  }

}


void CalculateResult() {

  if (action == '+')

    Number = Num1 + Num2;


  if (action == '-')

    Number = Num1 - Num2;


  if (action == '*')

    Number = Num1 * Num2;


  if (action == '/')

    Number = Num1 / Num2; 

}


void DisplayResult() {

  // Set the cursor to column 0, line 1 and display the results

  lcd.setCursor(0, 0);

  lcd.print(Num1);

  lcd.print(action);

  lcd.print(Num2);


  if (result == true) {

    lcd.print(" =");

    lcd.print(Number);

  }


  // Set the cursor to column 0, line 1 and display the result

  lcd.setCursor(0, 1);

  lcd.print(Number);

}


Comments

Popular posts from this blog

Digital clock " DS1307 RTC" with PIC16F877A microcontroller and I2C

Smart Elevator simulation with arduino uno and Proteus

Temperature sensor with PIC16F877a using Analog to Digital convertion.