firmware wip - now fading really works, renamed some stuff

This commit is contained in:
Marcus 2024-04-30 02:31:55 +02:00
parent eb0f91e313
commit f0106cbb9b
1 changed files with 82 additions and 44 deletions

View File

@ -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<select id='output' name='output' required>\n";
body += "<option disabled value='' selected hidden>---</option>\n";
body += "<option value='1'>LED</option>\n";
body += "<option value='2'>PUMP</option>\n";
body += "<option value='3'>FAN</option>\n";
body += "<option value='2'>FAN</option>\n";
body += "<option value='3'>PUMP</option>\n";
body += "</select><br>";
body += "On/Off: <select id='state' name='state' required>\n";
@ -1517,9 +1552,7 @@ void WEBroot() {
body += "</select><br>\n";
if(UseFANrelais == false) {
body += "Speed FAN: <input type='range' id='PINfanPWM' name='PINfanPWM' min='1' max='255' value='";
body += PINfanPWM;
body += "'/><br>\n";
body += "Intensity: <input type='range' id='OutputPWM' name='OutputPWM' min='1' max='255' value='255'/><br>\n";
}
body += "<input type='submit' value='Save'>\n";
@ -1776,7 +1809,7 @@ void WEBgrowSettings() {
body += "<option value='0'" + returnStrSelected(SunFade, 0) + ">No</option>\n";
body += "</select><br>\n";
body += "Fading duration (minutes): <input type='number' name='SunFadeDuration' min='0' max='255' value='";
body += "Fading duration (minutes): <input type='number' name='SunFadeDuration' min='1' max='255' value='";
body += SunFadeDuration;
body+= "' required><br>\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");
}