Arduino Calculator Using 4x4 Keypad
อุปกรณ์
- Arduino Uno (Any version will work)
- 16×2 LCD Display
- 4×4 Keypad
- 9V Battery
- Breadboard and Connecting wires
Diagram
Code
/*
 * Arduino Keypad calculator Program
 */
#include<LiquidCrystal.h>//ประกาศเมื่อจะใช้จอLCD
#include <Keypad.h> //ประกาศเมื่อมีกรใช้ Keypad
const byte ROWS = 4; // ให้มี 4 แถว
const byte COLS = 4; // ให้มี 4 คอลัมม์
// กำหนดรูปแบบ Keypad
char keys[ROWS][COLS] = {
  {'7','8','9','D'},
  {'4','5','6','C'},
  {'1','2','3','B'},
  {'*','0','#','A'}
};
byte rowPins[ROWS] = { 0, 1, 2, 3 };// ต่อ Keypad ROW0, ROW1, ROW2 และ ROW3 ที่บอร์ด
byte colPins[COLS] = { 4, 5, 6, 7 }; // ต่อ Keypad COL0, COL1 และ COL2 ที่บอร์ด.
Keypad kpd = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS ); //สร้างkeypad
const int rs = 8, en = 9, d4 = 10, d5 = 11, d6 = 12, d7 = 13; //ขาแต่ละขาที่ต่อLCD
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);
 long Num1,Num2,Number;
 char key,action;
 boolean result = false;
void setup() {
  lcd.begin(16, 2); //กำหนดจอ 16*2
  lcd.print("DIY Calculator"); //แสดงขอ้ความ
  lcd.setCursor(0, 1);   // เซ็ตตำแหน่งที่ 0,1
  lcd.print("-CircuitDigest"); //แสดงข้อความ
   delay(2000); 
    lcd.clear(); //ล้างหน้าจอ
}
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(); //ล้างหน้าจอ
    if (key=='*') //Iถ้ากดปุ่ม cancel Button
    {Serial.println ("Button Cancel"); Number=Num1=Num2=0; result=false;}
     if (key == '1') //ถ้ากด Button 1
    {Serial.println ("Button 1"); 
    if (Number==0)
    Number=1;
    else
    Number = (Number*10) + 1; //ถ้ากด 2 ครั้ง
    }
     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') //ถ้าพบการกกดปุ่มของคอลัมม์ที่ 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() //classของการคำนวณทางคณิตศาสตร์
{
  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
}
Credit By : https://circuitdigest.com/microcontroller-projects/arduino-calculator-using-4x4-keypad

ความคิดเห็น
แสดงความคิดเห็น