/**
Arduino Pump Control 2025
Copyright (C) 2025
Released under the MIT License.
*/
#include <LiquidCrystal.h>
#include <Keypad.h>
#include <Servo.h>
#include "SafeState.h"
#include "icons.h"
#define Pump_Relay 13 // Relay Pompa
/* Locking mechanism definitions */
#define PushButton_Pin 0 // Pin Input Start / Stop Pompa
#define SERVO_PIN 6
#define SERVO_LOCK_POS 20
#define SERVO_UNLOCK_POS 90
Servo lockServo; // XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
/* Display */
LiquidCrystal lcd(12, 11, 10, 9, 8, 7);
/* Keypad Setup */
const byte KEYPAD_ROWS = 4;
const byte KEYPAD_COLS = 4;
// Keypad Wiring
byte rowPins[KEYPAD_ROWS] = {5, 4, 3, 2};
byte colPins[KEYPAD_COLS] = {A3, A2, A1, A0};
// Keypad definition
char keys[KEYPAD_ROWS][KEYPAD_COLS] = {
{'1', '2', '3', 'A'},
{'4', '5', '6', 'B'},
{'7', '8', '9', 'C'},
{'*', '0', '#', 'D'}
};
Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, KEYPAD_ROWS, KEYPAD_COLS);
/* SafeState stores the secret code in EEPROM */
SafeState safeState;
void lock() {
lockServo.write(SERVO_LOCK_POS);
safeState.lock();
}
void unlock() {
lockServo.write(SERVO_UNLOCK_POS);
}
void showStartupMessage() {
lcd.setCursor(0, 0);
lcd.print("Selamat Datang..");
delay(1000);
lcd.setCursor(0, 2);
String message = "Pengendali Pompa";
for (byte i = 0; i < message.length(); i++) {
lcd.print(message[i]);
delay(50);
}
delay(1000);
}
String inputSecretCode() {
lcd.setCursor(2, 1);
lcd.print("[____] detik");
lcd.setCursor(3, 1);
String result = "";
while (result.length() < 4) {
char key = keypad.getKey();
if (key >= '0' && key <= '9') {
lcd.print(key);
result += key;
}
}
return result;
}
void showWaitScreen(int delayMillis) {
lcd.setCursor(2, 1);
lcd.print("[..........]");
lcd.setCursor(3, 1);
for (byte i = 0; i < 10; i++) {
delay(delayMillis);
lcd.print("=");
}
}
bool setNewCode() {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Enter new time:");
String newCode = inputSecretCode();
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Confirm new time");
String confirmCode = inputSecretCode();
if (newCode.equals(confirmCode)) {
safeState.setCode(newCode);
return true;
} else {
lcd.clear();
lcd.setCursor(1, 0);
lcd.print("Code mismatch");
lcd.setCursor(0, 1);
lcd.print("Safe not locked!");
delay(2000);
return false;
}
}
void showUnlockMessage() {
lcd.clear();
lcd.setCursor(0, 0);
//lcd.write(ICON_UNLOCKED_CHAR);
lcd.setCursor(4, 0);
lcd.print("Terbuka!");
lcd.setCursor(15, 0);
//lcd.write(ICON_UNLOCKED_CHAR);
delay(1000);
}
void safeUnlockedLogic() {
lcd.clear();
lcd.setCursor(0, 0);
//lcd.write(ICON_UNLOCKED_CHAR);
lcd.setCursor(0, 0);
lcd.print("# = mengunci");
lcd.setCursor(15, 0);
//lcd.write(ICON_UNLOCKED_CHAR);
bool newCodeNeeded = true;
if (safeState.hasCode()) {
lcd.setCursor(0, 1);
lcd.print(" A = kode baru");
newCodeNeeded = false;
}
auto key = keypad.getKey();
while (key != 'A' && key != '#') {
key = keypad.getKey();
}
bool readyToLock = true;
if (key == 'A' || newCodeNeeded) {
readyToLock = setNewCode();
}
if (readyToLock) {
lcd.clear();
lcd.setCursor(5, 0);
//lcd.write(ICON_UNLOCKED_CHAR);
//lcd.print(" ");
lcd.write(ICON_RIGHT_ARROW);
//lcd.print(" ");
//lcd.write(ICON_LOCKED_CHAR);
safeState.lock();
lock(); // Servo bergerak mengunci
digitalWrite(Pump_Relay, HIGH); // Pompa ON
showWaitScreen(100);
}
}
void safeLockedLogic() {
lcd.clear();
lcd.setCursor(0, 0);
//lcd.write(ICON_LOCKED_CHAR);
lcd.print("Kode membuka..");
//lcd.write(ICON_LOCKED_CHAR);
String userCode = inputSecretCode();
bool unlockedSuccessfully = safeState.unlock(userCode);
showWaitScreen(200);
if (unlockedSuccessfully) {
showUnlockMessage();
unlock(); // Servo bergerak membuka
digitalWrite(Pump_Relay, LOW); // Pompa OFF
} else {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Access Denied!");
showWaitScreen(1000);
}
}
// ---------------------------------------------------------------
int counter;
int i;
//const byte LED_PIN = 13;
//const byte BUTTON_PIN = 2;
const byte NUMBER_OF_BLINKS = 10;
const unsigned long DEBOUNCE_TIME = 20UL;
const unsigned long BLINK_DURATION = 500UL;
void setup() {
pinMode(PushButton_Pin, INPUT_PULLUP); // BUTTON_PIN
pinMode(Pump_Relay, OUTPUT); // LED_PIN
lcd.begin(16, 2);
init_icons(lcd);
showStartupMessage();
//===================================================================
// lockServo.attach(SERVO_PIN);
/* Make sure the physical lock is sync with the EEPROM state */
// Serial.begin(115200);
// if (safeState.locked()) {
// lock();
// } else {
// unlock();
// }
//=====================================================================
}
void loop() {
i = 0;
lcd.clear();
lcd.setCursor(0, 0);
static boolean blinking = false;
static byte blinkCount = 0;
static boolean ledState = false;
static unsigned long lastBlinkTime = 0;
static unsigned long debounceTimer = 0;
static int lastButtonState = digitalRead(PushButton_Pin);
int buttonState = digitalRead(PushButton_Pin);
unsigned long currentMillis = millis();
if (blinking)
{
if (currentMillis - lastBlinkTime >= BLINK_DURATION)
{
ledState = !ledState;
digitalWrite(Pump_Relay, ledState ? HIGH : LOW);
lcd.setCursor(1, 1);
lcd.print(blinkCount); delay (500);
lastBlinkTime = currentMillis;
blinkCount++;
}
if (blinkCount == NUMBER_OF_BLINKS)
{
blinking = false;
blinkCount = 0;
// be sure the led is off
digitalWrite(Pump_Relay, LOW);
ledState = false;
}
}
else {
if (lastButtonState != buttonState)
{
lastButtonState = buttonState;
debounceTimer = currentMillis;
}
if (debounceTimer > 0 && currentMillis - debounceTimer >= DEBOUNCE_TIME)
{
debounceTimer = 0;
blinking = true;
}
}
// ===========================================================================
// if (safeState.locked()) {
// safeLockedLogic();
// } else {
// safeUnlockedLogic();
// }
}