Aktualni
[mirrors/Programs.git] / arduino / Exit_Rum / bomba2b / bomba2b.ino
index 8445fb38b08c59c63ee8614894147e5330f4e5e2..5c49c1cd7565987805639ade2883a9e00fd3dbd6 100644 (file)
@@ -5,9 +5,9 @@
 
 //Nastaveni bomby
 
-#define DISARM_CODE "12345678"
-#define TIME_COUNTDOWN 600
-#define TIME_COUNTDOWN 15
+#define DISARM_CODE "73138477"
+#define TIME_COUNTDOWN 600 //600
+//#define TIME_COUNTDOWN 15
 
 //Prirazeni pinu
 
 
 //////////////////////////////////////////////////////////////////
 
+#define SOFTSERIAL
+
+#ifndef SOFTSERIAL
 #include <PS2Keyboard.h>
+PS2Keyboard keyboard;
+#else
+#include <SoftwareSerial.h>
+SoftwareSerial mySerial(PIN_KEY_CLK, PIN_KEY_DATA);
+#endif
 
 //#include <Wire.h> // Enable this line if using Arduino Uno, Mega, etc.
 #include <Adafruit_GFX.h>
 #include "Adafruit_LEDBackpack.h"
 
-PS2Keyboard keyboard;
 Adafruit_7segment display = Adafruit_7segment();
 
 #define cnt_sec(s) (s%60)
@@ -44,7 +51,12 @@ void setup()
 {
     //Pripravime periferie
     Serial.begin(9600);
+    Serial.println("zaciname");
+#ifndef SOFTSERIAL
     keyboard.begin(PIN_KEY_DATA, PIN_KEY_CLK);
+#else
+    mySerial.begin(4800);
+#endif
     display.begin(ADDR_DISPLAY);
 
     pinMode(PIN_LED_R, OUTPUT);
@@ -55,6 +67,60 @@ void setup()
     pinMode(PIN_CABLE_CHECK, INPUT_PULLUP);
 }
 
+int test_disarmed(int reset)
+{
+
+    static int guessed = 0, disarmed = 0;
+    if (reset) {
+       guessed = 0;
+       disarmed = 0;
+    }
+    //Cteme klavesnici
+#ifndef SOFTSERIAL
+    while (keyboard.available()) {
+           char c = keyboard.read();
+      //Serial.print("Klavesa: ");
+      //Serial.println(c);
+#else
+    while (mySerial.available()) {
+      char c = mySerial.read();
+#endif
+#ifndef SOFTSERIAL
+       if (c == PS2_ENTER) {
+           Serial.println();
+       } else {
+#else
+  if(1) {    
+#endif
+           Serial.print(c);
+       }
+
+  if (c < '0' || c > '9') {
+     Serial.println("IGNORED!");
+    continue;
+    
+    }
+       if (c == DISARM_CODE[guessed]) {
+           guessed++;
+           Serial.println("\nGOT!");
+       } else {
+           guessed = 0;
+           Serial.println("\nFAIL!");
+      if (c == DISARM_CODE[guessed]) {
+        guessed++;
+        Serial.println("\nGOT!");
+      }
+       }
+       if (DISARM_CODE[guessed] == 0) {
+           disarmed = 1;
+           Serial.println("\nDISARMED!");
+       }
+    }
+
+    return disarmed;
+
+}
+
 void loop()
 {
     //Vynulujem periferie
@@ -68,8 +134,14 @@ void loop()
     display.setBrightness(15);
     display.writeDisplay();
 
+#ifndef SOFTSERIAL
     while (keyboard.available())
        keyboard.read();
+#else
+    while (mySerial.available())
+  mySerial.read();
+#endif
+    test_disarmed(1);
 
     //Pockame na pripojeni kabelu
     display.print(0xCAB1, HEX);
@@ -84,9 +156,7 @@ void loop()
        delay(100);
 
     //Odpocitavame
-    int guessed = 0, disarmed = 0;
-
-    for (int cnt = TIME_COUNTDOWN; cnt >= 0 && !disarmed; cnt--) {
+    for (int cnt = TIME_COUNTDOWN; cnt >= 0 && !test_disarmed(0); cnt--) {
        display.print(cnt_dec(cnt), DEC);
        display.drawColon((cnt + 1) % 2);
        display.writeDisplay();
@@ -95,70 +165,55 @@ void loop()
        analogWrite(PIN_LED_G, random(0, 3));
        analogWrite(PIN_LED_B, random(0, 3));
 
-       tone(PIN_COIL_LO, 1000, 30);
+       //Tikani / pipani
+       tone(PIN_COIL_LO, 1000, 50);
+       delay(60);
+       digitalWrite(PIN_COIL_LO, HIGH);
+       delay(40);
+       digitalWrite(PIN_COIL_LO, LOW);
 
-       delay(1000);
+       delay(900);
 
-       //Cteme klavesnici
-       while (keyboard.available()) {
-           char c = keyboard.read();
-           if (c == PS2_ENTER) {
-               Serial.println();
-           } else {
-               Serial.print(c);
-           }
-           if (c == DISARM_CODE[guessed]) {
-               guessed++;
-               Serial.println("\nGOT!");
-           } else {
-               guessed = 0;
-               Serial.println("\nFAIL!");
-           }
-           if (DISARM_CODE[guessed] == 0) {
-               disarmed = 1;
-               Serial.println("\nDISARMED!");
-           }
-       }
     }
 
-    if (disarmed) {
-       //Bomba byla deaktivovana
-       tone(PIN_COIL_LO, 3000, 500);
-       delay(500);
-       tone(PIN_COIL_LO, 5000, 500);
-       analogWrite(PIN_LED_R, 0);
-       analogWrite(PIN_LED_G, 3);
-       analogWrite(PIN_LED_B, 0);
-       display.print(0xDEFD, HEX);
-       display.writeDisplay();
-       delay(1000);
 
-    }
+    while (!test_disarmed(0)) {
+       //Bomba vybouchla
+       int i;
 
-    char s = digitalRead(PIN_CABLE_CHECK);
-    while (digitalRead(PIN_CABLE_CHECK) != s) {
+       analogWrite(PIN_LED_R, random(0, 255));
+       analogWrite(PIN_LED_G, random(0, 0));
+       analogWrite(PIN_LED_B, random(0, 255));
 
-       if (!disarmed) {
+       for (i = 0; i < 10; i++)
+           display.writeDigitRaw(i, random(0, 255));
+       display.setBrightness(random(0, 15));
+       display.writeDisplay();
 
-           //Bomba vybouchla
-           int i;
+       digitalWrite(PIN_COIL_HI, HIGH);
+       delay(20);
+       digitalWrite(PIN_COIL_HI, LOW);
+       int rnd = random(30, 100);
+       tone(PIN_COIL_LO, random(100, 8000), rnd);
+       delay(rnd);
+    }
 
-           analogWrite(PIN_LED_R, random(0, 255));
-           analogWrite(PIN_LED_G, random(0, 0));
-           analogWrite(PIN_LED_B, random(0, 255));
 
-           for (i = 0; i < 10; i++)
-               display.writeDigitRaw(i, random(0, 255));
-           display.setBrightness(random(0, 15));
-           display.writeDisplay();
+    //Bomba byla deaktivovana
+    tone(PIN_COIL_LO, 3000, 500);
+    delay(500);
+    tone(PIN_COIL_LO, 5000, 500);
+    analogWrite(PIN_LED_R, 0);
+    analogWrite(PIN_LED_G, 0);
+    analogWrite(PIN_LED_B, 0);
+    display.print(0xDEFD, HEX);
+    display.writeDisplay();
+    delay(1000);
 
-           digitalWrite(PIN_COIL_HI, HIGH);
-           delay(20);
-           digitalWrite(PIN_COIL_HI, LOW);
-           int rnd = random(30, 100);
-           tone(PIN_COIL_LO, random(100, 8000), rnd);
-           delay(rnd);
-       }
-    }
+
+    //Pockame na zmenu kabelu
+    char s = digitalRead(PIN_CABLE_CHECK);
+    while (digitalRead(PIN_CABLE_CHECK) == s)
+       delay(100);
 
 }
This page took 0.150334 seconds and 4 git commands to generate.