Advertisement
aungwinhtut

Keypad Calculator

Jun 19th, 2020
2,213
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.37 KB | None | 0 0
  1. #include <LiquidCrystal_I2C.h>
  2. LiquidCrystal_I2C lcd(0x27, 20, 2); // set the LCD address to 0x27 for a 16 chars and 2 line display
  3.  
  4. #include <Keypad.h>
  5.  
  6. const byte ROWS = 4;
  7. const byte COLS = 4;
  8.  
  9. char keys [ROWS] [COLS] = {
  10.   {'1', '2', '3', '+'},
  11.   {'4', '5', '6', '-'},
  12.   {'7', '8', '9', '*'},
  13.   {'C', '0', '=', '/'}
  14. };
  15. byte rowPins[ROWS] = {10, 9, 7, 6};
  16. byte colPins[COLS] = {5, 4, 3, 2};
  17.  
  18. Keypad myKeypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );
  19.  
  20.  
  21. boolean presentValue = false;
  22. boolean next = false;
  23. boolean final = false;
  24. String num1, num2;
  25. int answer = 0;
  26. char op;
  27.  
  28. void setup()
  29. {
  30.   lcd.init();                      
  31.   lcd.backlight();
  32.   lcd.setCursor(3,0);
  33.   lcd.print("Maker UNO");
  34.   lcd.setCursor(3,1);
  35.   lcd.print("Calculator");
  36.   delay(3000);
  37.   lcd.clear();
  38. }
  39.  
  40. void loop() {
  41.   char key = myKeypad.getKey();
  42.  
  43.   if (key != NO_KEY && (key == '1' || key == '2' || key == '3' || key == '4' || key == '5' || key == '6' || key == '7' || key == '8' || key == '9' || key == '0'))
  44.   {
  45.     if (presentValue != true)
  46.     {
  47.       num1 = num1 + key;
  48.       int numLength = num1.length();
  49.       lcd.setCursor(0, 0);
  50.       lcd.print(num1);
  51.     }
  52.     else
  53.     {
  54.       num2 = num2 + key;
  55.       int numLength = num2.length();
  56.       int numLength1 = num1.length();
  57.       lcd.setCursor(1 + numLength1, 0);
  58.       lcd.print(num2);
  59.       final = true;
  60.     }
  61.   }
  62.  
  63.   else if (presentValue == false && key != NO_KEY && (key == '/' || key == '*' || key == '-' || key == '+'))
  64.   {
  65.     if (presentValue == false)
  66.     {
  67.       int numLength = num1.length();
  68.       presentValue = true;
  69.       op = key;
  70.       lcd.setCursor(0 + numLength, 0);
  71.       lcd.print(op);
  72.     }
  73.   }
  74.  
  75.   else if (final == true && key != NO_KEY && key == '=') {
  76.      
  77.     if (op == '+') {
  78.       answer = num1.toInt() + num2.toInt();
  79.     }
  80.     else if (op == '-') {
  81.       answer = num1.toInt() - num2.toInt();
  82.     }
  83.     else if (op == '*') {
  84.       answer = num1.toInt() * num2.toInt();
  85.     }
  86.     else if (op == '/') {
  87.       answer = num1.toInt() / num2.toInt();
  88.     }
  89.     lcd.clear();
  90.     lcd.setCursor(16, 1);
  91.     lcd.autoscroll();
  92.     lcd.print(answer);
  93.     lcd.noAutoscroll();
  94.    
  95.   }
  96.   else if (key != NO_KEY && key == 'C') {
  97.     lcd.clear();
  98.     presentValue = false;
  99.     final = false;
  100.     num1 = "";
  101.     num2 = "";
  102.     answer = 0;
  103.     op = ' ';
  104.   }
  105. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement