#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 16, 2);
const int tombolPlusPin = 2;
const int tombolMinusPin = 3;
const int tombolEnterPin = 4;
int counter = 0;
unsigned long lastDebounceTime = 0;
const unsigned long debounceDelay = 50;
bool lastPlusState = HIGH;
bool lastMinusState = HIGH;
bool lastEnterState = HIGH;
bool showSpelling = false;
unsigned long spellingStartTime = 0;
const unsigned long spellingDuration = 2000;
String ejaAngka(int n)
{
if (n == 0) return "nol";
String satuan[] = {"", "satu", "dua", "tiga", "empat", "lima", "enam", "tujuh", "delapan", "sembilan"};
String belasan[] = {"sepuluh", "sebelas", "dua belas", "tiga belas", "empat belas", "lima belas", "enam belas", "tujuh belas", "delapan belas", "sembilan belas"};
String puluhan[] = {"", "", "dua puluh", "tiga puluh", "empat puluh", "lima puluh", "enam puluh", "tujuh puluh", "delapan puluh", "sembilan puluh"};
String hasil = "";
if (n >= 100)
{
int ratus = n / 100;
if (ratus == 1) hasil += " ratus";
else hasil += satuan[ratus] + " ratus";
n %= 100;
if (n > 0) hasil += " ";
}
if (n >= 20)
{
int puluh = n / 10;
hasil += puluhan[puluh];
n %= 10;
if (n > 0) hasil += " " + satuan[n];
}
else if (n >= 12)
{
hasil += belasan[n - 10];
}
else if (n == 11)
{
hasil += "sebelas";
}
else if (n == 10)
{
hasil += "sepuluh";
}
else if (n > 0 && n < 10)
{
hasil += satuan[n];
}
return hasil;
}
void setup()
{
lcd.init();
lcd.backlight();
lcd.clear();
pinMode(tombolPlusPin, INPUT_PULLUP);
pinMode(tombolMinusPin, INPUT_PULLUP);
pinMode(tombolEnterPin, INPUT_PULLUP);
updateDisplay();
}
void loop()
{
unsigned long currentTime = millis();
bool plusPressed = (digitalRead(tombolPlusPin) == LOW);
bool minusPressed = (digitalRead(tombolMinusPin) == LOW);
bool enterPressed = (digitalRead(tombolEnterPin) == LOW);
if (plusPressed && !lastPlusState && (currentTime - lastDebounceTime > debounceDelay))
{
lastDebounceTime = currentTime;
counter++;
showSpelling = false;
updateDisplay();
}
if (minusPressed && !lastMinusState && (currentTime - lastDebounceTime > debounceDelay))
{
lastDebounceTime = currentTime;
counter--;
showSpelling = false;
updateDisplay();
}
if (enterPressed && !lastEnterState && (currentTime - lastDebounceTime > debounceDelay))
{
lastDebounceTime = currentTime;
showSpelling = true;
spellingStartTime = currentTime;
updateDisplay();
}
lastPlusState = plusPressed;
lastMinusState = minusPressed;
lastEnterState = enterPressed;
if (showSpelling && (currentTime - spellingStartTime >= spellingDuration))
{
showSpelling = false;
updateDisplay();
}
delay(10);
}
void updateDisplay()
{
lcd.clear();
if (showSpelling)
{
String ejaan = ejaAngka(counter);
if (ejaan.length() <= 16)
{
lcd.setCursor(0,0);
lcd.print(ejaan);
}
else
{
lcd.setCursor(0,0);
lcd.print(ejaan.substring(0,16));
lcd.setCursor(0,1);
lcd.print(ejaan.substring(16,32));
}
}
else
{
lcd.setCursor(0,0);
lcd.print("Counter: ");
lcd.print(counter);
lcd.setCursor(0,1);
lcd.print("Enter untuk eja");
}
}