Flag after force off

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();
}


If only you had posted the whole sketch it would have been possible to tell you the value of flag when it was defined. It certainly can't be flag and !flag at the same time

Always a pleasure, Bob.
I updated my post.

int flag;

Because it is a global variable flag will have a value of zero when it is declared but not explicitly defined but why is it not declared as a boolean ? It would make more sense as would explicitly defining its value

As for

The answer is yes, you can use digitalRead() on a pin with a pinMode() of INPUT

I was thinking internally in the software, but that's ok.
I figured it out, thanks anyway.

Set (=1) or clear (=0) this before use.

why not something like while dark

void loop ()
{
    if (HIGH == digitalRead(sensorpin)) {
        OLEDOff();
        digitalWrite(humidifier, HIGH); //Humidifier OFF
        flag = true;                    // what does flag represent?
        return;
    }

    sensors_event_t humidity, temp;

Whoops on my part

What I meant to write was
The answer is yes, you can use digitalRead() on a pin with a pinMode() of OUTPUT

Thanks for spotting that

Thanks for your guidance! That will deffinitely be useful one day. :slight_smile:

Instead of flag I used humonoff = 1/0 to represent on/off, that did the job. Then I could write an if inside the while dark to turn it off if on, and change humonoff to 0.

Edit: Ah, now I see what you mean. That's exactly what I was looking for with this thread, for some reason I never thought of using digitalRead. Thanks for everyone's input.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.