Přidání funkčnosti do loop()

This commit is contained in:
Matěj Kubíček
2026-06-08 10:15:05 +02:00
parent 8e3e3b33db
commit da759fa0b6
+96 -14
View File
@@ -27,6 +27,9 @@ int password = 0; // 0-9999
int passwordValue = 0; int passwordValue = 0;
String savedPassword = ""; String savedPassword = "";
// stav zámku
bool isOpen = false;
// enkodér // enkodér
int lastCLK = HIGH; int lastCLK = HIGH;
@@ -36,8 +39,8 @@ unsigned long lastEncoderMove = 0;
bool masked = false; bool masked = false;
// přeformátování kódu na 4 cifry // přeformátování kódu na 4 cifry
String formatCode(int value) { String formatCode(const int value) {
String s = String(value); auto s = String(value);
while (s.length() < 4) s = "0" + s; while (s.length() < 4) s = "0" + s;
return s; return s;
} }
@@ -48,8 +51,8 @@ void CloseLock() { servo.write(CLOSED_POS); }
// chování bzučáku // chování bzučáku
void OpenSound() { void OpenSound() {
int melody[] = { 262, 330, 392, 523 }; // C4, E4, G4, C5 const int melody[] = { 262, 330, 392, 523 }; // C4, E4, G4, C5
int durations[] = { 150, 150, 150, 300 }; const int durations[] = { 150, 150, 150, 300 };
for (int i = 0; i < 4; i++) { for (int i = 0; i < 4; i++) {
tone(BUZZER_PIN, melody[i], durations[i]); tone(BUZZER_PIN, melody[i], durations[i]);
@@ -59,8 +62,8 @@ void OpenSound() {
} }
void CloseSound() { void CloseSound() {
tone(BUZZER_PIN, 262, 150); tone(BUZZER_PIN, 200, 200); delay(230);
delay(500); tone(BUZZER_PIN, 150, 400); delay(430);
noTone(BUZZER_PIN); noTone(BUZZER_PIN);
} }
@@ -110,15 +113,28 @@ void ButtonPressed() {
} }
} }
// setup hesla // zobrazení zadávací obrazovky
void showSetupScreen(bool showValue) { void showEntryScreen(const bool showValue) {
lcd.setCursor(0, 0);
lcd.printstr("Zadejte heslo: ");
lcd.setCursor(0, 1);
if (showValue) {
String code = formatCode(password);
const String display = code[0] + String(" ") + code[1] + String(" ") + code[2] + String(" ") + code[3] + String(" ");
lcd.printstr(display.c_str());
} else {
lcd.printstr("* * * * ");
}
}
// zobrazení setup obrazovky
void showSetupScreen(const bool showValue) {
lcd.setCursor(0, 0); lcd.setCursor(0, 0);
lcd.printstr("Nastavte heslo: "); lcd.printstr("Nastavte heslo: ");
lcd.setCursor(0, 1); lcd.setCursor(0, 1);
if (showValue) { if (showValue) {
String code = formatCode(passwordValue); String code = formatCode(passwordValue);
// mezery okolo pro vizuální oddělení číslic const String display = code[0] + String(" ") + code[1] + String(" ") + code[2] + String(" ") + code[3] + String(" ");
String display = code[0] + String(" ") + code[1] + String(" ") + code[2] + String(" ") + code[3] + String(" ");
lcd.printstr(display.c_str()); lcd.printstr(display.c_str());
} else { } else {
lcd.printstr("* * * * "); lcd.printstr("* * * * ");
@@ -137,10 +153,9 @@ void runPasswordSetup() {
while (savedPassword == "") { while (savedPassword == "") {
// čtení enkodéru // čtení enkodéru
int currentCLK = digitalRead(ENCODER_CLK_PIN); const int currentCLK = digitalRead(ENCODER_CLK_PIN);
if (currentCLK != lastCLK && currentCLK == LOW) { if (currentCLK != lastCLK && currentCLK == LOW) {
int dt = digitalRead(ENCODER_DT_PIN); if (const int dt = digitalRead(ENCODER_DT_PIN); dt != currentCLK) {
if (dt != currentCLK) {
passwordValue = (passwordValue + 1) % 10000; passwordValue = (passwordValue + 1) % 10000;
} else { } else {
passwordValue = (passwordValue - 1 + 10000) % 10000; passwordValue = (passwordValue - 1 + 10000) % 10000;
@@ -227,5 +242,72 @@ void setup() {
} }
void loop() { void loop() {
// čtení enkodéru jen pokud je zamčeno
// (při odemčeném stavu enkodér nemá smysl)
if (!isOpen) {
const int currentCLK = digitalRead(ENCODER_CLK_PIN);
if (currentCLK != lastCLK && currentCLK == LOW) {
if (const int dt = digitalRead(ENCODER_DT_PIN); dt != currentCLK) {
password = (password + 1) % 10000;
} else {
password = (password - 1 + 10000) % 10000;
}
masked = false;
lastEncoderMove = millis();
showEntryScreen(true);
}
lastCLK = currentCLK;
} // maskování po nečinnosti
if (!masked && millis() - lastEncoderMove >= MASK_DELAY_MS) {
masked = true;
showEntryScreen(false);
}
}
// tlačítko
if (digitalRead(BUTTON_PIN) == HIGH) {
// debounce
while (digitalRead(BUTTON_PIN) == HIGH) {}
delay(50);
if (!isOpen) {
// pokus o odemčení
if (formatCode(password) == savedPassword) {
isOpen = true;
OpenLock();
OpenLED();
OpenSound();
lcd.setCursor(0, 0);
lcd.printstr("Odemceno! ");
lcd.setCursor(0, 1);
lcd.printstr("Stiskni pro zamk");
} else {
CloseSound();
lcd.setCursor(0, 0);
lcd.printstr("Spatne heslo! ");
lcd.setCursor(0, 1);
lcd.printstr(" ");
delay(1500);
// návrat na zadávací obrazovku, hodnota zachována
masked = false;
lastEncoderMove = millis();
showEntryScreen(true);
}
} else {
// zamčení
isOpen = false;
CloseLock();
CloseLED();
CloseSound();
password = 0;
masked = false;
lastEncoderMove = millis();
showEntryScreen(true);
}
}
}