ماهر

نبذة:

ول المحتويات مقدمــــــــــة عن المشروع متطلبات المشروع المنتجات الخاصة بالمشروع طريقة العمل والتوصيل الكــــــــــود البرمجي

عمل ألة حاسبة باستخدام الاردوينو

F84NB1SJUQSIXYA.LARGEالكــــــــــود البرمجي

  1. #include //Header file for LCD from
  2. #include //Header file for Keypad
  3. const byte ROWS = 4; // Four rows
  4. const byte COLS = 4; // Three columns
  5. // Define the Keymap
  6. char keys[ROWS][COLS] = {
  7. {‘7’,‘8’,‘9’,‘D’},
  8. {‘4’,‘5’,‘6’,‘C’},
  9. {‘1’,‘2’,‘3’,‘B’},
  10. {‘*’,‘0’,‘#’,‘A’}
  11. };
  12. byte rowPins[ROWS] = { 0, 1, 2, 3 };// Connect keypad ROW0, ROW1, ROW2 and ROW3 to these Arduino pins.
  13. byte colPins[COLS] = { 4, 5, 6, 7 }; // Connect keypad COL0, COL1 and COL2 to these Arduino pins.
  14. Keypad kpd = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS ); // Create the Keypad
  15. const int rs = 8, en = 9, d4 = 10, d5 = 11, d6 = 12, d7 = 13; //Pins to which LCD is connected
  16. LiquidCrystal lcd(rs, en, d4, d5, d6, d7);
  17. long Num1,Num2,Number;
  18. char key,action;
  19. boolean result = false;
  20. void setup() {
  21. lcd.begin(16, 2); //We are using a 16*2 LCD display
  22. lcd.print(“DIY Calculator”); //Display a intro message
  23. lcd.setCursor(0, 1); // set the cursor to column 0, line 1
  24. lcd.print(“-CircuitDigest”); //Display a intro message
  25. delay(2000); //Wait for display to show info
  26. lcd.clear(); //Then clean it
  27. }
  28. void loop() {
  29. key = kpd.getKey(); //storing pressed key value in a char
  30. if (key!=NO_KEY)
  31. DetectButtons();
  32. if (result==true)
  33. CalculateResult();
  34. DisplayResult();
  35. }
  36. void DetectButtons()
  37. {
  38. lcd.clear(); //Then clean it
  39. if (key==‘*’) //If cancel Button is pressed
  40. {Serial.println (“Button Cancel”); Number=Num1=Num2=0; result=false;}
  41. if (key == ‘1’) //If Button 1 is pressed
  42. {Serial.println (“Button 1”);
  43. if (Number==0)
  44. Number=1;
  45. else
  46. Number = (Number*10) + 1; //Pressed twice
  47. }
  48. if (key == ‘4’) //If Button 4 is pressed
  49. {Serial.println (“Button 4”);
  50. if (Number==0)
  51. Number=4;
  52. else
  53. Number = (Number*10) + 4; //Pressed twice
  54. }
  55. if (key == ‘7’) //If Button 7 is pressed
  56. {Serial.println (“Button 7”);
  57. if (Number==0)
  58. Number=7;
  59. else
  60. Number = (Number*10) + 7; //Pressed twice
  61. }
  62. if (key == ‘0’)
  63. {Serial.println (“Button 0”); //Button 0 is Pressed
  64. if (Number==0)
  65. Number=0;
  66. else
  67. Number = (Number*10) + 0; //Pressed twice
  68. }
  69. if (key == ‘2’) //Button 2 is Pressed
  70. {Serial.println (“Button 2”);
  71. if (Number==0)
  72. Number=2;
  73. else
  74. Number = (Number*10) + 2; //Pressed twice
  75. }
  76. if (key == ‘5’)
  77. {Serial.println (“Button 5”);
  78. if (Number==0)
  79. Number=5;
  80. else
  81. Number = (Number*10) + 5; //Pressed twice
  82. }
  83. if (key == ‘8’)
  84. {Serial.println (“Button 8”);
  85. if (Number==0)
  86. Number=8;
  87. else
  88. Number = (Number*10) + 8; //Pressed twice
  89. }
  90. if (key == ‘#’)
  91. {Serial.println (“Button Equal”);
  92. Num2=Number;
  93. result = true;
  94. }
  95. if (key == ‘3’)
  96. {Serial.println (“Button 3”);
  97. if (Number==0)
  98. Number=3;
  99. else
  100. Number = (Number*10) + 3; //Pressed twice
  101. }
  102. if (key == ‘6’)
  103. {Serial.println (“Button 6”);
  104. if (Number==0)
  105. Number=6;
  106. else
  107. Number = (Number*10) + 6; //Pressed twice
  108. }
  109. if (key == ‘9’)
  110. {Serial.println (“Button 9”);
  111. if (Number==0)
  112. Number=9;
  113. else
  114. Number = (Number*10) + 9; //Pressed twice
  115. }
  116. if (key == ‘A’ || key == ‘B’ || key == ‘C’ || key == ‘D’) //Detecting Buttons on Column 4
  117. {
  118. Num1 = Number;
  119. Number =0;
  120. if (key == ‘A’)
  121. {Serial.println (“Addition”); action = ‘+’;}
  122. if (key == ‘B’)
  123. {Serial.println (“Subtraction”); action = ‘-‘; }
  124. if (key == ‘C’)
  125. {Serial.println (“Multiplication”); action = ‘*’;}
  126. if (key == ‘D’)
  127. {Serial.println (“Devesion”); action = ‘/’;}
  128. delay(100);
  129. }
  130. }
  131. void CalculateResult()
  132. {
  133. if (action==‘+’)
  134. Number = Num1+Num2;
  135. if (action==‘-‘)
  136. Number = Num1-Num2;
  137. if (action==‘*’)
  138. Number = Num1*Num2;
  139. if (action==‘/’)
  140. Number = Num1/Num2;
  141. }
  142. void DisplayResult()
  143. {
  144. lcd.setCursor(0, 0); // set the cursor to column 0, line 1
  145. lcd.print(Num1); lcd.print(action); lcd.print(Num2);
  146. if (result==true)
  147. {lcd.print(” =”); lcd.print(Number);} //Display the result
  148. lcd.setCursor(0, 1); // set the cursor to column 0, line 1
  149. lcd.print(Number); //Display the result
  150. }

المشاركات (2)

أضف مشاركة