پروژه ماشین حساب با آردوینو
پروژه ماشین حساب با آردوینو ، پروژه ی مناسبیه برای دوره کردن و آشنایی بیشتر با اتصال کی پد به آردوینو و ال سی دی کاراکتری با آردوینو .
این پروژه توی شبیه سازی فقط نیاز به یک ال سی دی و برد آردوینو و یک کی پد داره.
اما اگر قراره روی برد برد ببندین ، بهتره از یک پتانسیومتر هم برای تنظیم کنتراست ال سی دی استفاده کنین:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 |
#include <Keypad.h> #include <LiquidCrystal.h> LiquidCrystal lcd(7,8,9,10,11,12); // Allocate pins to LCD long num1,num2 ; double total; char operation,button; const byte ROWS = 4; const byte COLS = 4; char keys[ROWS][COLS] = { {'7','8','9','/'}, {'4','5','6','*'}, {'1','2','3','-'}, {'C','0','=','+'} }; byte rowPins[ROWS] = {A2,A3,A4,A5}; //connect to the row pinouts of the keypad byte colPins[COLS] = {2,3,4,5}; //connect to the column pinouts of the keypad Keypad customKeypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS); void setup() { // put your setup code here, to run once: pinMode(13, OUTPUT); digitalWrite(13, LOW); lcd.begin(16,2); // initialize the lcd } void loop() { // Loops are convenient for reading key press from keypad while(1) // First loop. Here we read keypad and compose our first number. It does so untill we press operation button and break's out of loop or 'C' and it starts from beginning of this loop { button = customKeypad.getKey(); // Button read if (button=='C') // If user wants to resset while he is writing first number { num1=0; num2=0; total=0; operation=0; lcd.clear(); } if (button >='0' && button <='9') // If user pressed numeric value, 1 character at a time. { num1 = num1*10 + (button -'0'); // Our numeric values are 0-9 witch means they are in first decade, when we multiply with 10 we basicaly add zero after number, // than we add a new number entered to that zero spot. As for (button -'0') this is simple ASCII table "trick" 0...9 in ASCII table are 48 ... 57, // so by subtracting '0' from any of them we get their value in decade system ex. char '5' = 53 in decade numeric system minus 48 for zero char gives us value of actual 5, // if our previous number was ex. 25 we get 250 by multiplying it with 10 and then we add 5 and we get 255 witch gets printed on LCD. lcd.setCursor(0,0); // Select first row on lcd lcd.print(num1); // Print current number1 } if (num1 !=0 && (button=='-' || button=='+' || button=='*' || button=='/')) // If user is done inputing numbers { operation = button; // operation remembers what mathematical operation user wants on numbers lcd.setCursor(0,1); // set cursor to row 2 lcd.print(operation); // print our operator break; } } while(1) // Second while loop, it loops untill user has pressed '=' or 'C'. so it either prints total or ressets program { if (button =='C'){break;} // This covers case where user pressed operator and still wants to reset button = customKeypad.getKey(); if (button=='C') // Making sure user wants to reset at anytime { num1=0; num2=0; total=0; operation=0; lcd.clear(); break; } if (button >='0' && button <='9') // Getting chars from keypad for number 2 { num2 = num2*10 + (button -'0'); lcd.setCursor(1,1); lcd.print(num2); } if (button == '=' && num2 !=0)// If button pressed was '=' its the end of the road. Calls domath() subroutine does calculation and print our results { domath(); break; } } while(1) { // After all is done this loop waits for 'C' key to be pressed so it can reset program and start over. if (button =='C'){break;} // This line is side effect of previous loop since if user pressed 'C' it breaks out of previous loop and continues here.So we need to break this one aswell or user would need to press 'C' 2 times button = customKeypad.getKey(); if (button =='C') { lcd.clear(); lcd.setCursor(0,0); num1=0; num2=0; total=0; operation=0; break; } } } void domath() // Simple switch case to pick what operation to do, based on button pressed by user. { switch(operation) { case '+': // Addition total = num1+num2; break; case '-': // Subtraction total = num1-num2; break; case '/': // Division. Could add error for division by zero, or change line in second loop where it waits for '=' char to if (button == '=' && num2 != 0) this will halt program untill num2 is not zero and then continue total = (float)num1/(float)num2; break; case '*': // Multiplication total = num1*num2; break; } // Based on case selected print our total and lcd.setCursor(0,1); lcd.print('='); lcd.setCursor(1,1); lcd.print(total); } |
توی این کد 3 تا حلقه ی while(1) وجود داره .
while اول :
توی این حلقه ، برنامه ورودی اول رو میگیره و زمانی که operation یا همون عملیات ریاضی ضرب ، جمع ، تقسیم یا منهی هم وارد بشه، اگر کلید C هم زده بشه ایم حلقه دوباره از اول اجرا میشه.
زمانی که ما میخوایم یک عدد 2 رقمی یا چند رقمی رو وارد کنیم مثل 19 باید میکروکنترلر هم برای این عدد یکان و دهگان تعریف کنه و اون هارو مجزا از هم ندونه.کد :
1 |
num1 = num1*10 + (button -'0'); |
اگه مقدار اولیه نباشه مقدار رو میریزه توی متغیر NUM1 و اگه عددی در متغیر باشه اون رو ضربدر ده میکنه تا تبدیل به دهگان بشه و در آخر عدد جدید رو بعنوان یکان ذخیره میکنه.
while دوم :
توی این حلقه اگر کاربر کلید c رو بزنه برنامه break میشه و از اول شروع میشه.
و اگر عدد دومی زده بشه مثل الگوریتم حلقه اول عمل میکنه با این تفاوت که این بار توی متغیر num2 مقدار رو میریزه .
و در این حلقه اگر کلید مساوی زده بشه برنامه و مقدار num2 غیر صفر باشه به زیر برنامه ی domath میره.
while سوم:
این حلقه برای زمانیه که محاسبه انجام شده و مقدار خروجی نمایش داده شده و اگر کاربر c رو بزنه تمامی متغیر هارو صفر میکنه.
1 2 3 4 5 6 |
lcd.clear(); lcd.setCursor(0,0); num1=0; num2=0; total=0; operation=0; |
در زیر برنامه ی domath هم با کمک یک دستور swith case عملیات ها انجام میپزیره:
مثلا توی بخش اول کد بصورت زیره :
1 2 3 4 5 |
switch(operation) { case '+': // Addition total = num1+num2; break; |
این بدین معنیه که اگه متغیره operation مقدار ورودی + رو داشت مقدار های num1 و num2 رو با هم جمع میکنه و در متغیر Total ذخیره میکنه و در آخر با دستور break از حلقه ی switch خارج میشه. و اون رو روی lcd نمایش میده.
و این عمل به ازای ورودی های دیگه ی ضرب و تقسیم و منهی هم صورت میگیره.
در آخر توجه داشته باشین در کد زیر(خط 116 برنامه) :
1 |
total = (float)num1/(float)num2; |
چون در تقسیم ممکنه خروجی اعشاری بشه از قالب بندی نوع یا Type casting استفاده شده.
پروژه ی ماشین حساب تا 2 رقم اعشار :
دانلود شبیه سازی پروژه ماشین حساب با آردوینو به همراه کد: