First of all, apologies for being a rookie. Gotta start somewhere, right?
I'm predicting a potential bug here, while I haven't tested it yet.
I'm using flag and !flag to avoid the humidifier being constantly turned on or off. I'm also using a lightsensor to turn it off at night, see the while-loop at the top of the loop. So if the humidifier is running, and the lights turn off, the humidifier will turn off. The problem then is obviously the flag, so if the relative humidity is low when the lights turn on again, it won't turn on the humidifier again, will it?
Is there a way to "reset" flag, or make it undefined? When I boot the Arduino it seems to me flag is both flag and !flag at the same time, since either if's can be true the first time.
Hope this makes sense.
I was thinking I could use an if("humidifierON"), is there a way to derive whether digitalWrite(humidifier) is HIGH or LOW?
#include "Adafruit_SHT4x.h"
#include <GyverOLED.h>
GyverOLED<SSH1106_128x64> oled;
int MaxRH = 70; // Set MAX RH
int lightsensor;
int sensorpin = 2;
int humidifier = 9;
float Temp;
float RH;
double VPsat;
double VPair;
double pot1;
double dVPD;
double VPD;
int flag;
double buffer = 0.14;
double desiredVPD;
Adafruit_SHT4x sht4 = Adafruit_SHT4x();
void setup() {
Serial.begin(9600);
pinMode(sensorpin, INPUT);
oled.init();
while (!Serial) {
delay(10); // will pause Zero, Leonardo, etc until serial console opens
Serial.println("Adafruit SHT4x test");
}
if (! sht4.begin()) {
Serial.println("Couldn't find SHT4x");
while (1) delay(1);
}
Serial.println("Found SHT4x sensor");
Serial.print("Serial number 0x");
Serial.println(sht4.readSerial(), HEX);
// You can have 3 different precisions, higher precision takes longer
sht4.setPrecision(SHT4X_HIGH_PRECISION);
switch (sht4.getPrecision()) {
case SHT4X_HIGH_PRECISION:
Serial.println("High precision");
break;
case SHT4X_MED_PRECISION:
Serial.println("Med precision");
break;
case SHT4X_LOW_PRECISION:
Serial.println("Low precision");
break;
}
//You can have 6 different heater settings
//higher heat and longer times uses more power
//and reads will take longer too!
sht4.setHeater(SHT4X_NO_HEATER);
switch (sht4.getHeater()) {
case SHT4X_NO_HEATER:
Serial.println("No heater");
break;
case SHT4X_HIGH_HEATER_1S:
Serial.println("High heat for 1 second");
break;
case SHT4X_HIGH_HEATER_100MS:
Serial.println("High heat for 0.1 second");
break;
case SHT4X_MED_HEATER_1S:
Serial.println("Medium heat for 1 second");
break;
case SHT4X_MED_HEATER_100MS:
Serial.println("Medium heat for 0.1 second");
break;
case SHT4X_LOW_HEATER_1S:
Serial.println("Low heat for 1 second");
break;
case SHT4X_LOW_HEATER_100MS:
Serial.println("Low heat for 0.1 second");
break;
}
pinMode(humidifier, OUTPUT);
digitalWrite(humidifier, HIGH); //Debug humidifier
}
void loop() {
lightsensor = digitalRead(sensorpin);
while(lightsensor == 1) {
lightsensor = digitalRead(sensorpin);
OLEDOff();
digitalWrite(humidifier, HIGH); //Humidifier OFF
delay(1000);
}
sensors_event_t humidity, temp;
uint32_t timestamp = millis();
sht4.getEvent(&humidity, &temp); // populate temp and humidity objects with fresh data
timestamp = millis() - timestamp;
Temp = temp.temperature;
RH = humidity.relative_humidity;
pot1 = analogRead(A0);
dVPD = map(pot1, 0, 1000, 80, 160);
desiredVPD = dVPD / 100;
//MaxRH = map(analogRead(A1), 0, 1000, 55, 80); // MaxRH Via potmeter
VPsat = (610.7 * pow(10, (7.5*Temp)/(237.3+Temp)) / 1000);
VPair = (610.7 * pow(10, (7.5*Temp)/(237.3+Temp)) / 1000) * (RH / 100);
VPD = VPsat - VPair;
Serial.print("Temp: "); Serial.print(Temp); Serial.print("C");
Serial.print(" ");
Serial.print("RH: "); Serial.print(RH); Serial.print("%");
Serial.print(" ");
Serial.print("VPD: "); Serial.print(VPD); Serial.print("kPa");
Serial.print(" ");
Serial.print("desiredVPD: "); Serial.print(desiredVPD); Serial.println("kPa");
if(VPD < (desiredVPD - buffer) && flag || RH >= MaxRH && flag) {
digitalWrite(humidifier, HIGH); //Humidifier OFF
flag = !flag;
//Serial.println("Humidifier OFF");
}
else if(VPD >= desiredVPD && !flag && RH < MaxRH){
digitalWrite(humidifier, LOW); //Humidifier ON
flag = !flag;
//Serial.println("Humidifier ON");
}
//if(VPD <= desiredVPD - buffer && flag2) {
//digitalWrite(12, HIGH); //Dehumidifier ON
//flag2 = !flag2;
//}
//else if(VPD > desiredVPD && !flag2) {
//digitalWrite(12, LOW); //Demuhidifier OFF
//flag2 = !flag2;
//}
OLEDMenu();
}