Facebook
From Greatech, 3 Years ago, written in Plain Text.
Embed
Download Paste or View Raw
Hits: 319
  1. #include <Keypad.h>
  2. #include <Wire.h>
  3. #include <LiquidCrystal.h>
  4. LiquidCrystal lcd(13, 12, 11, 10, 9, 8);
  5.  
  6. long first = 0;
  7. long second = 0;
  8. double total = 0;
  9. char customKey;
  10. const byte ROWS = 4;
  11. const byte COLS = 4;
  12.  
  13. char keys[ROWS][COLS] = {
  14.   {'1','2','3','+'},
  15.   {'4','5','6','-'},
  16.   {'7','8','9','*'},
  17.   {'C','0','=','/'}
  18. };
  19. byte rowPins[ROWS] = {7,6,5,4}; //connect to the row pinouts of the keypad
  20. byte colPins[COLS] = {3,2,1,0}; //connect to the column pinouts of the keypad
  21.  
  22. //initialize an instance of class NewKeypad
  23. Keypad customKeypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS);
  24.  
  25. void setup()
  26. {
  27.   lcd.begin(16, 2);              
  28.   for(int i=0;i<=3;i++);
  29.   lcd.setCursor(0,0);
  30.     lcd.print("CALCULATOR");
  31.     lcd.setCursor(0,1);
  32.     lcd.print("GREATECH");
  33.   delay(2500);
  34.   lcd.clear();
  35.   lcd.setCursor(0, 0);
  36. }
  37.  
  38.  
  39. void loop()
  40. {
  41.  
  42.     customKey = customKeypad.getKey();
  43.     switch(customKey)
  44.     {
  45.         case '0' ... '9':
  46.           lcd.setCursor(0,0);
  47.           first = first * 10 + (customKey - '0');
  48.           lcd.print(first);
  49.           break;
  50.      
  51.         case '+':
  52.           first = (total != 0 ? total : first);
  53.           lcd.setCursor(0,1);
  54.           lcd.print("+");
  55.           second = SecondNumber();
  56.           total = first + second;
  57.           lcd.setCursor(0,3);
  58.           lcd.print(total);
  59.           first = 0, second = 0;
  60.           break;
  61.      
  62.         case '-':
  63.           first = (total != 0 ? total : first);
  64.           lcd.setCursor(0,1);
  65.           lcd.print("-");
  66.           second = SecondNumber();
  67.           total = first - second;
  68.           lcd.setCursor(0,3);
  69.           lcd.print(total);
  70.           first = 0, second = 0;
  71.           break;
  72.      
  73.         case '*':
  74.           first = (total != 0 ? total : first);
  75.           lcd.setCursor(0,1);
  76.           lcd.print("*");
  77.           second = SecondNumber();
  78.           total = first * second;
  79.           lcd.setCursor(0,3);
  80.           lcd.print(total);
  81.           first = 0, second = 0;
  82.           break;
  83.      
  84.         case '/':
  85.           first = (total != 0 ? total : first);
  86.           lcd.setCursor(0,1);
  87.           lcd.print("/");
  88.           second = SecondNumber();
  89.           lcd.setCursor(0,3);
  90.      
  91.           second == 0 ? lcd.print("Invalid") : total = (float)first / (float)second;
  92.      
  93.           lcd.print(total);
  94.           first = 0, second = 0;
  95.           break;
  96.      
  97.         case 'C':
  98.           total = 0;
  99.           lcd.clear();
  100.           break;
  101.     }
  102. }
  103.  
  104. long SecondNumber()
  105. {
  106.     while( 1 )
  107.     {
  108.           customKey = customKeypad.getKey();
  109.           if(customKey >= '0' && customKey <= '9')
  110.           {
  111.                   second = second * 10 + (customKey - '0');
  112.                   lcd.setCursor(0,2);
  113.                   lcd.print(second);
  114.           }
  115.      
  116.           if(customKey == '=') break;
  117.     }
  118.    return second;
  119. }