I am working on creating a fan system for my desk because the temperature varies quite a bit from spot to spot and I would like it to be a little more even. To accomplish this, I am using an Arduino with a DHT11 temperature monitor. I have also included a Nokia LCD just for fun so I can see the current temp and humidity.
This uses bits from the UTFT.h and dht11.h libraries
#include <UTFT.h>;
#include <dht11.h>;
class TL
{
private:
int greenLEDpin;
int redLEDpin;
int tempInPin;
int powerPin;
int manualSwitchPin;
public:
void printTemp(int temp);
void printHum(int hum);
int checkStatus(int chk);
void goodStatus();
void badStatus();
void initPins();
UTFT lcd;
TL(int pina, int pinb, int pinc, int pind, int pine);
};
TL::TL(int pina, int pinb, int pinc, int pind, int pine) : lcd(LPH9135,6,5,2,3,4) //initialize the LCD using basic settings.
{
greenLEDpin = pina;
redLEDpin = pinb;
tempInPin = pinc;
powerPin = pind;
manualSwitchPin = pine;
}
void TL::printTemp(int temp)
{ // print out temp as Int Celcius
lcd.print("Temp: ", 10,2,0);
lcd.printNumI(temp,80,2);
}
void TL::printHum(int hum)
{ // print out humidity as Int
lcd.print("Humidity: ", 10,20,0);
lcd.printNumI(hum,80,20);
}
int TL::checkStatus(int chk)
{
lcd.setContrast(64); // max contrast
lcd.setBackColor(0,0,0); // black background
lcd.setColor(255,255,255); // white text
switch (chk)
{
case 0: lcd.print("Status: OK",10,100,0); goodStatus(); return 0;
case -1: lcd.print("Status: Checksum error!",10,100,0); badStatus(); return 1;
case -2: lcd.print("Status: Timeout error!",10,100,0); badStatus(); return 1;
default: lcd.print("Status: Unknown error!",10,100,0); badStatus(); return 1;
}
}
void TL::goodStatus()
{ // turn off the red led and turn on the green led
digitalWrite(greenLEDpin,HIGH);
digitalWrite(redLEDpin,LOW);
}
void TL::badStatus()
{ // turn off the green led and turn on the red led
digitalWrite(greenLEDpin,LOW);
digitalWrite(redLEDpin,HIGH);
}
void TL::initPins()
{
extern uint8_t SmallFont[]; //can also use BigFont[];
pinMode(greenLEDpin, OUTPUT); // Green Status Light
pinMode(redLEDpin, OUTPUT); // Red Error Status Light
pinMode(tempInPin, INPUT); // Pin 2 on the temp sensor
pinMode(powerPin, OUTPUT); // Pin 1 on the temp sensor
pinMode(manualSwitchPin, INPUT); // momentary press switch for manual temp update if needed
digitalWrite(greenLEDpin, LOW); // turn off led
digitalWrite(redLEDpin, LOW); // turn off led
digitalWrite(powerPin, HIGH); // Power up the temp sensor
digitalWrite(tempInPin, LOW); // begin readint temp
digitalWrite(manualSwitchPin, LOW); // not using the manual switch right now so turn off the power
lcd.InitLCD(PORTRAIT);
lcd.setFont(SmallFont);
lcd.clrScr();
lcd.setContrast(64); //max contrast
lcd.setBackColor(0,0,0); //black background
lcd.setColor(255,255,255); //white text
}
dht11 tempSens;
const int greenLEDpin = 38; // Green Status Light
const int redLEDpin = 34; // Red Error Status Light
const int tempInPin = 40; // Pin 2 on the temp sensor
const int powerPin = 43; // Pin 1 on the temp sensor
const int manualSwitchPin = 52; // momentary press switch for manual temp update if needed
int lastTemp = 0; // var for tracking temp change if needed
int lastHum = 0; // var for tracking humidity change if needed
TL tl(greenLEDpin,redLEDpin,tempInPin,powerPin,manualSwitchPin); //Initialize class
void setup()
{
tl.initPins();
}
void loop()
{
tl.lcd.clrScr();
int chk = tempSens.read(tempInPin); // Read in temp
if(tl.checkStatus(chk)==0){
tl.printTemp(tempSens.temperature); // Int in degrees Celcius
tl.printHum(tempSens.humidity); // Int
}
delay(100000); //delay to take it easy on the sensor. This can be removed or changed based on your needs.
}

