Aktualni
[mirrors/Programs.git] / arduino / Exit_Rum / bomba2b / bomba2b.ino
CommitLineData
436dacb7
TM
1/*
2 * Exit Rum 2
3 * (c) Tomas 'Harvie' Mudrunka 2018
4 */
5
436dacb7
TM
6//Nastaveni bomby
7
e69402f8 8#define DISARM_CODE "73138477"
46a16958 9#define TIME_COUNTDOWN 600 //600
e69402f8 10//#define TIME_COUNTDOWN 15
436dacb7 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
46a16958
TM
30#define SOFTSERIAL
31
32#ifndef SOFTSERIAL
436dacb7 33#include <PS2Keyboard.h>
46a16958
TM
34PS2Keyboard keyboard;
35#else
36#include <SoftwareSerial.h>
37SoftwareSerial mySerial(PIN_KEY_CLK, PIN_KEY_DATA);
38#endif
436dacb7
TM
39
40//#include <Wire.h> // Enable this line if using Arduino Uno, Mega, etc.
41#include <Adafruit_GFX.h>
42#include "Adafruit_LEDBackpack.h"
43
436dacb7
TM
44Adafruit_7segment display = Adafruit_7segment();
45
46#define cnt_sec(s) (s%60)
47#define cnt_min(s) (s/60)
48#define cnt_dec(s) (cnt_min(s)*100+cnt_sec(s))
49
e19bc394
TM
50void setup()
51{
52 //Pripravime periferie
53 Serial.begin(9600);
46a16958
TM
54 Serial.println("zaciname");
55#ifndef SOFTSERIAL
e19bc394 56 keyboard.begin(PIN_KEY_DATA, PIN_KEY_CLK);
46a16958
TM
57#else
58 mySerial.begin(4800);
59#endif
e19bc394
TM
60 display.begin(ADDR_DISPLAY);
61
62 pinMode(PIN_LED_R, OUTPUT);
63 pinMode(PIN_LED_G, OUTPUT);
64 pinMode(PIN_LED_B, OUTPUT);
65 pinMode(PIN_COIL_LO, OUTPUT);
66 pinMode(PIN_COIL_HI, OUTPUT);
67 pinMode(PIN_CABLE_CHECK, INPUT_PULLUP);
68}
69
e69402f8
TM
70int test_disarmed(int reset)
71{
72
73 static int guessed = 0, disarmed = 0;
74 if (reset) {
75 guessed = 0;
76 disarmed = 0;
77 }
78 //Cteme klavesnici
46a16958 79#ifndef SOFTSERIAL
e69402f8 80 while (keyboard.available()) {
46a16958
TM
81 char c = keyboard.read();
82 //Serial.print("Klavesa: ");
83 //Serial.println(c);
84#else
85 while (mySerial.available()) {
86 char c = mySerial.read();
87#endif
88#ifndef SOFTSERIAL
e69402f8
TM
89 if (c == PS2_ENTER) {
90 Serial.println();
91 } else {
46a16958
TM
92#else
93 if(1) {
94#endif
e69402f8
TM
95 Serial.print(c);
96 }
46a16958
TM
97
98 if (c < '0' || c > '9') {
99 Serial.println("IGNORED!");
100 continue;
101
102 }
e69402f8
TM
103 if (c == DISARM_CODE[guessed]) {
104 guessed++;
105 Serial.println("\nGOT!");
106 } else {
107 guessed = 0;
108 Serial.println("\nFAIL!");
46a16958
TM
109 if (c == DISARM_CODE[guessed]) {
110 guessed++;
111 Serial.println("\nGOT!");
112 }
e69402f8
TM
113 }
114 if (DISARM_CODE[guessed] == 0) {
115 disarmed = 1;
116 Serial.println("\nDISARMED!");
117 }
118 }
119
120 return disarmed;
121
122}
123
e19bc394
TM
124void loop()
125{
126 //Vynulujem periferie
127 digitalWrite(PIN_COIL_LO, LOW);
128 digitalWrite(PIN_COIL_HI, LOW);
129 analogWrite(PIN_LED_R, 0);
130 analogWrite(PIN_LED_G, 0);
131 analogWrite(PIN_LED_B, 0);
132
133 display.clear();
134 display.setBrightness(15);
135 display.writeDisplay();
136
46a16958 137#ifndef SOFTSERIAL
1199f4b2
TM
138 while (keyboard.available())
139 keyboard.read();
46a16958
TM
140#else
141 while (mySerial.available())
142 mySerial.read();
143#endif
e69402f8 144 test_disarmed(1);
436dacb7 145
e19bc394
TM
146 //Pockame na pripojeni kabelu
147 display.print(0xCAB1, HEX);
148 display.writeDisplay();
149 while (digitalRead(PIN_CABLE_CHECK))
150 delay(100);
436dacb7 151
e19bc394
TM
152 //Pockame na odpojeni kabelu
153 display.clear();
154 display.writeDisplay();
155 while (!digitalRead(PIN_CABLE_CHECK))
156 delay(100);
157
158 //Odpocitavame
e69402f8 159 for (int cnt = TIME_COUNTDOWN; cnt >= 0 && !test_disarmed(0); cnt--) {
e19bc394
TM
160 display.print(cnt_dec(cnt), DEC);
161 display.drawColon((cnt + 1) % 2);
162 display.writeDisplay();
163
164 analogWrite(PIN_LED_R, random(0, 3));
165 analogWrite(PIN_LED_G, random(0, 3));
166 analogWrite(PIN_LED_B, random(0, 3));
167
e69402f8
TM
168 //Tikani / pipani
169 tone(PIN_COIL_LO, 1000, 50);
170 delay(60);
171 digitalWrite(PIN_COIL_LO, HIGH);
172 delay(40);
173 digitalWrite(PIN_COIL_LO, LOW);
436dacb7 174
e69402f8 175 delay(900);
436dacb7 176
e19bc394 177 }
436dacb7 178
436dacb7 179
e69402f8
TM
180 while (!test_disarmed(0)) {
181 //Bomba vybouchla
182 int i;
436dacb7 183
e69402f8
TM
184 analogWrite(PIN_LED_R, random(0, 255));
185 analogWrite(PIN_LED_G, random(0, 0));
186 analogWrite(PIN_LED_B, random(0, 255));
436dacb7 187
e69402f8
TM
188 for (i = 0; i < 10; i++)
189 display.writeDigitRaw(i, random(0, 255));
190 display.setBrightness(random(0, 15));
191 display.writeDisplay();
436dacb7 192
e69402f8
TM
193 digitalWrite(PIN_COIL_HI, HIGH);
194 delay(20);
195 digitalWrite(PIN_COIL_HI, LOW);
196 int rnd = random(30, 100);
197 tone(PIN_COIL_LO, random(100, 8000), rnd);
198 delay(rnd);
436dacb7
TM
199 }
200
e69402f8
TM
201
202 //Bomba byla deaktivovana
203 tone(PIN_COIL_LO, 3000, 500);
204 delay(500);
205 tone(PIN_COIL_LO, 5000, 500);
206 analogWrite(PIN_LED_R, 0);
207 analogWrite(PIN_LED_G, 0);
208 analogWrite(PIN_LED_B, 0);
209 display.print(0xDEFD, HEX);
210 display.writeDisplay();
211 delay(1000);
212
213
214 //Pockame na zmenu kabelu
215 char s = digitalRead(PIN_CABLE_CHECK);
216 while (digitalRead(PIN_CABLE_CHECK) == s)
217 delay(100);
218
436dacb7 219}
This page took 0.372817 seconds and 4 git commands to generate.