Essentially, I have a Temperature/Humidity sensor and a Real Time Clock module
wired up to the Arduino. The data is then output to a 16x2 LCD screen. When the
humidity reaches 60%+ a DC motor "FAN" is turned on. When the humidity falls
below 60% the fan turns off.
The first thing, when the humidity is climbing to 60; lets say we reach 60, the
motor doesn't kick in until the next cycle of humidity readings. How would I
achieve an more instantaneous start? an interrupt?
The second thing. The LCD projects the temperature for 3 seconds followed by the time for 3 seconds;
in a sort of cyclical fashion. The goal is to get "live" updates of the humidity as it
changes projected to the LCD( lets say I put my finger on the sensor, the humidity
changes rapidly). The same idea for the seconds. I'm looking for a way to clean up
the code's loop section a touch. maybe using some counter or millis funciton?..
Im relatively new to Arduino. Please help!!
//LIBRARIES
#include <SimpleDHT.h>
#include <LiquidCrystal.h>
#include <DS3231.h>
//TIMING FUNCTIONS
DS3231 clock;
RTCDateTime dt;
unsigned long startMillis;
unsigned long currentMillis;
const unsigned long period = 500;
//"FAN" USING L293D
#define ENABLE 5 // Motor
#define DIRA 3 // Motor
#define DIRB 4 // Motor
//DHT11 TEMP+HUM.
int pinDHT11 = 2;
SimpleDHT11 dht11;
byte temperature = 0;
byte humidity = 0;
//LIQUID CRYSTAL
LiquidCrystal lcd(7, 8, 9, 10, 11, 12);
void setup(){
startMillis = millis(); // initial start time
Serial.begin(9600);//initialize serial communication
// "FAN" OUTPUT //
pinMode(ENABLE,OUTPUT);
pinMode(DIRA,OUTPUT);
pinMode(DIRB,OUTPUT);
// RTC //
clock.begin();
clock.setDateTime(__DATE__,__TIME__);
// LCD DISPLAY //
lcd.begin(16,2);
lcd.setCursor(0,0);
lcd.print("Temp & Humidity");
delay(2000);
lcd.clear();
}
void Temp(){
dht11.read(pinDHT11, &temperature, &humidity, NULL);
currentMillis = millis();
//Display Temperature Reading
lcd.setCursor(0,0);
lcd.print("TEMP:");
lcd.print(temperature);
lcd.print("*C /");
lcd.print(round(temperature *1.8 + 32));
lcd.print(" *F");
//Display Humidity Reading
lcd.setCursor(0,1);
lcd.print("HUMIDITY:");
lcd.print("");
lcd.print(humidity);
lcd.print("%");
}
void Time(){
dt = clock.getDateTime();
lcd.print(clock.dateFormat("M-jS-Y", dt));
lcd.setCursor(0,1);
lcd.print("TIME:");
lcd.print(clock.dateFormat(" h:i:s",dt));
}
void loop(){
Temp();
delay(500);
Temp();
delay(500);
Temp();
delay(500);
Temp();
delay(500);
Temp();
delay(500);
lcd.clear();
Time();
delay(1000);
lcd.clear();
Time();
delay(1000);
lcd.clear();
Time();
delay(1000);
lcd.clear();
// Run "FAN" if humidity is 60% or above
if (humidity>= 60){
digitalWrite(ENABLE,HIGH);
digitalWrite(DIRA,LOW);
digitalWrite(DIRB,HIGH);
Serial.println("FAN ON");
}
else{
digitalWrite(ENABLE,LOW);
digitalWrite(DIRA,LOW);
digitalWrite(DIRB,LOW);
Serial.println("FAN OFF");
}
}