Implement safe cooldown for both fans

This commit is contained in:
Fisch 2017-07-19 00:14:42 +02:00
parent d599ebb4d7
commit 8a8c28a6f0
2 changed files with 152 additions and 92 deletions

View File

@ -1,4 +1,4 @@
//#define PRINTER //uncomment for 3d-Printer (cooldown functions) #define PRINTER //uncomment for 3d-Printer (cooldown functions)
#include <SPI.h> #include <SPI.h>
#include <LiquidCrystal.h> #include <LiquidCrystal.h>
@ -6,9 +6,13 @@
#include <MFRC522.h> #include <MFRC522.h>
#ifdef PRINTER #ifdef PRINTER
#define BTN_FANPROBE A3 #define BTN_FANPROBE_R A3
#define FANOFFVALUE 50 //adc value for fan #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 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 #endif
#define BTN_PIN A0 // lcd buttons are Voltage-Divider analog #define BTN_PIN A0 // lcd buttons are Voltage-Divider analog
@ -40,6 +44,7 @@ long lastActionMillis = 0;
boolean unlocked = false; boolean unlocked = false;
boolean outputOn = false; boolean outputOn = false;
boolean lcdbacklight = false;
int displayLogIndex = -1; int displayLogIndex = -1;
int displayLogIndexLast = -1; int displayLogIndexLast = -1;
@ -129,7 +134,9 @@ void lcd_print_home() {
void handleCards(long currentMillis) { void handleCards(long currentMillis) {
if(mfrc522.PICC_IsNewCardPresent() && mfrc522.PICC_ReadCardSerial()) { if(mfrc522.PICC_IsNewCardPresent() && mfrc522.PICC_ReadCardSerial()) {
mfrc522.PICC_HaltA(); mfrc522.PICC_HaltA();
cardWasPresent = true; cardWasPresent = true;
@ -141,84 +148,88 @@ void handleCards(long currentMillis) {
int cardIndex = check_uid(&(mfrc522.uid)); int cardIndex = check_uid(&(mfrc522.uid));
if(cardIndex == 0) { // card is master #ifdef PRINTER
lcd.setCursor(0, 1); if (flag_switchOff && cardIndex>=0){
abortSwitchoff(); //abort automatic switchoff
if(unlocked) { }else{
unlocked = false; #endif
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);
}
if(cardIndex == 0) { // card is master
lcd.setCursor(0, 1); lcd.setCursor(0, 1);
lcd.print("Ausgang: ");
lcd.print(outputOn ? "AN": "AUS"); if(unlocked) {
} unlocked = false;
lcd.print("Gesperrt");
} else { // card is neither master nor slave } else {
lcd.print("Entsperrt");
if(unlocked) { unlocked = true;
if(save_client_uid(&(mfrc522.uid))) { }
} 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.clear();
lcd.print("Hinzugefuegt"); lcd.print("Karte wurde");
lcd.setCursor(0, 1); lcd.setCursor(0, 1);
lcd.print(""); lcd.print("geloescht");
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 { } else {
lcd.clear(); // if we are locked, we switch out Output
lcd.print("Fehler beim");
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.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() { void loop() {
long currentMillis = millis(); long currentMillis = millis();
handleCards(currentMillis); 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 if(readButtons() == 1 && !cardsshow) { //select button shows log, if not showing cards atm
displayLogIndex++; displayLogIndex++;
@ -304,7 +356,7 @@ void loop() {
} }
// timeout handling for returning to locked state // timeout handling for returning to locked state
if(currentMillis - lastActionMillis > 30000) { if(unlocked && currentMillis - lastActionMillis > 30000) {
unlocked = false; unlocked = false;
displayLogIndex = -1; displayLogIndex = -1;
displayCardsIndex = -1; displayCardsIndex = -1;
@ -316,28 +368,14 @@ void loop() {
lcd_print_home(); 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 #ifdef PRINTER
bool fanActive() bool fanActive()
{ {
int fanvalue=analogRead(BTN_FANPROBE); int fanvalue_r=analogRead(BTN_FANPROBE_R);
Serial.println(fanvalue); int fanvalue_l=analogRead(BTN_FANPROBE_L);
if (fanvalue<FANOFFVALUE){ if (fanvalue_l>FANOFFVALUE && fanvalue_r>FANOFFVALUE){ //fan value high = fan off
return false; return false;
}else{ }else{
return true; return true;
@ -345,11 +383,27 @@ bool fanActive()
} }
void abortSwitchoff(){ 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; flag_switchOff=false;
lcd.clear(); lcd.clear();
lcd.print("Ausschalten"); lcd.print("SCHNELLER");
lcd.setCursor(0, 1); lcd.setCursor(0, 1);
lcd.print("Abgebrochen"); lcd.print("NEUSTART");
set_output(false);
delay(500);
set_output(true);
} }
#endif #endif

View File

@ -30,12 +30,18 @@ void set_output(boolean on) {
// switch the LDC Backlight on/off // switch the LDC Backlight on/off
void lcd_backlight(boolean on) { void lcd_backlight(boolean on) {
if(on) { if(on) {
lcdbacklight=true;
digitalWrite(LCD_BACKLIGHT, HIGH); digitalWrite(LCD_BACKLIGHT, HIGH);
} else { } else {
lcdbacklight=false;
digitalWrite(LCD_BACKLIGHT, LOW); digitalWrite(LCD_BACKLIGHT, LOW);
} }
} }
bool get_lcd_backlight(){
return lcdbacklight;
}
// print a Mifare UID at current LDC position // print a Mifare UID at current LDC position
void lcd_print_uid(MFRC522::Uid *uid) { void lcd_print_uid(MFRC522::Uid *uid) {
for (byte i=0; i < uid->size; i++) { for (byte i=0; i < uid->size; i++) {