6ac34f2545d1118aa75bc017dd63588bfb065492
[mirrors/Programs.git] / bomba2b.ino
1 /*
2 * Exit Rum 2
3 * (c) Tomas 'Harvie' Mudrunka 2018
4 */
5
6 //Nastaveni bomby
7
8 #define DISARM_CODE "73138477"
9 #define TIME_COUNTDOWN 600
10 //#define TIME_COUNTDOWN 15
11
12 //Prirazeni pinu
13
14 #define PIN_CABLE_CHECK 2
15
16 #define PIN_KEY_CLK 3 //IRQ!
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
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 int test_disarmed(int reset)
59 {
60
61 static int guessed = 0, disarmed = 0;
62 if (reset) {
63 guessed = 0;
64 disarmed = 0;
65 }
66 //Cteme klavesnici
67 while (keyboard.available()) {
68 char c = keyboard.read();
69 if (c == PS2_ENTER) {
70 Serial.println();
71 } else {
72 Serial.print(c);
73 }
74 if (c == DISARM_CODE[guessed]) {
75 guessed++;
76 Serial.println("\nGOT!");
77 } else {
78 guessed = 0;
79 Serial.println("\nFAIL!");
80 }
81 if (DISARM_CODE[guessed] == 0) {
82 disarmed = 1;
83 Serial.println("\nDISARMED!");
84 }
85 }
86
87 return disarmed;
88
89 }
90
91 void loop()
92 {
93 //Vynulujem periferie
94 digitalWrite(PIN_COIL_LO, LOW);
95 digitalWrite(PIN_COIL_HI, LOW);
96 analogWrite(PIN_LED_R, 0);
97 analogWrite(PIN_LED_G, 0);
98 analogWrite(PIN_LED_B, 0);
99
100 display.clear();
101 display.setBrightness(15);
102 display.writeDisplay();
103
104 while (keyboard.available())
105 keyboard.read();
106 test_disarmed(1);
107
108 //Pockame na pripojeni kabelu
109 display.print(0xCAB1, HEX);
110 display.writeDisplay();
111 while (digitalRead(PIN_CABLE_CHECK))
112 delay(100);
113
114 //Pockame na odpojeni kabelu
115 display.clear();
116 display.writeDisplay();
117 while (!digitalRead(PIN_CABLE_CHECK))
118 delay(100);
119
120 //Odpocitavame
121 for (int cnt = TIME_COUNTDOWN; cnt >= 0 && !test_disarmed(0); cnt--) {
122 display.print(cnt_dec(cnt), DEC);
123 display.drawColon((cnt + 1) % 2);
124 display.writeDisplay();
125
126 analogWrite(PIN_LED_R, random(0, 3));
127 analogWrite(PIN_LED_G, random(0, 3));
128 analogWrite(PIN_LED_B, random(0, 3));
129
130 //Tikani / pipani
131 tone(PIN_COIL_LO, 1000, 50);
132 delay(60);
133 digitalWrite(PIN_COIL_LO, HIGH);
134 delay(40);
135 digitalWrite(PIN_COIL_LO, LOW);
136
137 delay(900);
138
139 }
140
141
142 while (!test_disarmed(0)) {
143 //Bomba vybouchla
144 int i;
145
146 analogWrite(PIN_LED_R, random(0, 255));
147 analogWrite(PIN_LED_G, random(0, 0));
148 analogWrite(PIN_LED_B, random(0, 255));
149
150 for (i = 0; i < 10; i++)
151 display.writeDigitRaw(i, random(0, 255));
152 display.setBrightness(random(0, 15));
153 display.writeDisplay();
154
155 digitalWrite(PIN_COIL_HI, HIGH);
156 delay(20);
157 digitalWrite(PIN_COIL_HI, LOW);
158 int rnd = random(30, 100);
159 tone(PIN_COIL_LO, random(100, 8000), rnd);
160 delay(rnd);
161 }
162
163
164 //Bomba byla deaktivovana
165 tone(PIN_COIL_LO, 3000, 500);
166 delay(500);
167 tone(PIN_COIL_LO, 5000, 500);
168 analogWrite(PIN_LED_R, 0);
169 analogWrite(PIN_LED_G, 0);
170 analogWrite(PIN_LED_B, 0);
171 display.print(0xDEFD, HEX);
172 display.writeDisplay();
173 delay(1000);
174
175
176 //Pockame na zmenu kabelu
177 char s = digitalRead(PIN_CABLE_CHECK);
178 while (digitalRead(PIN_CABLE_CHECK) == s)
179 delay(100);
180
181 }
This page took 1.67069 seconds and 3 git commands to generate.