safwan092

حاسبة البالوت بدون شاشات

Mar 18th, 2021
959
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  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.   //(key=='1'||key=='2'||key=='3'||key=='4'||key=='5'||key=='6'||key=='7'||key=='8'||key=='9'||key=='0')
  41.   //(key == 'A' || key == 'B')
  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(15 - numLength, 0); //to adjust one whitespace for operator
  50.       //lcd.print(num1);
  51.       Serial.print(num1);
  52.     }
  53.     /*else
  54.     {
  55.       num2 = num2 + key;
  56.       int numLength = num2.length();
  57.       //lcd.setCursor(15 - numLength, 1);
  58.       //lcd.print(num2);
  59.       Serial.print(num2);
  60.       final = true;
  61.     }*/
  62.   }
  63.  
  64.   else if (presentValue == false && key != NO_KEY && (key == '-' || key == '+'))
  65.   {
  66.     if (presentValue == false)
  67.     {
  68.       presentValue = true;
  69.       op = key;
  70.       //lcd.setCursor(15,0);
  71.       //lcd.print(op);
  72.       Serial.print(op);
  73.       final = true;
  74.     }
  75.   }
  76.  
  77.   else if (final == true && key != NO_KEY && (key == 'A' || key == 'B')){
  78.     if (op == '+'){
  79.       answer = num1.toInt();
  80.       if(key == 'A'){
  81.         AValue = AValue + answer;
  82.       }
  83.       else{
  84.         BValue = BValue + answer;
  85.       }
  86.     }
  87.     else if (op == '-'){
  88.       answer = num1.toInt();
  89.       if(key == 'A'){
  90.         AValue = AValue - answer;
  91.       }
  92.       else{
  93.         BValue = BValue - answer;
  94.       }
  95.     }
  96.    
  97.       //lcd.clear();
  98.       //lcd.setCursor(15,0);
  99.       //lcd.autoscroll();
  100.       //lcd.print(answer);
  101.       Serial.print(" = ");
  102.       Serial.println(answer);
  103.       Serial.print("A = ");
  104.       Serial.println(AValue);
  105.       Serial.print("B = ");
  106.       Serial.println(BValue);
  107.       Serial.println("--------------");
  108.       presentValue = false;
  109.       final = false;
  110.       num1 = "";
  111.       num2 = "";
  112.       answer = 0;
  113.       op = ' ';
  114.       //lcd.noAutoscroll();
  115.   }
  116.   else if (key != NO_KEY && key == 'C'){
  117.     //lcd.clear();
  118.     Serial.println("--------------");
  119.     presentValue = false;
  120.     final = false;
  121.     num1 = "";
  122.     num2 = "";
  123.     answer = 0;
  124.     op = ' ';
  125.   }
  126. }
RAW Paste Data