From e26f1a550b45b5990966898dc53027a2fff2f4ce Mon Sep 17 00:00:00 2001 From: Lucas Faria Mendes Date: Sun, 19 Apr 2026 01:19:24 -0300 Subject: upload --- c4.ino | 261 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 261 insertions(+) create mode 100644 c4.ino (limited to 'c4.ino') diff --git a/c4.ino b/c4.ino new file mode 100644 index 0000000..42bea49 --- /dev/null +++ b/c4.ino @@ -0,0 +1,261 @@ +#include +#include + +LiquidCrystal lcd(10, 11, A0, A1, A2, A3); +const byte ROWS = 4; +const byte COLS = 4; + +char keys[ROWS][COLS] = { + {'*','0','#','D'}, // S13, S14, S15, S16 + {'7','8','9','C'}, // S9, S10, S11, S12 + {'4','5','6','B'}, // S5, S6, S7, S8 + {'1','2','3','A'} // S1, S2, S3, S4 +}; + +byte rowPins[ROWS] = {2, 3, 4, 5}; +byte colPins[COLS] = {6, 7, 8, 9}; + +Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS); + +const int led = 13; +const int buzzer = 12; + +String correctPassword = "7355608"; +String disarmPassword = "6672364"; +String inputPassword = ""; +bool armed = false; +bool configuringTime = false; +int timeToExplode = 900; + +const int minTimeToExplode = 5; +const int maxTimeToExplode = 900; + +void showTimeConfigMenu() { + lcd.clear(); + lcd.setCursor(0, 0); + lcd.print("Tempo: "); + lcd.print(timeToExplode); + lcd.print("s"); + lcd.setCursor(0, 1); + lcd.print("B+ C- D sair"); +} + +void updatePasswordDisplay() { + lcd.setCursor(4, 0); + + int passwordLength = correctPassword.length(); + int inputLength = inputPassword.length(); + int firstInputPosition = passwordLength - inputLength; + + for (int i = 0; i < passwordLength; i++) { + if (i < firstInputPosition) lcd.print("*"); + else lcd.print(inputPassword[i - firstInputPosition]); + } +} + +void initMenu() { + armed = false; + configuringTime = false; + inputPassword = ""; + noTone(buzzer); + digitalWrite(led, LOW); + + lcd.clear(); + updatePasswordDisplay(); +} + +void armBomb() { + armed = true; + lcd.clear(); + lcd.setCursor(0, 0); + lcd.print("Bomba armada!"); + + for (int i = 0; i < 3; i++) { + tone(buzzer, 1500, 100); + delay(150); + } + + delay(1000); +} + +void explode() { + lcd.clear(); + lcd.setCursor(0, 0); + lcd.print("TERRORIST WIN"); + + for (int f = 200; f > 50; f--) { + tone(buzzer, f, 10); + delay(5); + } + + while (true) { + char key = keypad.getKey(); + if (key == '#') { + initMenu(); + return; + } + } +} + +void updateDisarmDisplay(String disarmInput) { + lcd.setCursor(0, 0); + lcd.print("Desarmar: "); + lcd.setCursor(0, 1); + + int disarmLength = disarmPassword.length(); + int inputLength = disarmInput.length(); + int firstInputPosition = disarmLength - inputLength; + + for (int i = 0; i < disarmLength; i++) { + if (i < firstInputPosition) lcd.print("*"); + else lcd.print(disarmInput[i - firstInputPosition]); + } + lcd.print(" "); +} + +void execCount() { + unsigned long totalMs = (unsigned long)timeToExplode * 1000UL; + unsigned long startTime = millis(); + unsigned long lastBlink = 0; + bool ledState = false; + bool disarmMode = false; + String disarmInput = ""; + int lastShownSecond = -1; + + while (armed) { + unsigned long elapsed = millis() - startTime; + if (elapsed >= totalMs) { + explode(); + return; + } + + unsigned long remainingMs = totalMs - elapsed; + int remainingSeconds = (remainingMs + 999UL) / 1000UL; + + int blinkInterval = 1000; + if (remainingSeconds <= 10) blinkInterval = 400; + if (remainingSeconds <= 5) blinkInterval = 150; + + if (millis() - lastBlink >= (unsigned long)blinkInterval) { + lastBlink = millis(); + ledState = !ledState; + digitalWrite(led, ledState ? HIGH : LOW); + tone(buzzer, 2000, 80); + } + + if (!disarmMode && remainingSeconds != lastShownSecond) { + lastShownSecond = remainingSeconds; + lcd.clear(); + lcd.setCursor(0, 0); + lcd.print("Explode em:"); + lcd.setCursor(0, 1); + lcd.print(remainingSeconds); + lcd.print("s"); + lcd.print(" "); + } + + char key = keypad.getKey(); + if (!key) continue; + + tone(buzzer, 1000, 50); + + if (!disarmMode) { + if (key == 'A') { + disarmMode = true; + disarmInput = ""; + lcd.clear(); + updateDisarmDisplay(disarmInput); + } + continue; + } + + if (key == '#') { + disarmMode = false; + disarmInput = ""; + lastShownSecond = -1; + continue; + } + + if (key < '0' || key > '9') continue; + + disarmInput += key; + if (disarmInput.length() > disarmPassword.length()) + disarmInput = disarmInput.substring(disarmInput.length() - disarmPassword.length()); + + updateDisarmDisplay(disarmInput); + + if (disarmInput == disarmPassword) { + lcd.clear(); + lcd.setCursor(0, 0); + lcd.print("COUNTER"); + lcd.setCursor(0, 1); + lcd.print("TERRORIST WIN"); + int winMelody[] = {1200, 1500, 1800, 2200}; + int noteDuration = 120; + + for (int i = 0; i < 4; i++) { + tone(buzzer, winMelody[i], noteDuration); + delay(noteDuration + 40); + } + + delay(2200); + initMenu(); + return; + } + } +} + +void setup() { + lcd.begin(16, 2); + pinMode(led, OUTPUT); + pinMode(buzzer, OUTPUT); + + initMenu(); +} + +void loop() { + char key = keypad.getKey(); + + if (key) { + tone(buzzer, 1000, 50); + + if (!armed && key == 'D') { + if (configuringTime) { + initMenu(); + } else { + configuringTime = true; + showTimeConfigMenu(); + } + return; + } + + if (configuringTime) { + if (key == 'B' && timeToExplode < maxTimeToExplode) { + timeToExplode += 10; + showTimeConfigMenu(); + } else if (key == 'C' && timeToExplode > minTimeToExplode) { + timeToExplode -= 10; + showTimeConfigMenu(); + } + return; + } + + if (key == '*') { + initMenu(); + } else if (!armed && key != '#' && key != 'A' && key != 'B' && key != 'C' && key != 'D') { + inputPassword += key; + if (inputPassword.length() > correctPassword.length()) { + if (inputPassword != correctPassword) { + initMenu(); + return; + } + } + + updatePasswordDisplay(); + + if (inputPassword == correctPassword) armBomb(); + } + } + + if (armed) execCount(); +} \ No newline at end of file -- cgit v1.2.3