#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <Keypad.h>
int layer = 11;
const int buttonPin = A1;
int buttonState = 0;
int lastButtonState = 0;
int buttonPressCount = 0;
unsigned long lastDebounceTime = 0;
unsigned long debounceDelay = 5;
const int ledPin = 12;
unsigned long previousMillis = 0;
const long interval = 100;
unsigned long startTime;
const unsigned long longPressDuration = 500;
bool tombol = true;
bool sound = true;
const unsigned long longPressTime = 2000; // waktu tekan lama dalam milidetik (2 detik)
unsigned long keyPressTime = 0; // variabel untuk menyimpan waktu tombol ditekan
bool longPressDetected = false; // flag untuk mendeteksi tekan lama
char lastKey = NO_KEY; // variabel untuk menyimpan tombol terakhir yang ditekan
LiquidCrystal_I2C lcd(0x27, 16, 2);
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);
int setcount = 0;
char inputBuffer[6] = ""; // Buffer untuk input (5 digit + null terminator)
void setup() {
lcd.init();
lcd.backlight();
lcd.setCursor(0, 0);
lcd.print("Set Amount=0");
lcd.setCursor(0, 1);
lcd.print("Amount=");
lcd.setCursor(8, 1);
lcd.print(buttonPressCount);
pinMode(buttonPin, INPUT_PULLUP);
pinMode(ledPin, OUTPUT);
Serial.begin(9600);
pinMode(11,OUTPUT);
digitalWrite(11,HIGH);
}
void loop() {
unsigned long currentMillis = millis();
int reading = digitalRead(buttonPin);
if (reading != lastButtonState) {
lastDebounceTime = millis();
}
char key = keypad.getKey();
if (tombol == true){
if (key) {
switch (key)
{
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
if (layer != 16) {
lcd.setCursor(layer, 0);
lcd.print(key);
inputBuffer[layer - 11] = key; // Menyimpan input ke buffer
layer = layer + 1;
}
break;
case 'C':
if (layer != 11) {
lcd.setCursor(layer - 1, 0);
lcd.print(" ");
layer = layer - 1;
inputBuffer[layer - 11] = '\0'; // Menghapus input dari buffer
}
break;
}
}
}
if (key != NO_KEY) {
if (key == 'D') {
if (key != lastKey) {
keyPressTime = millis();
longPressDetected = false;
}
unsigned long pressDuration = millis() - keyPressTime;
if (pressDuration >= longPressTime &&
!longPressDetected) {
longPressDetected = true;
tombol = true;
digitalWrite(11,HIGH);
}
}
} else if (lastKey == 'D') {
if (!longPressDetected) {
if (tombol == true){
digitalWrite(11,LOW);
sound =true;
digitalWrite(ledPin, LOW);
inputBuffer[5] = '\0';
setcount = atoi(inputBuffer);
tombol = false;
}
}
}
if ((millis() - lastDebounceTime) > debounceDelay) {
if (reading != buttonState) {
buttonState = reading;
if (buttonState == LOW) {
buttonPressCount++;
lcd.setCursor(8, 1);
lcd.print(buttonPressCount);
digitalWrite(ledPin, HIGH);
}
}
}
lastButtonState = reading;
if ((buttonPressCount >= setcount) && (setcount != 0) && (sound == true)){
if (key == 'B'){
sound = false;
digitalWrite(ledPin, LOW);
}
if (currentMillis - previousMillis >= interval) {
previousMillis = currentMillis;
if (digitalRead(ledPin) == LOW) {
digitalWrite(ledPin, HIGH);
} else {
digitalWrite(ledPin, LOW);
}
}
}
if (buttonPressCount < setcount){
digitalWrite(ledPin,LOW);
}
lastKey = key;
if (key == 'A'){
tombol = true;
digitalWrite(11,HIGH);
}
}