ข้ามไปที่เนื้อหาหลัก

ส่งงานครั้งที่ 11

Arduino Calculator Using 4x4 Keypad

อุปกรณ์

  1. Arduino Uno (Any version will work)
  2. 16×2 LCD Display
  3. 4×4 Keypad
  4. 9V Battery
  5. 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

ความคิดเห็น

โพสต์ยอดนิยมจากบล็อกนี้

ส่งงานครั้งที่ 17

Air Pollution Monitoring อุปกรณ์ MQ135 Gas sensor Arduino Uno Wi-Fi module ESP8266 16X2 LCD Breadboard 10K potentiometer 1K ohm resistors 220 ohm resistor Buzzer Diagram Code #include "MQ135.h" #include <SoftwareSerial.h> #define DEBUG true SoftwareSerial esp8266(9,10); // กำหนดขา 9 ของ Arduino เป็น RX pin และ ขา 10 ของ Arduino เป็น the TX pin const int sensorPin= 0; int air_quality; #include <LiquidCrystal.h> LiquidCrystal lcd(12,11, 5, 4, 3, 2); void setup() { pinMode(8, OUTPUT); lcd.begin(16,2); lcd.setCursor (0,0); lcd.print ("circuitdigest "); lcd.setCursor (0,1); lcd.print ("Sensor Warming "); delay(1000); Serial.begin(115200); esp8266.begin(115200);   sendData("AT+RST\r\n",2000,DEBUG); // reset module   sendData("AT+CWMODE=2\r\n",1000,DEBUG); // กำหนดค่า  access point   sendData("AT+CIFSR\r\n",1000,DEBUG); // รับ ip address   sendData("AT+CIPMUair_quali

ส่งงานครั้งที่ 18

12 V Battery Charger อุปกรณ์ Transformer 12V 1Amp IC LM317 (2) Diode Bridge W005 Connector Terminal Block (2) Capacitor 1000uF, 1uF Capacitor 0.1uF (5) Variable resistor 100R Resistor 1k (5) Resistor 10k Diode- Nn007  (3) LM358 – Opamp 0.05R - Shunt Resistor/wire LCD-16*2 (optional) Arduino Nano (optional) Diagram Code #include <LiquidCrystal.h>// ประกาศเมื่อใช้LCD LiquidCrystal lcd(11, 12, 10, 9, 8, 7); float voltage,current;  void setup() {   Serial.begin(9600);   //ตั้งค่าLCD   lcd.begin(16, 2);   // Print a message to the LCD.   lcd.setCursor(0, 0);   lcd.print("12V Charger");   lcd.setCursor(0, 1);   lcd.print("-Circuit Digest");   delay(2000);   lcd.clear();   lcd.setCursor(0, 0);   lcd.print("Voltage = ");   lcd.setCursor(0, 1);   lcd.print("Current = "); } void loop() {   voltage = (analogRead(A0)) * 0.0140625;   current = (analogRead(A1)) * 0.35;           lcd.setCursor(1

ส่งงานครั้งที่ 15

Arduino Solar Tracker อุปกรณ์ Servo Motor (sg90) Solar panel Arduino Uno LDR’s X 2 (Light Dependent Resistor) 10K resistors X 2 Battery (6 to 12V) Diagram Code #include <Servo.h>      //ประกาศเมื่อมีการใช้Servo Servo sg90;             //ตั้งค่าServoด้วยตัวแปรsg90 int initial_position = 90;   //ประกาศตัวแปรไว้ที่ค่า 90 int LDR1 = A0;          //ขาที่LDRต่อไว้ int LDR2 = A1;          //ขาที่LDR2ต่อไว้ int error = 5;          //ตั้งค่าเริ่มต้นของ error ที่ 5 int servopin=9; void setup() {    sg90.attach(servopin);  // ให้ servo ที่ขา 9   pinMode(LDR1, INPUT);   //ให้ขาที่ต่อกับLDRเป็นInput   pinMode(LDR2, INPUT);   sg90.write(initial_position);   //ขยับServoไว้ที่90องศา   delay(2000);            // กำหนดdelay2วินาที } void loop() {   int R1 = analogRead(LDR1); // อ่านค่า LDR 1   int R2 = analogRead(LDR2); // อ่านค่า LDR 2   int diff1= abs(R1 - R2);   // คำนวณค่าการเปลี่ยนแปลงของ LDR's   int diff2= abs(R2 - R1);   if((diff1 <=