diff --git a/T11_RFID_Tutorial_Sketch/T11_RFID_Tutorial_Sketch.ino b/T11_RFID_Tutorial_Sketch/T11_RFID_Tutorial_Sketch.ino index 939d962..864a8e1 100644 --- a/T11_RFID_Tutorial_Sketch/T11_RFID_Tutorial_Sketch.ino +++ b/T11_RFID_Tutorial_Sketch/T11_RFID_Tutorial_Sketch.ino @@ -1,4 +1,4 @@ -//#define PRINTER //uncomment for 3d-Printer (cooldown functions) +#define PRINTER //uncomment for 3d-Printer (cooldown functions) #include #include @@ -6,9 +6,13 @@ #include #ifdef PRINTER - #define BTN_FANPROBE A3 - #define FANOFFVALUE 50 //adc value for fan + #define BTN_FANPROBE_R A3 + #define BTN_FANPROBE_L A2 + #define FANOFFVALUE 512 //adc value for fan. high value = fan off boolean flag_switchOff = false; //set true-> loop switches output off if possible + #define SWITCHOFFTRYBLINKDELAY 500 //blink period time in millis + long lastSwitchOffTryMillis=0; + #define QUICKRESTARTHOLDTIME 1000 //time to hold button during fan waiting to quick restart printer #endif #define BTN_PIN A0 // lcd buttons are Voltage-Divider analog @@ -40,6 +44,7 @@ long lastActionMillis = 0; boolean unlocked = false; boolean outputOn = false; +boolean lcdbacklight = false; int displayLogIndex = -1; int displayLogIndexLast = -1; @@ -129,7 +134,9 @@ void lcd_print_home() { void handleCards(long currentMillis) { if(mfrc522.PICC_IsNewCardPresent() && mfrc522.PICC_ReadCardSerial()) { + + mfrc522.PICC_HaltA(); cardWasPresent = true; @@ -141,84 +148,88 @@ void handleCards(long currentMillis) { int cardIndex = check_uid(&(mfrc522.uid)); - if(cardIndex == 0) { // card is master - lcd.setCursor(0, 1); - - if(unlocked) { - unlocked = false; - lcd.print("Gesperrt"); - } else { - lcd.print("Entsperrt"); - unlocked = true; - } - - } else if(cardIndex > 0) { // card is client - - if(unlocked) { - // if we are unlocked and detect a client card - // we remove this card from EEPROM - - delete_uid(cardIndex, EEP_ADDR_START); - lcd.clear(); - lcd.print("Karte wurde"); - lcd.setCursor(0, 1); - lcd.print("geloescht"); - - } else { - // if we are locked, we switch out Output - - if(outputOn) { - #ifdef PRINTER - if (!flag_switchOff){ - flag_switchOff=true; - lcd.clear(); - lcd.print("Wartet auf"); - lcd.setCursor(0, 1); - lcd.print("Luefter"); - }else{ - flag_switchOff=false; //cancel switchoff - } - #else - set_output(false); - #endif - } else { - add_log(&(mfrc522.uid)); - set_output(true); - } + #ifdef PRINTER + if (flag_switchOff && cardIndex>=0){ + abortSwitchoff(); //abort automatic switchoff + }else{ + #endif + if(cardIndex == 0) { // card is master lcd.setCursor(0, 1); - lcd.print("Ausgang: "); - lcd.print(outputOn ? "AN": "AUS"); - } - - } else { // card is neither master nor slave - - if(unlocked) { - if(save_client_uid(&(mfrc522.uid))) { + + if(unlocked) { + unlocked = false; + lcd.print("Gesperrt"); + } else { + lcd.print("Entsperrt"); + unlocked = true; + } + + } else if(cardIndex > 0) { // card is client + + if(unlocked) { + // if we are unlocked and detect a client card + // we remove this card from EEPROM + + delete_uid(cardIndex, EEP_ADDR_START); lcd.clear(); - lcd.print("Hinzugefuegt"); + lcd.print("Karte wurde"); lcd.setCursor(0, 1); - lcd.print(""); - lcd_print_uid(&(mfrc522.uid)); - - delay(500); - while(!mfrc522.PICC_IsNewCardPresent() && !mfrc522.PICC_ReadCardSerial()){ - delay(50); - } - lcd.setCursor(0, 0); - lcd.print(" "); - delay(200); + lcd.print("geloescht"); } else { - lcd.clear(); - lcd.print("Fehler beim"); + // if we are locked, we switch out Output + + if(outputOn) { + #ifdef PRINTER + if (!flag_switchOff){ + flag_switchOff=true; //set flag to turn off printer + }else{ + flag_switchOff=false; //cancel switchoff + } + #else + set_output(false); + #endif + } else { + add_log(&(mfrc522.uid)); + set_output(true); + } + lcd.setCursor(0, 1); - lcd.print("hinzufuegen"); + lcd.print("Ausgang: "); + lcd.print(outputOn ? "AN": "AUS"); + } + + } else { // card is neither master nor slave + + if(unlocked) { + if(save_client_uid(&(mfrc522.uid))) { + lcd.clear(); + lcd.print("Hinzugefuegt"); + lcd.setCursor(0, 1); + lcd.print(""); + lcd_print_uid(&(mfrc522.uid)); + + delay(500); + while(!mfrc522.PICC_IsNewCardPresent() && !mfrc522.PICC_ReadCardSerial()){ + delay(50); + } + lcd.setCursor(0, 0); + lcd.print(" "); + delay(200); + + } else { + lcd.clear(); + lcd.print("Fehler beim"); + lcd.setCursor(0, 1); + lcd.print("hinzufuegen"); + } + + } else { + lcd.setCursor(0, 1); + lcd.print("Unbekannte Karte"); } - } else { - lcd.setCursor(0, 1); - lcd.print("Unbekannte Karte"); } } @@ -228,12 +239,53 @@ void handleCards(long currentMillis) { } } +long lastPrint=0; + void loop() { long currentMillis = millis(); handleCards(currentMillis); + #ifdef PRINTER + /*if (currentMillis>lastPrint+1000){ //TESTING + int fanvalue=analogRead(BTN_FANPROBE_R); + Serial.println(fanvalue); + lastPrint=currentMillis; + }*/ + if (flag_switchOff){ + + if (readButtons()!=0){ //any buttons pressed + long _buttonPressStart=millis(); + while (readButtons()==1){ //select button + delay(100); //wait for button to release + } + + if (millis()-_buttonPressStart > QUICKRESTARTHOLDTIME) { //pressed button for long time + quickRestart(); + }else{ //just pressed short + abortSwitchoff(); + } + + } + if (!fanActive()){ + set_output(false); + flag_switchOff=false; + lcd_print_home(); + }else{ //fan is active + if (lastSwitchOffTryMillis+SWITCHOFFTRYBLINKDELAY < currentMillis){ + lcd_backlight(!get_lcd_backlight()); //blink backlight + lcd.clear(); + lcd.print("Wartet auf"); + lcd.setCursor(0, 1); + lcd.print("Luefter"); + lastSwitchOffTryMillis=currentMillis; + } + } + } + #endif + + if(readButtons() == 1 && !cardsshow) { //select button shows log, if not showing cards atm displayLogIndex++; @@ -304,7 +356,7 @@ void loop() { } // timeout handling for returning to locked state - if(currentMillis - lastActionMillis > 30000) { + if(unlocked && currentMillis - lastActionMillis > 30000) { unlocked = false; displayLogIndex = -1; displayCardsIndex = -1; @@ -316,28 +368,14 @@ void loop() { lcd_print_home(); } } - - #ifdef PRINTER - int fanvalue=analogRead(BTN_FANPROBE); - Serial.println(fanvalue); - if (flag_switchOff){ - if (readButtons()!=0){ //any buttons pressed - abortSwitchoff(); - } - if (!fanActive()){ - set_output(false); - flag_switchOff=false; - } - } - #endif } #ifdef PRINTER bool fanActive() { - int fanvalue=analogRead(BTN_FANPROBE); - Serial.println(fanvalue); - if (fanvalueFANOFFVALUE && fanvalue_r>FANOFFVALUE){ //fan value high = fan off return false; }else{ return true; @@ -345,11 +383,27 @@ bool fanActive() } void abortSwitchoff(){ + lcd_backlight(true); + if (flag_switchOff){ + flag_switchOff=false; + lcd.clear(); + lcd.print("Ausschalten"); + lcd.setCursor(0, 1); + lcd.print("Abgebrochen"); + delay(500); + } +} + +void quickRestart(){ + lcd_backlight(true); flag_switchOff=false; lcd.clear(); - lcd.print("Ausschalten"); + lcd.print("SCHNELLER"); lcd.setCursor(0, 1); - lcd.print("Abgebrochen"); + lcd.print("NEUSTART"); + set_output(false); + delay(500); + set_output(true); } #endif diff --git a/T11_RFID_Tutorial_Sketch/functions.ino b/T11_RFID_Tutorial_Sketch/functions.ino index 42adbb5..f3b9847 100644 --- a/T11_RFID_Tutorial_Sketch/functions.ino +++ b/T11_RFID_Tutorial_Sketch/functions.ino @@ -30,12 +30,18 @@ void set_output(boolean on) { // switch the LDC Backlight on/off void lcd_backlight(boolean on) { if(on) { + lcdbacklight=true; digitalWrite(LCD_BACKLIGHT, HIGH); } else { + lcdbacklight=false; digitalWrite(LCD_BACKLIGHT, LOW); } } +bool get_lcd_backlight(){ + return lcdbacklight; +} + // print a Mifare UID at current LDC position void lcd_print_uid(MFRC522::Uid *uid) { for (byte i=0; i < uid->size; i++) {