From ae72fe61887b46ed4881777a13816a45c662b469 Mon Sep 17 00:00:00 2001 From: Marcus Date: Mon, 29 Apr 2024 01:32:17 +0200 Subject: [PATCH] firmware wip - swap PINfan and PINpump, according to PCB MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit set analogWriteFreq to 220Hz. This was so far the sweetspot for Fan PWM control in combination with a 47µF electrolyt capacitor. If I had a PWM pin free, i would use PWM functionality of pc fans but all pins are in use. So I went with putting a capacitor to the output and regulate the fan speed this way. --- Arduino/CanGrow/CanGrow.ino | 92 +++++++++++++++++++++++++++++++------ 1 file changed, 78 insertions(+), 14 deletions(-) diff --git a/Arduino/CanGrow/CanGrow.ino b/Arduino/CanGrow/CanGrow.ino index c79a57f..9126ca6 100644 --- a/Arduino/CanGrow/CanGrow.ino +++ b/Arduino/CanGrow/CanGrow.ino @@ -707,8 +707,10 @@ bool loadEEPROM() { * * -- afterwards added, need to sort -- * - * 213 UseLEDrelais + * 213 PINled_PWM * 214 TemperatureSensor_Type + * 215 UseFANrelais + * 216 PINfan_PWM * */ @@ -768,7 +770,13 @@ bool loadEEPROM() { // I forgot to add TemperatureSensor_Type to EEPROM and noticed it // quite late in the process of programming. So this value residents // in 214 and not directly after UseLEDrelais + EEPROM.get(213, PINled_PWM); + // size is 1 byte EEPROM.get(214, TemperatureSensor_Type); + // size is 1 byte + EEPROM.get(215, UseFANrelais); + // size is 1 byte + EEPROM.get(216, PINfan_PWM); } // TODO auth does not work atm // EEPROM.get(160, WebUiUsername); @@ -798,14 +806,6 @@ bool loadEEPROM() { // size is 1 byte EEPROM.get(212, DayOfGrow); - // to ensure PINled_PWM always set to 255 when UseLEDrelais is true - // we set it here again, does not matter whats stored. - // size is 1 byte - if(UseLEDrelais == true) { - PINled_PWM = 255; - } else { - EEPROM.get(213, PINled_PWM); - } } @@ -837,6 +837,8 @@ bool loadEEPROM() { Serial.println(ntpOffset); Serial.print("UseLEDrelais: "); Serial.println(UseLEDrelais); + Serial.print("UseFANrelais: "); + Serial.println(UseFANrelais); Serial.println("---- Grow values ----"); Serial.print("GrowName: "); @@ -859,6 +861,8 @@ bool loadEEPROM() { Serial.println(DayOfGrow); Serial.print("PINled_PWM: "); Serial.println(PINled_PWM); + Serial.print("PINfan_PWM: "); + Serial.println(PINfan_PWM); } else { @@ -972,6 +976,9 @@ void setup() { digitalWrite(PINled, LOW); digitalWrite(PINpump, LOW); + // set frequency lower to avoid annoying noises + analogWriteFreq(220); + // Start Serial Serial.begin(115200); @@ -1438,8 +1445,14 @@ void WEBroot() { body += "\n"; body += "\n"; body += "\n"; - body += "
\n"; + + if(UseFANrelais == false) { + body += "Speed FAN:
\n"; + } + body += "\n"; body += ""; @@ -1568,6 +1581,13 @@ void WEBsystemSettings() { body += "\n"; body += "\n"; body += "
\n"; + + // UseFANrelais bool + body += "Use relais for FAN:
\n"; // TODO ugly. can this done be better? // PumpOnTime int @@ -1687,6 +1707,12 @@ void WEBgrowSettings() { body += "'/>
\n"; } + if(UseFANrelais == false) { + body += "Speed FAN:
\n"; + } + body += "\n"; body += "\n"; body += FPSTR(JSconvertDateToEpoch); @@ -1713,6 +1739,14 @@ void POSTgrowSettings() { PINled_PWM = webserver.arg("PINled_PWM").toInt(); } + if(UseFANrelais == true) { + // if a relais is used to turn on grow light, we force PWM to max val + PINfan_PWM = 255; + } else { + // otherwise just do PWM + PINfan_PWM = webserver.arg("PINfan_PWM").toInt(); + } + String GrowName_tmp = webserver.arg("GrowName"); GrowName_tmp.toCharArray(GrowName, 32); @@ -1743,6 +1777,9 @@ void POSTgrowSettings() { EEPROM.put(211, SunriseMinute); // size is 1 byte EEPROM.put(213, PINled_PWM); + // size is 1 byte + EEPROM.put(216, PINfan_PWM); + EEPROM.commit(); @@ -1771,6 +1808,8 @@ void POSTgrowSettings() { Serial.println(SunriseMinute); Serial.print("PINled_PWM: "); Serial.println(PINled_PWM); + Serial.print("PINfan_PWM: "); + Serial.println(PINfan_PWM); webserver.sendHeader("Location", String("/growSettings?success"), true); webserver.send(302, "text/plain", "growSettings/save: success!\n"); @@ -1785,6 +1824,7 @@ void POSTsystemSettings() { PumpOnTime = webserver.arg("PumpOnTime").toInt(); UseFan = webserver.arg("UseFan").toInt(); UseLEDrelais = webserver.arg("UseLEDrelais").toInt(); + UseFANrelais = webserver.arg("UseFANrelais").toInt(); TemperatureSensor_Type = webserver.arg("TemperatureSensor_Type").toInt(); configured = true; @@ -1807,6 +1847,8 @@ void POSTsystemSettings() { EEPROM.put(169, UseLEDrelais); // size is 1 byte EEPROM.put(214, TemperatureSensor_Type); + // size is 1 byte + EEPROM.put(215, UseFANrelais); // write data to EEPROM EEPROM.commit(); @@ -1827,6 +1869,13 @@ void POSTsystemSettings() { EEPROM.commit(); Serial.println("UseLEDrelais is 1, forcing PINled_PWM to max to prevent relais damage"); } + + if(UseFANrelais == true) { + PINfan_PWM = 255; + EEPROM.put(215, PINfan_PWM); + EEPROM.commit(); + Serial.println("UseFANrelais is 1, forcing PINfan_PWM to max to prevent relais damage"); + } Serial.print("configured: "); Serial.println(configured); @@ -1844,6 +1893,8 @@ void POSTsystemSettings() { Serial.println(ntpOffset); Serial.print("UseLEDrelais: "); Serial.println(UseLEDrelais); + Serial.print("UseFANrelais: "); + Serial.println(UseFANrelais); Serial.print("TemperatureSensor_Type: "); Serial.println(TemperatureSensor_Type); @@ -1922,6 +1973,7 @@ void POSTwifiSettings() { void POSTswitchMOSFET() { byte MosfetState = webserver.arg("state").toInt(); byte MosfetNr = webserver.arg("output").toInt(); + byte PINfan_PWM = webserver.arg("PINfan_PWM").toInt(); Serial.println(":: GETswitchMOSFET ::"); Serial.print("MosfetState: "); @@ -1934,8 +1986,12 @@ void POSTswitchMOSFET() { } else { switch(MosfetNr) { case 1: - if( MosfetState == 1) { - analogWrite(PINled, PINled_PWM); + if(MosfetState == 1) { + if(UseLEDrelais == true) { + digitalWrite(PINled, MosfetState); + } else { + analogWrite(PINled, PINled_PWM); + } } else { digitalWrite(PINled, MosfetState); } @@ -1944,7 +2000,15 @@ void POSTswitchMOSFET() { digitalWrite(PINpump, MosfetState); break; case 3: - digitalWrite(PINfan, MosfetState); + if(MosfetState == 1) { + if(UseFANrelais == true) { + digitalWrite(PINfan, MosfetState); + } else { + analogWrite(PINfan, PINfan_PWM); + } + } else { + digitalWrite(PINfan, MosfetState); + } break; default: webserver.send(400, "text/plain", "not valid\n"); @@ -1996,7 +2060,7 @@ void APIgetSensors() { * - PWM for fan control * - avoid using javascript to set the GrowName in the Menu and the time * as well - * - + * - rework UseLEDrelais usage * * */