/*BATHROOM EXHAUST FAN*/
#include <Adafruit_Sensor.h>
#include <DHT.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 16, 2);
//Rotary encoder inputs
#define UP 8
#define DOWN 9
#define buttonClick 10
//Output
#define fan 3
// Set DHT type and pin
#define DHTTYPE DHT22 // DHT 22 (AM2302)
#define DHT_sensorPin 12
// Initialize DHT sensor for normal 16mhz Arduino:
DHT dht = DHT(DHT_sensorPin, DHTTYPE);
int humiditySet = 50;
int currentStateUP;
int lastStateUP;
String currentDir = "";
unsigned long lastButtonPress = 0;
int max = 80;
int min = 25;
int sensingIncrement = 5000;
int state = 1;
int counter = 0;
void setup() {
// Setup sensor:
dht.begin();
//Run Rotary Encoder Setup
rotaryEncoderSetup();
//Run LCD 1602 Display Setup
lcd1602Setup();
pinMode(fan, OUTPUT);
// Setup Serial Monitor
Serial.begin(9600);
}
void loop() {
if (counter == 0) {
// Read the humidity in %:
float h = dht.readHumidity();
// Read Temp in F
float T = dht.readTemperature(true);
// Check if any reads failed and exit early (to try again):
if (isnan(h) || isnan(T)) {
lcd.setCursor(0,0);
lcd.print("Failed to read ");
lcd.setCursor(0,1);
lcd.print("from DHT sensor!");
return;
}
// Print a message to the LCD.
lcd.setCursor(0, 0);
lcd.print("Humidity: ");
lcd.print(h, 1);
lcd.print("%");
// set the cursor to column 0, line 1
// (note: line 1 is the second row, since counting begins with 0):
lcd.setCursor(0, 1);
// Print a message to the LCD.
lcd.print("Temp: ");
lcd.print(T, 1);
lcd.print((char)223);
lcd.print("F ");
if (h > humiditySet) {
digitalWrite(fan, HIGH);
} else {
digitalWrite(fan, LOW);
}
}
// Read the current state of UP
currentStateUP = digitalRead(UP);
// If last and current state of UP are different, then pulse occurred
// React to only 1 state change to avoid double count
if (currentStateUP != lastStateUP && currentStateUP == 1) {
// If the DOWN state is different than the UP state then
// the encoder is rotating CCW so decrease humidity setting
if (digitalRead(DOWN) != currentStateUP) {
humiditySet--;
counter = 0;
} else {
// Encoder is rotating CW so increase humidity setting
humiditySet++;
counter = 0;
}
humiditySet = maxMinCheck(humiditySet);
lcd.setCursor(0, 1);
lcd.print("Humidity Set:");
lcd.print(humiditySet);
lcd.print("%");
}
// Remember last UP state
lastStateUP = currentStateUP;
// Read the button state
int btnState = digitalRead(buttonClick);
//If we detect LOW signal, button is pressed
if (btnState == LOW) {
//if 50ms have passed since last LOW pulse, it means that the
//button has been pressed, released and pressed again
if (millis() - lastButtonPress > 50) {
switch (state) {
case 0:
state = 1;
lcd.backlight();
break;
case 1:
state = 0;
lcd.noBacklight();
break;
}
}
// Remember last button press event
lastButtonPress = millis();
}
// Put in a slight delay to help debounce the reading
delay(1);
if (counter < sensingIncrement) {
counter++;
} else {
counter = 0;
}
}
void rotaryEncoderSetup() {
// Set encoder pins as inputs
pinMode(UP, INPUT);
pinMode(DOWN, INPUT);
pinMode(buttonClick, INPUT_PULLUP);
// Read the initial state of UP
lastStateUP = digitalRead(UP);
}
void lcd1602Setup() {
lcd.init();
lcd.backlight();
lcd.setCursor(0, 0);
lcd.print("Setup Complete!");
delay(2000);
lcd.clear();
}
int maxMinCheck(int checkIn) {
int checkOut;
if (checkIn < min) {
checkOut = min;
} else if (checkIn > max) {
checkOut = max;
} else {
checkOut = checkIn;
}
return checkOut;
}