const int buttonPin = 2; // Pin untuk tombol
const int ledPin = 13; // Pin untuk LED
int buttonState = 0; // Variabel untuk menyimpan status tombol
int lastButtonState = HIGH; // Variabel untuk menyimpan status tombol terakhir
int buttonPressCount = 0; // Variabel untuk menghitung jumlah tekanan tombol
unsigned long lastDebounceTime = 0; // Waktu terakhir kali tombol dibaca
unsigned long debounceDelay = 50; // Delay untuk debounce
unsigned long pressStartTime = 0; // Waktu ketika tombol pertama kali ditekan
const unsigned long pressWindow = 1000; // Interval waktu untuk menghitung tekanan tombol
void setup() {
pinMode(buttonPin, INPUT_PULLUP); // Set pin tombol sebagai INPUT dengan pull-up internal
pinMode(ledPin, OUTPUT); // Set pin LED sebagai OUTPUT
}
void loop() {
buttonState = digitalRead(buttonPin);
// Cek jika tombol sudah ditekan
if (buttonState == LOW && lastButtonState == HIGH) {
lastDebounceTime = millis();
pressStartTime = millis(); // Simpan waktu mulai tekanan tombol
buttonPressCount++;
}
// Cek jika tombol sudah dilepas
if (buttonState == HIGH && lastButtonState == LOW) {
if (millis() - pressStartTime <= pressWindow) {
// Tunggu sebentar sebelum pembacaan berikutnya
delay(200);
}
else {
buttonPressCount = 0; // Reset hitungan jika melebihi jendela waktu
}
}
// Cek jika waktu debounce telah berlalu
if ((millis() - lastDebounceTime) > debounceDelay) {
if (buttonPressCount > 0) {
if (millis() - pressStartTime > pressWindow) {
// Jika waktu jendela habis, lakukan tindakan sesuai hitungan
performAction(buttonPressCount);
buttonPressCount = 0; // Reset hitungan tombol setelah LED berkedip
}
}
}
lastButtonState = buttonState; // Update status tombol terakhir
}
void performAction(int count) {
if (count == 5 || count == 3) {
blinkLED(count);
}
}
void blinkLED(int times) {
for (int i = 0; i < times; i++) {
digitalWrite(ledPin, HIGH); // Hidupkan LED
delay(500); // Tunggu 500 ms
digitalWrite(ledPin, LOW); // Matikan LED
delay(500); // Tunggu 500 ms
}
}