From bfb2c68a45f88b90ab6ac750517aa127bd822920 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mat=C4=9Bj=20Kub=C3=AD=C4=8Dek?= Date: Mon, 8 Jun 2026 10:01:20 +0200 Subject: [PATCH] =?UTF-8?q?P=C5=99id=C3=A1n=C3=AD=20zad=C3=A1v=C3=A1n?= =?UTF-8?q?=C3=AD=20hesla.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/src/main.cpp | 133 ++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 109 insertions(+), 24 deletions(-) diff --git a/src/src/main.cpp b/src/src/main.cpp index 3f8edd6..b3f4b6a 100644 --- a/src/src/main.cpp +++ b/src/src/main.cpp @@ -23,17 +23,27 @@ LiquidCrystal_I2C lcd(0x27, 16, 2); // proměnné // příklad je zadávané heslo, stavy, apod. -String password = ""; +int password = ""; // 0-9999 String savedPassword = ""; -// chování zámku -void OpenLock() { - servo.write(OPEN_POS); +// enkodér +int lastCLK = HIGH; + +// maska – po kolika ms nečinnosti se heslo maskuje +constexpr unsigned long MASK_DELAY_MS = 2000; +unsigned long lastEncoderMove = 0; +bool masked = false; + +// přeformátování kódu na 4 cifry +String formatCode(int value) { + String s = String(value); + while (s.length() < 4) s = "0" + s; + return s; } -void CloseLock() { - servo.write(CLOSED_POS); -} +// chování zámku +void OpenLock() { servo.write(OPEN_POS); } +void CloseLock() { servo.write(CLOSED_POS); } // chování bzučáku void OpenSound() { @@ -53,6 +63,14 @@ void CloseSound() { noTone(BUZZER_PIN); } +void ConfirmSound() { + tone(BUZZER_PIN, 440, 100); + delay(130); + tone(BUZZER_PIN, 880, 200); + delay(230); + noTone(BUZZER_PIN); +} + // chování RGB LED void OpenLED() { analogWrite(RGB_GREEN_PIN, 120); @@ -74,10 +92,8 @@ void NeutralLED() { } void ButtonPressed() { - if (savedPassword == "") { - savedPassword = password; - } - else { + if (savedPassword != "") + { if (password == savedPassword) { OpenLock(); OpenSound(); @@ -93,6 +109,77 @@ void ButtonPressed() { } } +// setup hesla +void showSetupScreen(bool showValue) { + lcd.setCursor(0, 0); + lcd.printstr("Nastavte heslo: "); + lcd.setCursor(0, 1); + if (showValue) { + String code = formatCode(passwordValue); + // mezery okolo pro vizuální oddělení číslic + String display = code[0] + String(" ") + code[1] + String(" ") + code[2] + String(" ") + code[3] + String(" "); + lcd.printstr(display.c_str()); + } else { + lcd.printstr("* * * * "); + } +} + +// setup fáze – blokující, dokud není heslo uloženo +void runPasswordSetup() { + NeutralLED(); + passwordValue = 0; + masked = false; + lastEncoderMove = millis(); + lastCLK = digitalRead(ENCODER_CLK_PIN); + + showSetupScreen(true); + + while (savedPassword == "") { + // čtení enkodéru + int currentCLK = digitalRead(ENCODER_CLK_PIN); + if (currentCLK != lastCLK && currentCLK == LOW) { + int dt = digitalRead(ENCODER_DT_PIN); + if (dt != currentCLK) { + passwordValue = (passwordValue + 1) % 10000; + } else { + passwordValue = (passwordValue - 1 + 10000) % 10000; + } + masked = false; + lastEncoderMove = millis(); + showSetupScreen(true); + } + lastCLK = currentCLK; + + // maskování po nečinnosti + if (!masked && millis() - lastEncoderMove >= MASK_DELAY_MS) { + masked = true; + showSetupScreen(false); + } + + // potvrzení tlačítkem + if (digitalRead(BUTTON_PIN) == HIGH) { + savedPassword = formatCode(passwordValue); + ConfirmSound(); + + lcd.setCursor(0, 0); + lcd.printstr("Heslo ulozeno! "); + lcd.setCursor(0, 1); + lcd.printstr("* * * * "); + delay(1500); + + // přechod do normálního režimu + lcd.setCursor(0, 0); + lcd.printstr("Zadejte heslo: "); + lcd.setCursor(0, 1); + lcd.printstr(" "); + + // debounce + while (digitalRead(BUTTON_PIN) == HIGH) {} + delay(50); + } + } +} + void setup() { Serial.begin(9600); @@ -125,23 +212,21 @@ void setup() { Serial.println("Piny: 1/8"); // nastavení pinů pro RGB LED (bude přes pwm, není to nutné přes pinmode, ale lepší to udělat) - pinMode(RGB_RED_PIN, OUTPUT); Serial.println("Piny: 2/8"); - pinMode(RGB_GREEN_PIN, OUTPUT); Serial.println("Piny: 3/8"); - pinMode(RGB_BLUE_PIN, OUTPUT); Serial.println("Piny: 4/8"); - - // nastavení pinů pro enkodér - pinMode(ENCODER_CLK_PIN, INPUT); Serial.println("Piny: 5/8"); - pinMode(ENCODER_DT_PIN, INPUT); Serial.println("Piny: 6/8"); - - // nastavení pinu pro servo - pinMode(SERVO_PIN, OUTPUT); Serial.println("Piny: 7/8"); - - // nastavení pinu pro bzučák - pinMode(BUZZER_PIN, OUTPUT); Serial.println("Piny: 8/8"); + // formátování vymyšleno přes Claude Sonnet 4.6 + pinMode(BUTTON_PIN, INPUT_PULLDOWN); Serial.println("Piny: 1/8"); + pinMode(RGB_RED_PIN, OUTPUT); Serial.println("Piny: 2/8"); + pinMode(RGB_GREEN_PIN, OUTPUT); Serial.println("Piny: 3/8"); + pinMode(RGB_BLUE_PIN, OUTPUT); Serial.println("Piny: 4/8"); + pinMode(ENCODER_CLK_PIN, INPUT); Serial.println("Piny: 5/8"); + pinMode(ENCODER_DT_PIN, INPUT); Serial.println("Piny: 6/8"); + pinMode(SERVO_PIN, OUTPUT); Serial.println("Piny: 7/8"); + pinMode(BUZZER_PIN, OUTPUT); Serial.println("Piny: 8/8"); Serial.println("Piny nastaveny."); lcd.setCursor(0, 1); lcd.printstr("Piny nastaveny. "); delay(1000); + + runPasswordSetup(); } void loop() {