Commit | Line | Data |
---|---|---|
436dacb7 TM |
1 | /* |
2 | * Exit Rum 2 | |
3 | * (c) Tomas 'Harvie' Mudrunka 2018 | |
4 | */ | |
5 | ||
436dacb7 TM |
6 | //Nastaveni bomby |
7 | ||
8 | #define DISARM_CODE "12345678" | |
9 | #define TIME_COUNTDOWN 600 | |
10 | #define TIME_COUNTDOWN 15 | |
11 | ||
436dacb7 TM |
12 | //Prirazeni pinu |
13 | ||
14 | #define PIN_CABLE_CHECK 2 | |
15 | ||
e19bc394 | 16 | #define PIN_KEY_CLK 3 //IRQ! |
436dacb7 TM |
17 | #define PIN_KEY_DATA 4 |
18 | ||
19 | #define PIN_LED_G 5 | |
20 | #define PIN_LED_B 6 | |
21 | #define PIN_LED_R 7 | |
22 | ||
23 | #define PIN_COIL_LO 10 | |
24 | #define PIN_COIL_HI 11 | |
25 | ||
26 | #define ADDR_DISPLAY 0x70 | |
27 | ||
28 | ////////////////////////////////////////////////////////////////// | |
29 | ||
30 | #include <PS2Keyboard.h> | |
31 | ||
32 | //#include <Wire.h> // Enable this line if using Arduino Uno, Mega, etc. | |
33 | #include <Adafruit_GFX.h> | |
34 | #include "Adafruit_LEDBackpack.h" | |
35 | ||
36 | PS2Keyboard keyboard; | |
37 | Adafruit_7segment display = Adafruit_7segment(); | |
38 | ||
39 | #define cnt_sec(s) (s%60) | |
40 | #define cnt_min(s) (s/60) | |
41 | #define cnt_dec(s) (cnt_min(s)*100+cnt_sec(s)) | |
42 | ||
e19bc394 TM |
43 | void setup() |
44 | { | |
45 | //Pripravime periferie | |
46 | Serial.begin(9600); | |
47 | keyboard.begin(PIN_KEY_DATA, PIN_KEY_CLK); | |
48 | display.begin(ADDR_DISPLAY); | |
49 | ||
50 | pinMode(PIN_LED_R, OUTPUT); | |
51 | pinMode(PIN_LED_G, OUTPUT); | |
52 | pinMode(PIN_LED_B, OUTPUT); | |
53 | pinMode(PIN_COIL_LO, OUTPUT); | |
54 | pinMode(PIN_COIL_HI, OUTPUT); | |
55 | pinMode(PIN_CABLE_CHECK, INPUT_PULLUP); | |
56 | } | |
57 | ||
58 | void loop() | |
59 | { | |
60 | //Vynulujem periferie | |
61 | digitalWrite(PIN_COIL_LO, LOW); | |
62 | digitalWrite(PIN_COIL_HI, LOW); | |
63 | analogWrite(PIN_LED_R, 0); | |
64 | analogWrite(PIN_LED_G, 0); | |
65 | analogWrite(PIN_LED_B, 0); | |
66 | ||
67 | display.clear(); | |
68 | display.setBrightness(15); | |
69 | display.writeDisplay(); | |
70 | ||
71 | while (keyboard.available()) keyboard.read(); | |
436dacb7 | 72 | |
e19bc394 TM |
73 | //Pockame na pripojeni kabelu |
74 | display.print(0xCAB1, HEX); | |
75 | display.writeDisplay(); | |
76 | while (digitalRead(PIN_CABLE_CHECK)) | |
77 | delay(100); | |
436dacb7 | 78 | |
e19bc394 TM |
79 | //Pockame na odpojeni kabelu |
80 | display.clear(); | |
81 | display.writeDisplay(); | |
82 | while (!digitalRead(PIN_CABLE_CHECK)) | |
83 | delay(100); | |
84 | ||
85 | //Odpocitavame | |
86 | int guessed = 0, disarmed = 0; | |
87 | ||
88 | for (int cnt = TIME_COUNTDOWN; cnt >= 0 && !disarmed; cnt--) { | |
89 | display.print(cnt_dec(cnt), DEC); | |
90 | display.drawColon((cnt + 1) % 2); | |
91 | display.writeDisplay(); | |
92 | ||
93 | analogWrite(PIN_LED_R, random(0, 3)); | |
94 | analogWrite(PIN_LED_G, random(0, 3)); | |
95 | analogWrite(PIN_LED_B, random(0, 3)); | |
96 | ||
97 | tone(PIN_COIL_LO, 1000, 30); | |
98 | ||
99 | delay(1000); | |
100 | ||
101 | //Cteme klavesnici | |
102 | while (keyboard.available()) { | |
103 | char c = keyboard.read(); | |
104 | if (c == PS2_ENTER) { | |
105 | Serial.println(); | |
106 | } else { | |
107 | Serial.print(c); | |
108 | } | |
109 | if (c == DISARM_CODE[guessed]) { | |
110 | guessed++; | |
111 | Serial.println("\nGOT!"); | |
112 | } else { | |
113 | guessed = 0; | |
114 | Serial.println("\nFAIL!"); | |
115 | } | |
116 | if (DISARM_CODE[guessed] == 0) { | |
117 | disarmed = 1; | |
118 | Serial.println("\nDISARMED!"); | |
119 | } | |
120 | } | |
121 | } | |
436dacb7 | 122 | |
e19bc394 TM |
123 | if (disarmed) { |
124 | //Bomba byla deaktivovana | |
125 | tone(PIN_COIL_LO, 3000, 500); | |
126 | delay(500); | |
127 | tone(PIN_COIL_LO, 5000, 500); | |
128 | analogWrite(PIN_LED_R, 0); | |
129 | analogWrite(PIN_LED_G, 3); | |
130 | analogWrite(PIN_LED_B, 0); | |
131 | display.print(0xDEFD, HEX); | |
132 | display.writeDisplay(); | |
133 | delay(1000); | |
436dacb7 | 134 | |
e19bc394 | 135 | } |
436dacb7 | 136 | |
e19bc394 TM |
137 | char s = digitalRead(PIN_CABLE_CHECK); |
138 | while (digitalRead(PIN_CABLE_CHECK) != s) { | |
436dacb7 | 139 | |
e19bc394 | 140 | if (!disarmed) { |
436dacb7 | 141 | |
e19bc394 TM |
142 | //Bomba vybouchla |
143 | int i; | |
436dacb7 | 144 | |
e19bc394 TM |
145 | analogWrite(PIN_LED_R, random(0, 255)); |
146 | analogWrite(PIN_LED_G, random(0, 0)); | |
147 | analogWrite(PIN_LED_B, random(0, 255)); | |
436dacb7 | 148 | |
e19bc394 TM |
149 | for (i = 0; i < 10; i++) |
150 | display.writeDigitRaw(i, random(0, 255)); | |
151 | display.setBrightness(random(0, 15)); | |
152 | display.writeDisplay(); | |
436dacb7 | 153 | |
e19bc394 TM |
154 | digitalWrite(PIN_COIL_HI, HIGH); |
155 | delay(20); | |
156 | digitalWrite(PIN_COIL_HI, LOW); | |
157 | int rnd = random(30, 100); | |
158 | tone(PIN_COIL_LO, random(100, 8000), rnd); | |
159 | delay(rnd); | |
160 | } | |
436dacb7 TM |
161 | } |
162 | ||
436dacb7 | 163 | } |