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