safwan092

Project-ON7590-Calculator

Apr 5th, 2021
774
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <Keypad.h>
  2. #include <Wire.h>
  3. #include <LiquidCrystal_I2C.h>
  4.  
  5. LiquidCrystal_I2C lcd(0x27, 20, 4);
  6.  
  7. const byte ROWS = 4;
  8. const byte COLS = 4;
  9.  
  10. char keys [ROWS] [COLS] = {
  11.   {'1', '2', '3', 'A'},
  12.   {'4', '5', '6', 'B'},
  13.   {'7', '8', '9', '+'},
  14.   {'C', '0', '=', '-'}
  15. };
  16. byte rowPins[ROWS] = {2, 3, 4, 5}; //{9, 8, 7, 6};
  17. byte colPins[COLS] = {6, 7, 8, 9}; //{5, 4, 3, 2};
  18.  
  19. Keypad myKeypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );
  20.  
  21. boolean presentValue = false;
  22.  
  23. boolean next = false;
  24.  
  25. boolean final = false;
  26.  
  27. String num1, num2;
  28.  
  29. int answer;
  30. int AValue = 0;
  31. int BValue = 0;
  32. char op;
  33.  
  34. void setup()
  35. {
  36.   Serial.begin(9600);
  37.   lcd.init();                      // initialize the lcd
  38.   lcd.init();
  39.   // Print a message to the LCD.
  40.   lcd.backlight();
  41.   /*lcd.setCursor(3, 0);
  42.   lcd.print("Hello, world!");
  43.   lcd.setCursor(2, 1);
  44.   lcd.print("Ywrobot Arduino!");
  45.   lcd.setCursor(0, 2);
  46.   lcd.print("Arduino LCM IIC 2004");
  47.   lcd.setCursor(2, 3);
  48.   lcd.print("Power By Ec-yuan!");*/
  49.   delay(3000);
  50.   lcd.clear();
  51.   lcd.setCursor(7, 1);
  52.   lcd.print("A = ");
  53.   lcd.print(AValue);
  54.   lcd.setCursor(7, 3);
  55.   lcd.print("B = ");
  56.   lcd.print(BValue);
  57. }
  58.  
  59. void loop() {
  60.  
  61.   char key = myKeypad.getKey();
  62.  
  63.   //(key=='1'||key=='2'||key=='3'||key=='4'||key=='5'||key=='6'||key=='7'||key=='8'||key=='9'||key=='0')
  64.   //(key == 'A' || key == 'B')
  65.  
  66.   if (key != NO_KEY && (key == '1' || key == '2' || key == '3' || key == '4' || key == '5' || key == '6' || key == '7' || key == '8' || key == '9' || key == '0'))
  67.   {
  68.     if (presentValue != true)
  69.     {
  70.       num1 = num1 + key;
  71.       int numLength = num1.length();
  72.       //lcd.clear();
  73.       lcd.setCursor(13 - numLength, 0); //to adjust one whitespace for operator
  74.       lcd.print(num1);
  75.       Serial.print(num1);
  76.     }
  77.     /*else
  78.       {
  79.       num2 = num2 + key;
  80.       int numLength = num2.length();
  81.       //lcd.setCursor(15 - numLength, 1);
  82.       //lcd.print(num2);
  83.       Serial.print(num2);
  84.       final = true;
  85.       }*/
  86.   }
  87.  
  88.   else if (presentValue == false && key != NO_KEY && (key == '-' || key == '+'))
  89.   {
  90.     if (presentValue == false)
  91.     {
  92.       presentValue = true;
  93.       op = key;
  94.       lcd.setCursor(13, 0);
  95.       lcd.print(op);
  96.       Serial.print(op);
  97.       final = true;
  98.     }
  99.   }
  100.  
  101.   else if (final == true && key != NO_KEY && (key == 'A' || key == 'B')) {
  102.     if (op == '+') {
  103.       answer = num1.toInt();
  104.       if (key == 'A') {
  105.         AValue = AValue + answer;
  106.       }
  107.       else {
  108.         BValue = BValue + answer;
  109.       }
  110.     }
  111.     else if (op == '-') {
  112.       answer = num1.toInt();
  113.       if (key == 'A') {
  114.         AValue = AValue - answer;
  115.       }
  116.       else {
  117.         BValue = BValue - answer;
  118.       }
  119.     }
  120.  
  121.     //lcd.clear();
  122.     //lcd.setCursor(15,0);
  123.     //lcd.autoscroll();
  124.     //lcd.print(answer);
  125.     lcd.clear();
  126.     lcd.setCursor(7, 1);
  127.     lcd.print("A = ");
  128.     lcd.print(AValue);
  129.     lcd.setCursor(7, 3);
  130.     lcd.print("B = ");
  131.     lcd.print(BValue);
  132.     Serial.print(" = ");
  133.     Serial.println(answer);
  134.     Serial.print("A = ");
  135.     Serial.println(AValue);
  136.     Serial.print("B = ");
  137.     Serial.println(BValue);
  138.     Serial.println("--------------");
  139.     presentValue = false;
  140.     final = false;
  141.     num1 = "";
  142.     num2 = "";
  143.     answer = 0;
  144.     op = ' ';
  145.     //lcd.noAutoscroll();
  146.   }
  147.   else if (key != NO_KEY && key == 'C') {
  148.     //lcd.clear();
  149.     /*lcd.clear();
  150.     lcd.setCursor(7, 1);
  151.     lcd.print("A = ");
  152.     lcd.print(AValue);
  153.     lcd.setCursor(7, 3);
  154.     lcd.print("B = ");
  155.     lcd.print(BValue);*/
  156.     Serial.println("--------------");
  157.     presentValue = false;
  158.     final = false;
  159.     num1 = "";
  160.     num2 = "";
  161.     //AValue = 0;
  162.     //BValue = 0;
  163.     answer = 0;
  164.     op = ' ';
  165.   }
  166. }
RAW Paste Data