0% found this document useful (0 votes)
14 views5 pages

Lab 9

This document contains code for a basic 4x4 keypad calculator circuit that uses an Arduino and 16x2 LCD display. The keypad is initialized and mapped. In setup, the LCD is configured and cleared. In loop, keys pressed are read and used to either display the first number, perform operations like addition/subtraction, or clear the display. SecondNumber() is used to collect the second number input. Calculations are displayed on the LCD.

Uploaded by

Hasnain Tariq
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views5 pages

Lab 9

This document contains code for a basic 4x4 keypad calculator circuit that uses an Arduino and 16x2 LCD display. The keypad is initialized and mapped. In setup, the LCD is configured and cleared. In loop, keys pressed are read and used to either display the first number, perform operations like addition/subtraction, or clear the display. SecondNumber() is used to collect the second number input. Calculations are displayed on the LCD.

Uploaded by

Hasnain Tariq
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Lab 9

Hasnain Tariq
63578

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

LiquidCrystal lcd(8,9,10,11,12,13);
const byte ROWS = 4;
const byte COLS = 4;

char keys[ROWS][COLS]= {
{'1','2','3','H'},
{'4','5','6','A'},
{'7','8','I','S'},
{' ','0',':','N'}
};

byte rowPins[ROWS]={7,6,5,4};
byte colPins[COLS]={3,2,1,0};

Keypad keypad = Keypad(makeKeymap(keys),rowPins,colPins,ROWS,COLS);

void setup()
{
for(int k=8;k<14;k++)
{
pinMode(k,OUTPUT);

}
lcd.begin(16,2);
}
void loop()
{
char key = keypad.getKey();
if (key != NO_KEY)
{
if(key == ' ')
{
lcd.setCursor(0,0);
lcd.print("ID:");
}
else if(key == ':')
{
lcd.setCursor(0,1);
lcd.print("NAME");
}
lcd.print(key);
}

//Name: Shreoshree Adhikari


//Roll no :18CS8012

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

LiquidCrystal lcd(13, 12, 11, 10, 9, 8);

long first = 0;
long second = 0;
double total = 0;

char customKey;
const byte ROWS = 4;
const byte COLS = 4;

char keys[ROWS][COLS] = {
//calculator display
{'1','2','3','+'},
{'4','5','6','-'},
{'7','8','9','*'},
{'C','0','=','/'}
};
byte rowPins[ROWS] = {7,6,5,4}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {3,2,1,0}; //connect to the column pinouts of the keypad

//initialize an instance of class NewKeypad


Keypad customKeypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS);

void setup()
{
lcd.begin(16, 2); // start lcd
for(int i=0;i<=3;i++);
lcd.setCursor(0,0);
delay(2000);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Enter Num:");
delay(2000);
lcd.clear();
}

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

case '+':
first = (total != 0 ? total : first);
lcd.setCursor(0,1);
lcd.print(" = ");
lcd.print("+");
second = SecondNumber(); // get the collected the second number
total = first + second;
lcd.setCursor(0,3);
lcd.print("Result = ");
lcd.print(total);
first = 0, second = 0; // reset values back to zero for next use
break;

case '-':
first = (total != 0 ? total : first);
lcd.setCursor(0,1);
lcd.print("= ");
lcd.print("-");
second = SecondNumber();
total = first - second;
lcd.setCursor(0,3);
lcd.print("Ans= ");
lcd.print(total);
first = 0, second = 0;
break;

case '*':
first = (total != 0 ? total : first);
lcd.setCursor(0,1);
lcd.print("= ");
lcd.print("*");
second = SecondNumber();
total = first * second;
lcd.setCursor(0,3);
lcd.print("Ans = ");
lcd.print(total);
first = 0, second = 0;
break;

case '/':
first = (total != 0 ? total : first);
lcd.setCursor(0,1);
lcd.print("= ");
lcd.print("/");
second = SecondNumber();
lcd.setCursor(0,3);

second == 0 ? lcd.print("InCorrect") : total = (float)first / (float)second;


lcd.print("Ans = ");
lcd.print(total);
first = 0, second = 0;
break;

case 'C':
total = 0;
lcd.clear();
break;
}
}

long SecondNumber()
{
while( 1 )
{
customKey = customKeypad.getKey();
if(customKey >= '0' && customKey <= '9')
{
second = second * 10 + (customKey - '0');
lcd.setCursor(0,0);
lcd.print("= ");
lcd.print(second);
}

if(customKey == '=') break; //return second;


}
return second;
}

You might also like