| 1 | /* |
| 2 | * Exit Rum |
| 3 | * (c) Tomas 'Harvie' Mudrunka 2016 |
| 4 | */ |
| 5 | |
| 6 | #include <Wire.h> |
| 7 | #include <LiquidCrystal_I2C.h> |
| 8 | #include <TM1637Display.h> |
| 9 | |
| 10 | #define TIMEOUT 300 |
| 11 | #define BEEP_FREQ 3000 |
| 12 | #define BEEP_LEN 50 |
| 13 | |
| 14 | #define P_NASLAP 12 |
| 15 | #define P_PIEZO 35 |
| 16 | #define P_RELAY_1 9 |
| 17 | #define P_RELAY_2 8 |
| 18 | #define P_LEDSEG_CLK 2 |
| 19 | #define P_LEDSEG_DIO 3 |
| 20 | #define P_LEDSTRIP 11 |
| 21 | |
| 22 | TM1637Display display(P_LEDSEG_CLK, P_LEDSEG_DIO); |
| 23 | LiquidCrystal_I2C lcd(0x3F, 16, 2); //i2c addr, chars, lines |
| 24 | |
| 25 | #define JBASE 38 |
| 26 | #define JCOUNT 16 |
| 27 | |
| 28 | #define J_A 0 |
| 29 | #define J_B 2 |
| 30 | #define J_C 4 |
| 31 | #define J_D 6 |
| 32 | #define J_E 8 |
| 33 | #define J_F 10 |
| 34 | #define J_G 12 |
| 35 | #define J_H 14 |
| 36 | #define J_1 1 |
| 37 | #define J_2 3 |
| 38 | #define J_3 5 |
| 39 | #define J_4 7 |
| 40 | #define J_5 9 |
| 41 | #define J_6 11 |
| 42 | #define J_7 13 |
| 43 | #define J_8 15 |
| 44 | |
| 45 | boolean jumpers_read[JCOUNT][JCOUNT] = {0}, jumpers_secret[JCOUNT][JCOUNT] = {0}; |
| 46 | |
| 47 | void jumper_store(int a, int b, boolean c) { |
| 48 | jumpers_secret[min(a,b)][max(a,b)]=c; |
| 49 | } |
| 50 | |
| 51 | void sound(char sndpin, float freq, float duration) { //Play bit-bang sound |
| 52 | if(duration<=0) return; if(freq<=0) { delay(duration); return; } |
| 53 | freq=((1000000/2)/freq); //Convert freq to delay (us) |
| 54 | duration*=1000; //Convert duration to us |
| 55 | pinMode(sndpin, OUTPUT); |
| 56 | for(;duration>0;duration-=2*freq) { |
| 57 | digitalWrite(sndpin, HIGH); delayMicroseconds(freq); |
| 58 | digitalWrite(sndpin, LOW); delayMicroseconds(freq); |
| 59 | } |
| 60 | pinMode(sndpin, INPUT); //Close pin to avoid noise (optional) |
| 61 | } |
| 62 | |
| 63 | int check_jumpers() { |
| 64 | int i, j, check = 0, fail = 0; |
| 65 | |
| 66 | for(i=0;i<JCOUNT;i++) { |
| 67 | pinMode(JBASE+i, OUTPUT); |
| 68 | digitalWrite(JBASE+i, LOW); |
| 69 | for(j=0;j<JCOUNT;j++) if(i<j) { |
| 70 | pinMode(JBASE+j, INPUT_PULLUP); |
| 71 | jumpers_read[i][j] = !digitalRead(JBASE+j); |
| 72 | if(jumpers_read[i][j]) { |
| 73 | check++; |
| 74 | Serial.print(JBASE+i, DEC); |
| 75 | Serial.print("->"); |
| 76 | Serial.print(JBASE+j, DEC); |
| 77 | if(jumpers_secret[i][j]) { |
| 78 | Serial.print("!"); |
| 79 | } else { |
| 80 | fail++; |
| 81 | } |
| 82 | Serial.print(", "); |
| 83 | } |
| 84 | } |
| 85 | pinMode(JBASE+i, INPUT_PULLUP); |
| 86 | } |
| 87 | Serial.println(); |
| 88 | check = fail ? -check : check; |
| 89 | Serial.println(check, DEC); |
| 90 | return check; |
| 91 | } |
| 92 | |
| 93 | void setup() { |
| 94 | jumper_store(J_A, J_5, true); |
| 95 | jumper_store(J_B, J_4, true); |
| 96 | jumper_store(J_C, J_7, true); |
| 97 | jumper_store(J_D, J_2, true); |
| 98 | jumper_store(J_E, J_6, true); |
| 99 | jumper_store(J_F, J_8, true); |
| 100 | jumper_store(J_G, J_1, true); |
| 101 | jumper_store(J_H, J_3, true); |
| 102 | |
| 103 | pinMode(P_NASLAP, INPUT_PULLUP); |
| 104 | pinMode(P_RELAY_1, OUTPUT); |
| 105 | pinMode(P_RELAY_2, OUTPUT); |
| 106 | pinMode(P_LEDSTRIP, OUTPUT); |
| 107 | |
| 108 | Serial.begin(9600); |
| 109 | lcd.begin(); |
| 110 | } |
| 111 | |
| 112 | #define seconds_display(seconds) ((((seconds)/60)*100)+((seconds)%60)) |
| 113 | |
| 114 | // the loop function runs over and over again forever |
| 115 | void loop() { |
| 116 | Serial.println("Inicializace systemu..."); |
| 117 | |
| 118 | lcd.backlight(); |
| 119 | lcd.clear(); |
| 120 | lcd.print(" (c) SPOJE.NET"); |
| 121 | lcd.setCursor(0,1); |
| 122 | lcd.print("Tomas@MudrunkaCZ"); |
| 123 | |
| 124 | display.setBrightness(0x0f); |
| 125 | display.setColon(true); |
| 126 | display.showNumberDec(2323); |
| 127 | |
| 128 | delay(5000); |
| 129 | |
| 130 | lcd.noDisplay(); |
| 131 | lcd.noBacklight(); |
| 132 | display.setBrightness(0x00); |
| 133 | |
| 134 | display.setBrightness(0); |
| 135 | display.showNumberDec(0); |
| 136 | |
| 137 | analogWrite(P_LEDSTRIP, 0); |
| 138 | digitalWrite(P_RELAY_1, HIGH); |
| 139 | digitalWrite(P_RELAY_2, HIGH); |
| 140 | |
| 141 | sound(P_PIEZO, BEEP_FREQ, BEEP_LEN); |
| 142 | delay(BEEP_LEN); |
| 143 | sound(P_PIEZO, BEEP_FREQ, BEEP_LEN); |
| 144 | Serial.println("Cekam na aktivaci..."); |
| 145 | while(digitalRead(P_NASLAP)); |
| 146 | |
| 147 | Serial.println("Sekvence aktivovana!"); |
| 148 | |
| 149 | display.setBrightness(0x0f); |
| 150 | for(int i=0;i<70;i++) { |
| 151 | sound(P_PIEZO, random(500,10000), random(10,100)); |
| 152 | display.showNumberDec(random(1000,9999)); |
| 153 | analogWrite(P_LEDSTRIP, random(0,255)); |
| 154 | } |
| 155 | sound(P_PIEZO, BEEP_FREQ, 2000); |
| 156 | |
| 157 | Serial.println("Odpocet aktivovan!"); |
| 158 | int timer = TIMEOUT; |
| 159 | while(check_jumpers() != 8) { |
| 160 | Serial.print("Zbyva: "); |
| 161 | Serial.print(timer, DEC); |
| 162 | Serial.println(" sekund"); |
| 163 | if(timer <= 0) { |
| 164 | digitalWrite(P_RELAY_1, LOW); |
| 165 | } |
| 166 | display.setColon(timer%2); |
| 167 | display.showNumberDec(seconds_display(timer)); |
| 168 | analogWrite(P_LEDSTRIP, map(timer%5, 0, 4, 255, 10)); |
| 169 | digitalWrite(P_RELAY_2, timer%2); |
| 170 | if(timer>0) { |
| 171 | timer--; |
| 172 | sound(P_PIEZO, BEEP_FREQ, BEEP_LEN); |
| 173 | delay(1000-BEEP_LEN); |
| 174 | } else { |
| 175 | analogWrite(P_LEDSTRIP, random(0,255)); |
| 176 | sound(P_PIEZO, random(500,10000), random(100,1000)); |
| 177 | } |
| 178 | } |
| 179 | |
| 180 | Serial.println("Odpocet zastaven."); |
| 181 | display.setColon(true); |
| 182 | display.showNumberDec(seconds_display(timer)); |
| 183 | analogWrite(P_LEDSTRIP, 0); |
| 184 | digitalWrite(P_RELAY_1, HIGH); |
| 185 | |
| 186 | //Muhammad Ismáil Árifi |
| 187 | lcd.clear(); |
| 188 | lcd.print(" Muhammad"); |
| 189 | lcd.setCursor(0,1); |
| 190 | lcd.print(" Ismail Arifi"); |
| 191 | lcd.backlight(); |
| 192 | lcd.display(); |
| 193 | |
| 194 | Serial.println("Cekam na reset..."); |
| 195 | //while(digitalRead(P_NASLAP)); |
| 196 | while(check_jumpers() != 0) delay(1000); |
| 197 | } |