Advertisement
safwan092

test بالوت حاسبة

Mar 18th, 2021
868
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.55 KB | None | 0 0
  1. #include <Keypad.h>
  2.  
  3. const byte ROWS = 4;
  4. const byte COLS = 4;
  5.  
  6. char keys [ROWS] [COLS] = {
  7.   {'1', '2', '3', 'A'},
  8.   {'4', '5', '6', 'B'},
  9.   {'7', '8', '9', '+'},
  10.   {'C', '0', '=', '-'}
  11. };
  12. byte rowPins[ROWS] = {9,8,7,6};
  13. byte colPins[COLS] = {5,4,3,2};
  14.  
  15. Keypad myKeypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );
  16.  
  17. boolean presentValue = false;
  18.  
  19. boolean next = false;
  20.  
  21. boolean final = false;
  22.  
  23. String num1, num2;
  24.  
  25. int answer;
  26. int AValue=0;
  27. int BValue=0;
  28. char op;
  29.  
  30. void setup()
  31. {
  32.   Serial.begin(9600);
  33.   delay(3000);
  34. }
  35.  
  36. void loop(){
  37.  
  38.   char key = myKeypad.getKey();
  39.  
  40.   if (key != NO_KEY && (key=='1'||key=='2'||key=='3'||key=='4'||key=='5'||key=='6'||key=='7'||key=='8'||key=='9'||key=='0'))
  41.   {
  42.     if (presentValue != true)
  43.     {
  44.       num1 = num1 + key;
  45.       int numLength = num1.length();
  46.       //lcd.setCursor(15 - numLength, 0); //to adjust one whitespace for operator
  47.       //lcd.print(num1);
  48.       Serial.print(num1);
  49.     }
  50.     else
  51.     {
  52.       num2 = num2 + key;
  53.       int numLength = num2.length();
  54.       //lcd.setCursor(15 - numLength, 1);
  55.       //lcd.print(num2);
  56.       Serial.print(num2);
  57.       final = true;
  58.     }
  59.   }
  60.  
  61.   else if (presentValue == false && key != NO_KEY && (key == '-' || key == '+'))
  62.   {
  63.     if (presentValue == false)
  64.     {
  65.       presentValue = true;
  66.       op = key;
  67.       //lcd.setCursor(15,0);
  68.       //lcd.print(op);
  69.       Serial.print(op);
  70.     }
  71.   }
  72.  
  73.   else if (final == true && key != NO_KEY && (key == 'A' || key == 'B')){
  74.     if (op == '+'){
  75.       answer = num1.toInt() + num2.toInt();
  76.       if(key == 'A'){
  77.         AValue = AValue + answer;
  78.       }
  79.       else{
  80.         BValue = BValue + answer;
  81.       }
  82.     }
  83.     else if (op == '-'){
  84.       answer = num1.toInt() - num2.toInt();
  85.       if(key == 'A'){
  86.         AValue = AValue - answer;
  87.       }
  88.       else{
  89.         BValue = BValue - answer;
  90.       }
  91.     }
  92.    
  93.       //lcd.clear();
  94.       //lcd.setCursor(15,0);
  95.       //lcd.autoscroll();
  96.       //lcd.print(answer);
  97.       Serial.print(" = ");
  98.       Serial.println(answer);
  99.       Serial.print("A = ");
  100.       Serial.println(AValue);
  101.       Serial.print("B = ");
  102.       Serial.println(BValue);
  103.       presentValue = false;
  104.       final = false;
  105.       num1 = "";
  106.       num2 = "";
  107.       answer = 0;
  108.       op = ' ';
  109.       //lcd.noAutoscroll();
  110.   }
  111.   else if (key != NO_KEY && key == 'C'){
  112.     //lcd.clear();
  113.     Serial.println("--------------");
  114.     presentValue = false;
  115.     final = false;
  116.     num1 = "";
  117.     num2 = "";
  118.     answer = 0;
  119.     op = ' ';
  120.   }
  121. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement