SIMULASI KALKULATOR SEDERHANA MENGGUNAKAN LCD 12C DAN KEYPAD BERBASIS ARDUINO PADA SOFTWARE PROTEUS 8

 SKEMATIK

KOMPONEN

-        Arduino UNO

-        LM016L (LCD 16x2)

-        PCF8574 (Modul I2C)

-        Keypad Smalcalc (Keypad)

Sketch Arduino

#include <LiquidCrystal_I2C.h>

LiquidCrystal_I2C lcd(0x27, 20, 2);

#include <Keypad.h>

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] = {2, 3, 4, 5};

byte colPins[COLS] = {6, 7, 8, 9};

Keypad myKeypad = Keypad(

makeKeymap(keys), rowPins, colPins,

ROWS, COLS );

float presentValue = false;

float next = false;float final = false;

String num1, num2; String answer = "0";

char op;

void setup(){

  lcd.init();

  lcd.backlight();

  lcd.setCursor(5,0);

  lcd.print("UNIT 5");

  lcd.setCursor(3,1);

  lcd.print("Calculator");

  delay(1000);

  lcd.clear();

}

void loop() {

  char key = myKeypad.getKey();

  if (key != NO_KEY && (key == '1' || key =='2' || key == '3' || key == '4' || key == '5'|| key == '6' || key == '7' || key == '8' ||key== '9' || key == '0')){

    if (presentValue != true){

      num1 = num1 + key;

      int numLength = num1.length();

      lcd.setCursor(0, 0);

      lcd.print(num1);

    }

    else{

      num2 = num2 + key;

      int numLength = num2.length();

      int numLength1 = num1.length();

      lcd.setCursor(1 + numLength1, 0);

      lcd.print(num2);

      final = true;

    }

  }

  else if (presentValue == false && key !=

  NO_KEY && (key == '/' || key == '*' || key

  == '-' || key == '+')){

    if (presentValue == false){

      int numLength = num1.length();

      presentValue = true;op = key;

      lcd.setCursor(0 + numLength, 0);

      lcd.print(op);

    }

  }

  else if (final == true && key != NO_KEY && key == '=') {

    switch (op){

      case '+':

        answer = num1.toFloat() + num2.toFloat();

        break;

      case'-' :

        answer = num1.toFloat() - num2.toFloat();

        break;

      case'*' :

      answer = num1.toFloat() * num2.toFloat();

      break;

      case'/':

      answer = num1.toFloat() / num2.toFloat();

      break;

    }

    lcd.clear();

    lcd.setCursor(16, 1);lcd.autoscroll();

    lcd.print(answer); lcd.noAutoscroll();

  }

  else if (key != NO_KEY && key == 'C'){

    lcd.clear();

    presentValue = false;final = false;

    num1 = "";

    num2 = ""; answer = "0";op = ' ';

  }

}

Komentar

Postingan populer dari blog ini

SIMULASI PARKIR MENGGUNAKAN SENSOR ULTRASONIK BERBASIS ARDUINO PADA SOFTWARE PROTEUS 8