#include #include #define clkPin 2 #define dtPin 3 #define btnPin 4 #define swPin 5 //prepinac #define otresPin 6 #define pirPin 7 #define rLEDPin 9 #define gLEDPin 10 #define bLEDPin 11 LiquidCrystal_I2C lcd(0x27, 16, 2); //bool rezimOld; //bool rezim; bool noc = true; bool defValue; bool otevreno = false; bool alarm = false; void setup() { pinMode(swPin, INPUT_PULLUP); pinMode(btnPin, INPUT_PULLUP); pinMode(otresPin, INPUT_PULLUP); pinMode(clkPin, INPUT); pinMode(dtPin, INPUT); pinMode(pirPin, INPUT); pinMode(rLEDPin, OUTPUT); pinMode(gLEDPin, OUTPUT); pinMode(bLEDPin, OUTPUT); Serial.begin(9600); Serial.println("System pro ovladani trezoru byl aktivovan"); lcd.begin(); lcd.clear(); lcd.backlight(); lcd.home(); lcd.setCursor(0, 0); lcd.print("Nocni rezim"); defValue = digitalRead(swPin); } void loop() { NactiVstup(); ZpracujSerial(); if (!noc && !otevreno) { NactiEnkoder(); } Lcd(); RGB(); } unsigned long rgbLastBlink = millis(); bool rgbState = false; void RGB() { if (!noc && !otevreno && !alarm) { SetRGB(0, 255, 0); } else if(!noc && otevreno) { if((millis()-rgbLastBlink)>500) { if(rgbState) { SetRGB(0, 255, 0); } else { SetRGB(0, 0, 0); } rgbLastBlink = millis(); rgbState = !rgbState; } } else if(!noc && alarm) { if((millis()-rgbLastBlink)>500) { if(rgbState) { SetRGB(255, 0, 0); } else { SetRGB(0, 0, 0); } rgbLastBlink = millis(); rgbState = !rgbState; } } else if (noc && !alarm) { SetRGB(0, 0, 255); } else if(noc && alarm) { if((millis()-rgbLastBlink)>500) { if(rgbState) { SetRGB(255, 255, 0); } else { SetRGB(0, 0, 0); } rgbLastBlink = millis(); rgbState = !rgbState; } } } bool swValue = defValue; bool swValueOld = defValue; bool btnValue; bool otresValue; unsigned long btnPressStart = millis(); void NactiVstup() { swValue = digitalRead(swPin); btnValue = digitalRead(btnPin); if (swValue != swValueOld) { PrepniRezim(); swValueOld = swValue; } if (otevreno) { if (btnValue) { btnPressStart = millis(); } if (millis() - btnPressStart > 3000) { OtevriTrezor(); //V tomto pripade slouzi k zavreni } } if (!otevreno) { otresValue = digitalRead(otresPin); if (otresValue == 1) { Serial.println("Podezrele otresy!"); Alarm(true); } } } int kod[4]; int pocetPokusu = 0; int poziceEnkod = 0; int stavPred; int stavCLK; int cursorPosition = -1; void NactiEnkoder() { if (btnValue == 0) { lcd.blink(); delay(300); if (cursorPosition > -1) { lcd.setCursor(cursorPosition, 1); lcd.print("*"); kod[cursorPosition] = poziceEnkod; } poziceEnkod = 0; cursorPosition++; } stavCLK = digitalRead(clkPin); if (stavCLK != stavPred) { if (digitalRead(dtPin) != stavCLK) { poziceEnkod ++; } else { poziceEnkod--; } } stavPred = stavCLK; if (poziceEnkod > 9) poziceEnkod = 9; else if (poziceEnkod < 0) poziceEnkod = 0; if (cursorPosition > 3) { lcd.noBlink(); lcd.setCursor(0, 1); lcd.print(" "); VyhodnotKod(kod); cursorPosition = -1; } if (cursorPosition > -1) { lcd.setCursor(cursorPosition, 1); lcd.print(poziceEnkod); } if (pocetPokusu > 2) { Serial.println("Chybny kod!"); pocetPokusu = 0; Alarm(true); } } void VyhodnotKod(int kod[]) { int kodInt = 1000 * kod[0] + 100 * kod[1] + 10 * kod[2] + kod[3]; if ((kodInt == 5703) || (kodInt == 6219)) { OtevriTrezor(); pocetPokusu = 0; } else if (kodInt == 3497) { if (!noc) { Alarm(false); pocetPokusu = 0; } } else pocetPokusu++; } void PrepniRezim() { if (!otevreno) { noc = !noc; Serial.print("Rezim "); Serial.println((noc) ? "NOC" : "DEN"); lcd.clear(); lcd.home(); if (!noc) { lcd.setCursor(5, 0); lcd.print((otevreno) ? "Otevreno" : "Zavreno"); lcd.setCursor(0, 1); } else { lcd.setCursor(0, 0); lcd.print("Nocni rezim"); } } } bool lcdZmena = false; bool lcdOnce = true; bool lcdScroll = false; int lcdScrollCount; unsigned long lcdLastScrollTime; void Lcd() { if (lcdZmena) { if (lcdOnce) { lcdLastScrollTime = millis(); lcdScrollCount = 0; lcd.setCursor(16, 0); lcd.print((alarm) ? "Alarm" : ((otevreno) ? "Otevreno" : "Zavreno")); lcdOnce = false; } if ((millis() - lcdLastScrollTime) > 400) { lcd.scrollDisplayLeft(); lcdLastScrollTime = millis(); lcdScrollCount++; } if (lcdScrollCount > 11) { lcdOnce = true; lcdZmena = false; lcd.clear(); lcd.home(); lcd.setCursor(5, 0); lcd.print((alarm) ? "Alarm" : ((otevreno) ? "Otevreno" : "Zavreno")); } } if (noc) { if ((millis() - lcdLastScrollTime) > 400) { lcd.scrollDisplayRight(); lcdLastScrollTime = millis(); } } } char SerialVstup = 0; void ZpracujSerial() { if (Serial.available() > 0) { SerialVstup = Serial.read(); if (SerialVstup == 'Q') { Alarm(false); Serial.println("Alarm vypnut"); } else if (SerialVstup == 'S') { Serial.print("rezim: "); Serial.print((noc) ? "NOC" : "DEN"); Serial.print(", stav: "); Serial.println((otevreno) ? "OTEVRENO" : "ZAVRENO"); } else if (SerialVstup == 'R') { PrepniRezim(); } } } void SetRGB(int rValue, int gValue, int bValue) { analogWrite(rLEDPin, rValue); analogWrite(gLEDPin, gValue); analogWrite(bLEDPin, bValue); } void OtevriTrezor() { if (!noc) { otevreno = !otevreno; lcdZmena = true; } } void Alarm(bool stav) { alarm = stav; lcdZmena = true; }