| 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 //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 | #define SOFTSERIAL |
| 31 | |
| 32 | #ifndef SOFTSERIAL |
| 33 | #include <PS2Keyboard.h> |
| 34 | PS2Keyboard keyboard; |
| 35 | #else |
| 36 | #include <SoftwareSerial.h> |
| 37 | SoftwareSerial mySerial(PIN_KEY_CLK, PIN_KEY_DATA); |
| 38 | #endif |
| 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 | |
| 44 | Adafruit_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 | |
| 50 | void setup() |
| 51 | { |
| 52 | //Pripravime periferie |
| 53 | Serial.begin(9600); |
| 54 | Serial.println("zaciname"); |
| 55 | #ifndef SOFTSERIAL |
| 56 | keyboard.begin(PIN_KEY_DATA, PIN_KEY_CLK); |
| 57 | #else |
| 58 | mySerial.begin(4800); |
| 59 | #endif |
| 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 | |
| 70 | int 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 |
| 79 | #ifndef SOFTSERIAL |
| 80 | while (keyboard.available()) { |
| 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 |
| 89 | if (c == PS2_ENTER) { |
| 90 | Serial.println(); |
| 91 | } else { |
| 92 | #else |
| 93 | if(1) { |
| 94 | #endif |
| 95 | Serial.print(c); |
| 96 | } |
| 97 | |
| 98 | if (c < '0' || c > '9') { |
| 99 | Serial.println("IGNORED!"); |
| 100 | continue; |
| 101 | |
| 102 | } |
| 103 | if (c == DISARM_CODE[guessed]) { |
| 104 | guessed++; |
| 105 | Serial.println("\nGOT!"); |
| 106 | } else { |
| 107 | guessed = 0; |
| 108 | Serial.println("\nFAIL!"); |
| 109 | if (c == DISARM_CODE[guessed]) { |
| 110 | guessed++; |
| 111 | Serial.println("\nGOT!"); |
| 112 | } |
| 113 | } |
| 114 | if (DISARM_CODE[guessed] == 0) { |
| 115 | disarmed = 1; |
| 116 | Serial.println("\nDISARMED!"); |
| 117 | } |
| 118 | } |
| 119 | |
| 120 | return disarmed; |
| 121 | |
| 122 | } |
| 123 | |
| 124 | void 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 | |
| 137 | #ifndef SOFTSERIAL |
| 138 | while (keyboard.available()) |
| 139 | keyboard.read(); |
| 140 | #else |
| 141 | while (mySerial.available()) |
| 142 | mySerial.read(); |
| 143 | #endif |
| 144 | test_disarmed(1); |
| 145 | |
| 146 | //Pockame na pripojeni kabelu |
| 147 | display.print(0xCAB1, HEX); |
| 148 | display.writeDisplay(); |
| 149 | while (digitalRead(PIN_CABLE_CHECK)) |
| 150 | delay(100); |
| 151 | |
| 152 | //Pockame na odpojeni kabelu |
| 153 | display.clear(); |
| 154 | display.writeDisplay(); |
| 155 | while (!digitalRead(PIN_CABLE_CHECK)) |
| 156 | delay(100); |
| 157 | |
| 158 | //Odpocitavame |
| 159 | for (int cnt = TIME_COUNTDOWN; cnt >= 0 && !test_disarmed(0); cnt--) { |
| 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 | |
| 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); |
| 174 | |
| 175 | delay(900); |
| 176 | |
| 177 | } |
| 178 | |
| 179 | |
| 180 | while (!test_disarmed(0)) { |
| 181 | //Bomba vybouchla |
| 182 | int i; |
| 183 | |
| 184 | analogWrite(PIN_LED_R, random(0, 255)); |
| 185 | analogWrite(PIN_LED_G, random(0, 0)); |
| 186 | analogWrite(PIN_LED_B, random(0, 255)); |
| 187 | |
| 188 | for (i = 0; i < 10; i++) |
| 189 | display.writeDigitRaw(i, random(0, 255)); |
| 190 | display.setBrightness(random(0, 15)); |
| 191 | display.writeDisplay(); |
| 192 | |
| 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); |
| 199 | } |
| 200 | |
| 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 | |
| 219 | } |