Humidity Controller

Hi All,

I have been working on this code for a couple of weeks, and need some assistance. I am looking to include a value in my IF statement where when it reaches the minimum humidity threshold of 50% to kick on but to stay on until it reaches 80%.

i would also like it to be bypassed by a button if possible. Can anyone help? Im still pretty newbish and would love the help.

#define BLYNK_PRINT Serial   
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <SimpleTimer.h>
#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include "DHT.h"
#define OLED_MOSI   13
#define OLED_CLK   14
#define OLED_DC    4
#define OLED_CS    15
#define OLED_RESET 12

Adafruit_SSD1306 display(OLED_MOSI, OLED_CLK, OLED_DC, OLED_RESET, OLED_CS);


//#define DHTTYPE DHT11   // DHT 11
#define DHTTYPE DHT22   // DHT 22  (AM2302), AM2321
#define DHTPIN 2     // what digital pin we're connected to
DHT dht= DHT(DHTPIN, DHTTYPE); 

//Constants
const int airPump = 5;   
const int humidity = 2; 

int value;
int minhum = 50;

char auth[] = "00***************"; 

SimpleTimer timer;

void setup()   {
	Serial.begin(9600);
  Blynk.begin(auth, "*******", "***********");
  timer.setInterval(1000L, sendUptime);
  pinMode(airPump,OUTPUT);
	display.begin(SSD1306_SWITCHCAPVCC);
	dht.begin(); 
	display.clearDisplay();
	display.setTextSize(3);
	display.setTextColor(WHITE);
	display.println("REPTILE\n EDGE");
	display.display();
	delay(1000);
}

void sendUptime()
{
  float h = dht.readHumidity();
  float t = dht.readTemperature(true);
  Blynk.virtualWrite(10, t);
  Blynk.virtualWrite(11, h);
}

void loop(){
	float h = dht.readHumidity();
	// Read temperature as Celsius (the default)
	float t = dht.readTemperature(true);
	display.clearDisplay();
	display.setCursor(0,0);
	display.setTextSize(2);
	display.setTextColor(WHITE);
	// Check if any reads failed and exit early (to try again).
	if (isnan(h) || isnan(t)) {
	display.println("DHT sensor\n FAIL");}	
	else{display.print("Temp:");
	display.println(t);
	display.print("Hum:");
	display.println(h);}
  
 if(h < minhum){
  digitalWrite(airPump,HIGH);}
  else {
  digitalWrite(airPump,LOW);}
  Blynk.run();
  timer.run();
	display.display();
	delay(150);
	//display.print("0x");
}

Getting hysteresis like that is as simple as using two different if statements.

if(humidity < 50%) {
    // turn on humidifier
}

if(humidity > 80% {
   // turn off humidifier
}

Think about that for a second. Between 50 and 80 nothing happens, so the humidifier would stay in whatever state it is in.

To add an override button is easy too. Just write an if statement to check the state of the button and only allow the code you want to run inside the if statement.

if(digitalRead(buttonPin) == HIGH) {  // if button not pressed
   // do your thing
}
else {
   // button was pressed
   // do whatever you want to do in that case
}

to clarify, my

 if(h < minhum){
  digitalWrite(airPump,HIGH);}
 if(h> maxhum) {
  digitalWrite(airPump,LOW); 
 }

would go inside the

 if(digitalRead(16) == HIGH) {  // if button not pressed
   // do your thing

?

Thank you so much for your help

nevermind, figured it out. works just as planned now. Thank you!