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

0


بسم الله الرحمن الرحيم

 سنقوم اليوم بتصميم احد المشاريع الجميلة  فسنقوم اليوم بتصميم آله حاسبة بإستخدام الأردوينو 

ستقوم هذه الآله بالعمليات الأساسية فقط ( الجمع - الطرح - الضرب - القسمة )

سيكون هذا المشروع يتعامل مع رقمان فقط لكن بتواجد الفكرة يمكن اضافة العديد من الخانات الأخرى



اولا الأدوات 

keypad

arduino uno

lcd

wires

keypad

سيتم استخدام هذه اللوحة فى إدخال الأرقام والعمليات الحسابية


Arduino
المتحكم الذى سيتم استخدامه فى إجراء العمليات الحسابية 



lcd

سنستخدمها لعرض البيانات عليها



ثانيًا التوصيلات :



تحتوى key pad على 8 منافذ
4 منافذ للصفوف و 4 منافذ للأعمده 
تم التوصيل كالأتى
row1 = 9
row2 = 8
row3 = 7
row4 = 6
col1 = 5
col2 = 4
col3 = 3
col4 = 2
تحتوى شاشة lcd على 12 منفذ سيتم استخدامهم فى هذا المشروع
gnd : gnd
vcc : 5v
vo : gnd
rs : A0
rw : gnd
E : A1 
d4 : A2
d5 : A3
d6 : A4
d7 : A5
LEDA : 5v
LEDC : gnd throug resistor 330 ohm
يمكن التعامل مع المنافذ التماثلية كأنها منافذ رقيمة

ثالثا الكود :
#include <LiquidCrystal.h> //Header file for LCD
#include <Keypad.h> //Header file for Keypad from https://github.com/Chris--A/Keypad

const byte ROWS = 4; // Four rows
const byte COLS = 4; // Three columns

// Define the Keymap
char keys[ROWS][COLS] = {

  {'1','2','3','A'},

  {'4','5','6','B'},

  {'7','8','9','C'},

  {'*','0','#','D'}

};

byte rowPins[ROWS] = { 9, 8, 7, 6 };// Connect keypad ROW0, ROW1, ROW2 and ROW3 to these Arduino pins.
byte colPins[COLS] = { 5, 4, 3, 2 }; // Connect keypad COL0, COL1 and COL2 to these Arduino pins.

Keypad kpd = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS ); //  Create the Keypad

const int rs = A0, en = A1, d4 = A2, d5 = A3, d6 = A4, d7 = A5; //Pins to which LCD is connected
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);

 long Num1,Num2,Number;
 char key,action;
 boolean result = false;
 
void setup() {
  lcd.begin(16, 2); //We are using a 16*2 LCD display
  lcd.print("DIY Calculator"); //Display a intro message
  lcd.setCursor(0, 1);   // set the cursor to column 0, line 1
   delay(2000); //Wait for display to show info
    lcd.clear(); //Then clean it
}

void loop() {
  
key = kpd.getKey(); //storing pressed key value in a char

if (key!=NO_KEY)
DetectButtons();

if (result==true)
CalculateResult();

DisplayResult();   
}

void DetectButtons()
     lcd.clear(); //Then clean it
    if (key=='*') //If cancel Button is pressed
    {Serial.println ("Button Cancel"); Number=Num1=Num2=0; result=false;}
    
     if (key == '1') //If Button 1 is pressed
    {Serial.println ("Button 1"); 
    if (Number==0)
    Number=1;
    else
    Number = (Number*10) + 1; //Pressed twice
    }
    
     if (key == '4') //If Button 4 is pressed
    {Serial.println ("Button 4"); 
    if (Number==0)
    Number=4;
    else
    Number = (Number*10) + 4; //Pressed twice
    }
    
     if (key == '7') //If Button 7 is pressed
    {Serial.println ("Button 7");
    if (Number==0)
    Number=7;
    else
    Number = (Number*10) + 7; //Pressed twice
    } 
  

    if (key == '0')
    {Serial.println ("Button 0"); //Button 0 is Pressed
    if (Number==0)
    Number=0;
    else
    Number = (Number*10) + 0; //Pressed twice
    }
    
     if (key == '2') //Button 2 is Pressed
    {Serial.println ("Button 2"); 
     if (Number==0)
    Number=2;
    else
    Number = (Number*10) + 2; //Pressed twice
    }
    
     if (key == '5')
    {Serial.println ("Button 5"); 
     if (Number==0)
    Number=5;
    else
    Number = (Number*10) + 5; //Pressed twice
    }
    
     if (key == '8')
    {Serial.println ("Button 8"); 
     if (Number==0)
    Number=8;
    else
    Number = (Number*10) + 8; //Pressed twice
    }   
  

    if (key == '#')
    {Serial.println ("Button Equal"); 
    Num2=Number;
    result = true;
    }
    
     if (key == '3')
    {Serial.println ("Button 3"); 
     if (Number==0)
    Number=3;
    else
    Number = (Number*10) + 3; //Pressed twice
    }
    
     if (key == '6')
    {Serial.println ("Button 6"); 
    if (Number==0)
    Number=6;
    else
    Number = (Number*10) + 6; //Pressed twice
    }
    
     if (key == '9')
    {Serial.println ("Button 9");
    if (Number==0)
    Number=9;
    else
    Number = (Number*10) + 9; //Pressed twice
    }  

      if (key == 'A' || key == 'B' || key == 'C' || key == 'D') //Detecting Buttons on Column 4
  {
    Num1 = Number;    
    Number =0;
    if (key == 'A')
    {Serial.println ("Addition"); action = '+';}
     if (key == 'B')
    {Serial.println ("Subtraction"); action = '-'; }
     if (key == 'C')
    {Serial.println ("Multiplication"); action = '*';}
     if (key == 'D')
    {Serial.println ("Devesion"); action = '/';}  

    delay(100);
  }
  
}

void CalculateResult()
{
  if (action=='+')
    Number = Num1+Num2;

  if (action=='-')
    Number = Num1-Num2;

  if (action=='*')
    Number = Num1*Num2;

  if (action=='/')
    Number = Num1/Num2; 
}

void DisplayResult()
{
  lcd.setCursor(0, 0);   // set the cursor to column 0, line 1
  lcd.print(Num1); lcd.print(action); lcd.print(Num2); 
  
  if (result==true)
  {lcd.print(" ="); lcd.print(Number);} //Display the result
  
  lcd.setCursor(0, 1);   // set the cursor to column 0, line 1
  lcd.print(Number); //Display the result
}

يتمحور الكود فى الأعلى على تحديد الأرقام التى سيتم اجراء عليها العملية 
والقيام بتحديد العملية المراد القيام بها 
ثم طباعة الناتج النهائى على الشاشة

A : +
B : -
C : *
D : /
# : =
* : DELETE ALL NUMBERS
هذه هى الرموز واستخداماتها 
اى استفسار فى التعليقات 

لتحميل الكود اضغط هنا
تصميم وبرمجة : بلال حسان سعدى

لا يوجد تعليقات

أضف تعليق