//Schema zapojeni je otevřené v programu Fritzing //Pro zapnutí zapojte kabel z arduina do usb portu //pokud se arduino nezapne (display zůstane prázdný), zmáčkněte červené tlačítko RESET na arduinu #include #include #define RGB_B 11 #define RGB_G 12 #define RGB_R 13 #define SWITCH 7 #define ROTARY1 38 #define ROTARY2 39 #define TILT 36 #define MOVEMENT 34 #define BUTTON 40 bool stavCas = false; bool stavClose = true; bool stavAlarm = false; bool switchLast; bool zadavani = false; short posKod = 0; short kod[4]; short cislo = 0; long oldPosition = -999; int realPos; short pokusy = 0; unsigned long lastButtonTime; unsigned long lastDetektorTime; bool lastButtonState = true; bool lastDetektorState; volatile bool detektor = false; unsigned long greenMillis; unsigned long redMillis; unsigned long yellowMillis; bool red, green, yellow; bool predchoziAlarm; char recievedChar; bool prepnout; Encoder myEnc(ROTARY2, ROTARY1); LiquidCrystal_I2C lcd(0x27, 20, 4); void setup() { pinMode(SWITCH, INPUT); pinMode(TILT, INPUT_PULLUP); pinMode(MOVEMENT, INPUT); pinMode(BUTTON, INPUT_PULLUP); pinMode(RGB_B, OUTPUT); pinMode(RGB_G, OUTPUT); pinMode(RGB_R, OUTPUT); switchLast = digitalRead(SWITCH); lcd.begin(); lcd.setCursor(5, 0); lcd.print("Nocni rezim"); attachInterrupt(MOVEMENT, movITR, RISING); Serial.begin(9600); Serial.println("System pro ovladani trezoru byl aktivovan"); //Počáteční nastavení } void loop() { if (Serial.available() > 0) { //Čtení z terminálu recievedChar = Serial.read(); if (recievedChar == 'Q'){ stavAlarm = false; Serial.println("Alarm vypnut"); if (stavCas){ lcd.clear(); lcd.setCursor(7, 0); if (stavClose) lcd.print("Zavreno"); else lcd.print("Otevreno"); } else{ lcd.clear(); lcd.setCursor(5, 0); lcd.print("Nocni rezim"); } predchoziAlarm = false; } if (recievedChar == 'S'){ Serial.print("Rezim: "); if (stavCas) Serial.println("DEN"); //Vykonání příkazů z terminálu else Serial.println("NOC"); Serial.print("Stav: "); if (stavClose) Serial.println("ZAVRENO"); else Serial.println("OTEVRENO"); } if (recievedChar == 'R') prepnout = true; } if ((switchLast != digitalRead(SWITCH) || prepnout) && !stavAlarm){ //Přepínaní mezi dnem a nocí prepnout = false; if (stavCas){ if (stavClose){ stavCas = false; zadavani = false; Serial.println("Rezim NOC"); lcd.clear(); lcd.setCursor(5, 0); lcd.print("Nocni rezim"); attachInterrupt(MOVEMENT, movITR, RISING); } } else{ stavCas = true; Serial.println("Rezim DEN"); lcd.clear(); lcd.setCursor(7, 0); if (stavClose) lcd.print("Zavreno"); else lcd.print("Otevreno"); detachInterrupt(MOVEMENT); } delay(10); switchLast = digitalRead(SWITCH); } if (stavCas && stavClose){ //Zadávání kódu if (!digitalRead(BUTTON)){ delay(200); if (!zadavani){ zadavani = true; lcd.blink(); lcd.setCursor(posKod, 1); lcd.print("0"); myEnc.write(0); } else{ kod[posKod] = realPos; lcd.setCursor(posKod, 1); lcd.print("*"); posKod++; lcd.setCursor(posKod, 1); lcd.print("0"); myEnc.write(0); if (posKod > 3){ zadavani = false; lcd.clear(); lcd.setCursor(7, 0); if(stavAlarm){ //vypnutí alarmu kódem lcd.print("Alarm"); if ((kod[0] == 3 && kod[1] == 4 && kod[2] == 9 && kod[3] == 7)){ stavAlarm = false; Serial.println("Alarm vypnut"); lcd.noBlink(); if (stavCas){ lcd.clear(); lcd.setCursor(7, 0); if (stavClose) lcd.print("Zavreno"); else lcd.print("Otevreno"); } else{ lcd.clear(); lcd.setCursor(5, 0); lcd.print("Nocni rezim"); } predchoziAlarm = false; } } else{ if ((kod[0] == 5 && kod[1] == 7 && kod[2] == 0 && kod[3] == 3) || (kod[0] == 6 && kod[1] == 2 && kod[2] == 1 && kod[3] == 9)){ //otevření trezoru kódem stavClose = false; pokusy = 0; lcd.print("Otevreno"); } else{ pokusy++; //kontrola počtu chybných pokusů if (pokusy == 3){ stavAlarm = true; Serial.println("Chybny kod"); pokusy = 0; } lcd.print("Zavreno"); }} posKod = 0; lcd.noBlink(); } } } if (zadavani){ //Zjištění pozice enkodéru long newPosition = myEnc.read(); if (newPosition != oldPosition){ oldPosition = newPosition; realPos = newPosition / 4; while (realPos > 9) realPos -= 10; while (realPos < 0) realPos += 10; lcd.setCursor(posKod, 1); lcd.print(realPos); } } } if (!stavClose && !stavAlarm){ //Uzavírání trezoru if (!digitalRead(BUTTON)){ if (lastButtonState == true){ lastButtonTime = millis(); lastButtonState = false; } if (millis() - lastButtonTime > 3000 && lastButtonState == false){ stavClose = true; lcd.setCursor(7, 0); lcd.print("Zavreno"); delay(250); } } else lastButtonState = true; } if (stavClose && !stavAlarm){ //Kontrola čidla otřesů if (digitalRead(TILT)){ stavAlarm = true; Serial.println("Podezrele otresy!"); } } if (!stavCas && !stavAlarm){ //kontrola čidla pohybu if (detektor){ Serial.println("Podezrely pohyb"); if (lastDetektorState == false){ lastDetektorTime = millis(); lastDetektorState = true; } if (millis() - lastDetektorTime > 3000){ lastDetektorState = false; stavAlarm = true; } detektor = false; } if (millis() - lastDetektorTime > 3000){ lastDetektorState = false; } } if (stavCas){ //Ovládání RGB LED diody if (stavAlarm){ if (millis() - redMillis > 500){ if (red){ red = false; setColor(0, 0, 0); } else{ red = true; setColor(255, 0, 0); } redMillis = millis(); } goto con; } if (stavClose) setColor(0, 255, 0); else { if (millis() - greenMillis > 500){ if (green){ green = false; setColor(0, 0, 0); } else{ green = true; setColor(0, 255, 0); } greenMillis = millis(); } } } else{ if (stavAlarm){ if (millis() - yellowMillis > 500){ if (yellow){ yellow = false; setColor(0, 0, 0); } else{ yellow = true; setColor(255, 255, 0); } yellowMillis = millis(); } goto con; } if (lastDetektorState) setColor(255, 255, 0); else setColor(0, 0, 255); } con: if (stavAlarm){ //Kontrola změny stavu alarmu if(!predchoziAlarm){ predchoziAlarm = true; if (stavCas) { lcd.clear(); lcd.setCursor(7, 0); lcd.print("Alarm"); } } } } void movITR(){ //interrupt pro detektor pohybu detektor = true; } void setColor(short R, short G, short B){ //funkce pro nastavení barvy RGB LED diody analogWrite(RGB_R, R); analogWrite(RGB_G, G); analogWrite(RGB_B, B); }