#include <LiquidCrystal_I2C.h>
#include <Keypad.h>
#include <Servo.h>
// Set the LCD address
LiquidCrystal_I2C lcd(0x27, 16, 2); // Adjust if necessary
// Define the keypad layout
const byte ROWS = 4; // four rows
const byte COLS = 4; // four columns
char* keys[ROWS][COLS] = {
{'1','4','7','*'},
{'2','5','8','0'},
{'3','6','9','#'},
{'A','B','C','D'}
};
byte rowPins[ROWS] = {5, 4, 3, 2};
byte colPins[COLS] = {9, 8, 7, 6};
Keypad customKeypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );
// Variables to store numbers and operator
char* inputString = " ";
float num1 = 0, num2 = 0;
char operation;
int LED1 = 11;
const int trig = 9;
const int echo = 10;
//const servo = 12;
Servo motor;
void setup() {
pinMode(LED1, OUTPUT);
Serial.begin(9600);
lcd.init(); // initialize the lcd
lcd.backlight();
lcd.clear();
pinMode(trig, OUTPUT);
pinMode(echo, INPUT);
motor.attach(12);
lcd.backlight();
lcd.setCursor(1, 0);
lcd.println("Masukan Volume");
lcd.setCursor(6, 1);
lcd.println("(ml)");
}
void loop() {
digitalWrite(trig, LOW);
delayMicroseconds(2);
digitalWrite(trig, HIGH);
delayMicroseconds(10);
digitalWrite(trig, LOW);
// Baca durasi pulsa echo
long durasi = pulseIn(echo, HIGH);
// Hitung jarak
int jarak = durasi*0.034/2;
char key = customKeypad.getKey();
if (key) {
lcd.clear();
inputString += key;
if (key >= '0' && key <= '9') {
lcd.print(inputString);
lcd.setCursor(0,0);
}
else if (key == 'C'){
lcd.clear();
inputString = "";
lcd.print(inputString);
lcd.setCursor(0,0);
}
/*else if (key == 'A'){
int myint1 = atoi(inputString);
durasi = myint1 * (1000) / 17975;
//servo.write(90);
delay(durasi);
digitalWrite(LED1, HIGH);
motor.write(0);
}*/
// Check if the key is a number
/*if (key >= '0' && key <= '9') {
if (operation == '\0') {
num1 = num1 * 10 + (key - '0');
} else {
num2 = num2 * 10 + (key - '0');
}
}
// Check if the key is an operator
else if (key == 'A' || key == 'B' || key == 'C' || key == 'D') {
operation = key;
}
// Check if the equal sign is pressed
else if (key == '#') {
switch (operation) {
case '+':
num1 += num2;
break;
case '-':
num1 -= num2;
break;
case '*':
num1 *= num2;
break;
case '/':
if (num2 == 0) {
lcd.print("Error");
} else {
num1 /= num2;
}
break;
}
lcd.setCursor(0,1);
lcd.print(num1);
num1 = 0;
num2 = 0;
operation = '\0';
}
// Clear the screen for 'C'
else if (key == 'C') {
lcd.clear();
inputString = " ";
num1 = 0;
num2 = 0;
operation = '\0';
}*/
}
/*if (inputString) {
if (key == 'A'){
int myint1 = atoi(inputString);
durasi = myint1 * (1000) / 17975;
motor.write(90);
delay(durasi);
motor.write(0);
}
}*/
}