From f0106cbb9bc73c29b769dceebce8102eb3fd771b Mon Sep 17 00:00:00 2001 From: Marcus Date: Tue, 30 Apr 2024 02:31:55 +0200 Subject: [PATCH] firmware wip - now fading really works, renamed some stuff --- Arduino/CanGrow/CanGrow.ino | 126 +++++++++++++++++++++++------------- 1 file changed, 82 insertions(+), 44 deletions(-) diff --git a/Arduino/CanGrow/CanGrow.ino b/Arduino/CanGrow/CanGrow.ino index edc682c..3f55526 100644 --- a/Arduino/CanGrow/CanGrow.ino +++ b/Arduino/CanGrow/CanGrow.ino @@ -960,7 +960,7 @@ void wifiAp() { //Serial.println("The login credentials for the WebUI are 'cangrow' for username and password"); } -void setOutput(byte pin, byte pinState) { +void setOutput(byte Output, byte OutputState) { /* * Pin assignments * @@ -969,40 +969,46 @@ void setOutput(byte pin, byte pinState) { * 3 - PUMP * */ + bool UseRelais = true; + byte OutputPin; - - switch(pin) { + switch(Output) { case 1: - if(pinState > 0) { - if(UseLEDrelais == true) { - digitalWrite(PINled, HIGH); - } else { - analogWrite(PINled, PINledPWM); - } + OutputPin = PINled; + if(UseLEDrelais == true) { + UseRelais = true; } else { - digitalWrite(PINled, LOW); + UseRelais = false; } break; case 2: - if(pinState > 0) { - if(UseFANrelais == true) { - digitalWrite(PINfan, HIGH); - } else { - analogWrite(PINfan, PINledPWM); - } + OutputPin = PINfan; + if(UseFANrelais == true) { + UseRelais = true; } else { - digitalWrite(PINfan, LOW); + UseRelais = false; } break; // PUMP Pin (D0) does not support PWM, so we do not need to care about case 3: - if(pinState > 0) { - digitalWrite(PINpump, HIGH); - } else { - digitalWrite(PINpump, LOW); - } + OutputPin = PINpump; break; } + + //~ Serial.print("Output: "); + //~ Serial.println(Output); + //~ Serial.print("OutputPin: "); + //~ Serial.println(OutputPin); + //~ Serial.print("OutputState: "); + //~ Serial.println(OutputState); + //~ Serial.print("UseRelais: "); + //~ Serial.println(UseRelais); + + if( (UseRelais == true) || (OutputPin == PINpump) ) { + digitalWrite(OutputPin, OutputState); + } else { + analogWrite(OutputPin, OutputState); + } } /* @@ -1148,9 +1154,6 @@ void loop() { // do every second when everything is configured and grow is started if( (configured == true) && (strlen(GrowName) > 0) && (currentRuntime - outputPrevTime >= 1000) ){ - /* - * LED controlling - */ // calculate acutal DayOfGrow DayOfGrow = int(ceil(float((timeClient.getEpochTime() - GrowStart) / 60 / 60 / 24))); @@ -1162,31 +1165,63 @@ void loop() { lightHours = LighthoursVeg; } + + /* + * LED controlling + */ + // check if secondsToday is larger then secondsSunrise time AND if // secondsToday is smaller then the sum of secondsSunrise + seconds of lightHours - if( (SunFade == true) && ((secondsToday >= secondsSunrise) && (secondsToday <= ( secondsSunrise + (lightHours * 60 * 60))) ) ){ - - // in the first n minutes of lighting (SunFadeDuration), we want - //to raise the light slowly to prevent stress from the plant - if(secondsSunrise + SunFadeDuration * 60 >= secondsToday) { + if( ((secondsToday >= secondsSunrise) && (secondsToday <= ( secondsSunrise + (lightHours * 60 * 60))) ) ){ + Serial.println("light on time"); + + // when SunFade is true, fade LED light. Otherwise just turn on or off + if( (SunFade == true) && (UseLEDrelais == false) && (secondsSunrise + SunFadeDuration * 60 >= secondsToday) ) { + // in the first n minutes of lighting (SunFadeDuration), we want + // to raise the light slowly to prevent stress from the plant // convert progress sunrise to PWM value - PINledPWM_tmp = (SunFadeDuration * 60 - ((secondsSunrise + SunFadeDuration * 60) - secondsToday)) * 255 / (SunFadeDuration * 60); + PINledPWM_tmp = (SunFadeDuration * 60 - ((secondsSunrise + SunFadeDuration * 60) - secondsToday)) * PINledPWM / (SunFadeDuration * 60); + setOutput(1, PINledPWM_tmp); + Serial.print("sunrise PWM; "); + Serial.println(PINledPWM_tmp); - } else if( (SunFade == true) && (secondsToday >= ((secondsSunrise + lightHours * 60 * 60) - SunFadeDuration * 60) ) ) { + } else if( (SunFade == true) && (UseLEDrelais == false) && (secondsToday >= ((secondsSunrise + lightHours * 60 * 60) - SunFadeDuration * 60) ) ) { // calculate progress sunset to PWM value - PINledPWM_tmp = (secondsSunrise + (lightHours * 60 * 60) - secondsToday) * 255 / (SunFadeDuration * 60); + PINledPWM_tmp = (secondsSunrise + (lightHours * 60 * 60) - secondsToday) * PINledPWM / (SunFadeDuration * 60); + setOutput(1, PINledPWM_tmp); + Serial.print("sunset PWM: "); + Serial.println(PINledPWM_tmp); } else { + Serial.println("just turn on the light"); // no sunrise or sunset, just keep the LED turned on setOutput(1, PINledPWM); } } else { + Serial.println("good night time"); // turn off setOutput(1, 0); } // current time gets previous time for new interval + + + + /* ------------------------------- + // check if secondsToday is larger then secondsSunrise time AND if secondsToday is smaller then the sum of secondsSunrise + seconds of lightHours + if( (secondsToday >= secondsSunrise) && (secondsToday <= ( secondsSunrise + (lightHours * 60 * 60) ) )){ + // turn on light + + + // TODO write a LED control function which takes also takes care of UseLEDrelais + //analogWrite(PINled, PINledPWM); + setOutput(1, PINledPWM); + } else { + // turn off + digitalWrite(PINled, LOW); + } + ---------------------------------- */ outputPrevTime = currentRuntime; } @@ -1506,8 +1541,8 @@ void WEBroot() { body += "MOSFET
"; body += "On/Off:
\n"; if(UseFANrelais == false) { - body += "Speed FAN:
\n"; + body += "Intensity:
\n"; } body += "\n"; @@ -1776,7 +1809,7 @@ void WEBgrowSettings() { body += "\n"; body += "
\n"; - body += "Fading duration (minutes):
\n"; @@ -2055,20 +2088,25 @@ void POSTwifiSettings() { void POSTsetOutput() { byte OutputState = webserver.arg("state").toInt(); byte OutputNr = webserver.arg("output").toInt(); - PINledPWM = webserver.arg("PINledPWM").toInt(); - PINfanPWM = webserver.arg("PINfanPWM").toInt(); + //PINledPWM = webserver.arg("PINledPWM").toInt(); + byte OutputPWM = webserver.arg("OutputPWM").toInt(); - Serial.println(":: GETswitchMOSFET ::"); + Serial.println(":: POSTsetOutput ::"); Serial.print("OutputState: "); Serial.println(OutputState); Serial.print("OutputNr: "); Serial.println(OutputNr); - if((OutputNr > 3) || (OutputNr < 1) || (OutputState > 255) || (OutputState < 255)) { + if((OutputNr > 3) || (OutputNr < 1) || (OutputState > 255) || (OutputState < 0)) { webserver.send(400, "text/plain", "not valid\n"); } else { - setOutput(OutputNr, OutputState); + if(OutputState > 0){ + setOutput(OutputNr, OutputPWM); + } else { + setOutput(OutputNr, 0); + } + webserver.sendHeader("Location", String("/?success"), true); webserver.send(302, "text/plain", "switch: success!\n"); }