Translate

Membuat kalkulator Digital (Digital Kalkulator) Menggunakan ARDUINO dan KEYPAD Matrix 4x4

Membuat kalkulator Digital (Digital Kalkulator) Menggunakan ARDUINO dan KEYPAD Matrix 4x4


        Pada kesempatan kali ini saya akan menjelaskan mengenai bagaimana cara membuat sebuah alat yang dapat digunakan untuk menghitung baik itu menjumlah, mengurangi dan perhitungan yang lainnya seperti perkalian dan pembagian, alat ini menggunakan mikrokontroller Arduino dan input keypad matrix 4x4, untuk lebih jelasnya berikut adalah skema dan programnya.



a. Arduino Uno




b. Keypad Matrix 4x4




c. LCD 16x2 I2C






d. Program Arduino IDE

#include "Wire.h"
#include <Keypad.h>
#include <LiquidCrystal.h>
LiquidCrystal lcd(A5, A4, A3, A2, A1, A0);

long first = 0;
long second = 0;
double total = 0;

char customKey;
const byte ROWS = 4;
const byte COLS = 4;

char keys[ROWS][COLS] = {
{'/', 'C', '-', '+'},
{'=', '9', '6', '3'},
{'0', '8', '5', '2'},
{'*', '7', '4', '1'}
};

byte rowPins[ROWS] = {2,3,4,5}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {6,7,8,9}; //connect to the column pinouts of the keypad

//initialize an instance of class NewKeypad
Keypad customKeypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS);


void setup()
{
  lcd.begin(16, 2);
  lcd.clear();
  lcd.noCursor();
  pinMode(10,OUTPUT);
  pinMode(11,OUTPUT);
  pinMode(12,OUTPUT);
  pinMode(13,OUTPUT);
 
}

void loop()
{
 
   digitalWrite(10,HIGH);
   digitalWrite(10,HIGH);
   digitalWrite(10,HIGH);
   digitalWrite(10,HIGH);
  
  customKey = customKeypad.getKey();
  switch(customKey)
  {
  case '0' ... '9': // This keeps collecting the first value until a operator is pressed "+-*/"
    lcd.setCursor(0,0);
    first = first * 10 + (customKey - '0');
    lcd.print(first);
    break;

  case '+':
    first = (total != 0 ? total : first);
    lcd.setCursor(3,0);
    lcd.print("+");
    second = SecondNumber(); // get the collected the second number
    total = first + second;
    lcd.setCursor(0,3);
    lcd.print(total);
    first = 0, second = 0; // reset values back to zero for next use
    break;

  case '-':
    first = (total != 0 ? total : first);
    lcd.setCursor(3,0);
    lcd.print("-");
    second = SecondNumber();
    total = first - second;
    lcd.setCursor(0,3);
    lcd.print(total);
    first = 0, second = 0;
    break;

  case '*':
    first = (total != 0 ? total : first);
    lcd.setCursor(3,0);
    lcd.print("*");
    second = SecondNumber();
    total = first * second;
    lcd.setCursor(0,3);
    lcd.print(total);
    first = 0, second = 0;
    break;

  case '/':
    first = (total != 0 ? total : first);
    lcd.setCursor(3,0);
    lcd.print("/");
    second = SecondNumber();
    lcd.setCursor(0,3);

    second == 0 ? lcd.print("Invalid") : total = (float)first / (float)second;

    lcd.print(total);
    first = 0, second = 0;
    break;

  case 'C':
    total = 0;
    lcd.clear();
    break;
  }
}

long SecondNumber()
{
  while( 1 )
  {
    customKey = customKeypad.getKey();
    if(customKey >= '0' && customKey <= '9')
    {
      second = second * 10 + (customKey - '0');
      lcd.setCursor(5,0);
      lcd.print(second);
    }

    if(customKey == '=') break;  //return second;
  }
 return second;
}






 e. VIDEO HASILNYA











No comments:

Post a Comment