#include <Keypad.h>
#include <LiquidCrystal_I2C.h>
#include <Servo.h>
// **Konfigurasi LCD**
LiquidCrystal_I2C lcd(0x27, 16, 2);
// **Konfigurasi Keypad**
const byte ROWS = 4;
const byte COLS = 4;
char keys[ROWS][COLS] = {
{'1', '2', '3', 'A'},
{'4', '5', '6', 'B'},
{'7', '8', '9', 'C'},
{'*', '0', '#', 'D'}
};
byte rowPins[ROWS] = {9, 8, 7, 6};
byte colPins[COLS] = {5, 4, 3, 2};
Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);
// **Konfigurasi Servo dan Buzzer**
Servo servos[5];
int servoPins[5] = {10, 11, 12, 13, 45};
const int buzzerPin = 44;
// **Harga Item**
int prices[5] = {25000, 28000, 30000, 33000, 35000};
// **Variabel Global**
int uangMasuk = 0;
int selectedItem = -1;
void setup() {
lcd.init();
lcd.backlight();
for (int i = 0; i < 5; i++) {
servos[i].attach(servoPins[i]);
servos[i].write(0);
}
pinMode(buzzerPin, OUTPUT);
Serial.begin(9600);
}
void loop() {
uangMasuk = analogRead(A0);
uangMasuk = map(uangMasuk, 0, 1023, 0, 50000);
lcd.setCursor(0, 0);
lcd.print("Uang: Rp ");
lcd.print(uangMasuk);
lcd.print(" ");
char key = keypad.getKey();
if (key) {
if (key >= '1' && key <= '5') {
selectedItem = key - '1';
lcd.setCursor(0, 1);
lcd.print("Item: ");
lcd.print(selectedItem + 1);
lcd.print(" Harga: ");
lcd.print(prices[selectedItem]);
if (uangMasuk >= prices[selectedItem] - 500 && uangMasuk <= prices[selectedItem] + 500) {
int kembalian = uangMasuk - prices[selectedItem];
delay(1000);
keluarkanItem(selectedItem);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Kembalian: Rp ");
lcd.print(kembalian);
delay(3000);
} else {
lcd.setCursor(0, 1);
lcd.print("Uang Tidak Cukup");
delay(2000);
}
resetTransaksi();
}
}
}
void keluarkanItem(int item) {
servos[item].write(90);
tone(buzzerPin, 1000, 200);
delay(1000);
servos[item].write(0);
}
void resetTransaksi() {
uangMasuk = 0;
selectedItem = -1;
}