Trying to program a way to control my thermostat by interfacing it with my Arduino Uno. I am using a DHT11 to read temperatures and humidity. However, after making its first reading, it is not updating after that. I have included the reading in a loop that waits 2.5 seconds between readings to give the sensor time. However, its not updating. I tested the DHT11 with the example code that came with the DHT library and it is in itself functioning. To test it I have directed a warm hairdryer to the DHT sensor, but the readings dont change. However, when I reset it, the sensor will then show the higher reading initially but then wont update like before.
As well, the the photoresistor in the system will update its lux readings the whole time.
I have included the code below. Any help greatly appreciated! (sorry if the code is a bit messy, I am a novice programmer)
#include <LiquidCrystal.h>
#include <dht.h>
dht DHT;
#define DHT11_PIN 8
#define BOUNCE_DURATION 400
unsigned long checkLCD = 0;
volatile unsigned long bounceTime = 0;
unsigned long refreshBut = millis();
volatile int desiredTemp = 21;
boolean AC_on = true;
unsigned long checkBut = 0;
int temp = 0;
int humi = 0;
unsigned long lastTime = 0;
// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(12, 11, 5, 4, 10, 9);
void setup() {
// set up the LCD's number of columns and rows:
lcd.begin(16, 2);
Serial.begin(9600);
// Print a message to the LCD.
lcd.print("DHT test");
Serial.println("DHT TEST Type,\tstatus,\tHumidity (%),\tTemperature (C)");
pinMode(3, INPUT_PULLUP);
pinMode(2, INPUT_PULLUP);
pinMode(13, OUTPUT);
pinMode(A1, INPUT_PULLUP);
testDHT(); // testing DHT sensor
}
void loop() {
if (millis() > lastTime)
{
temp = DHT.temperature;
humi = DHT.humidity;
lastTime = millis() + 2500;
Serial.println(temp);
}
int luxSEND = -0.066433 * analogRead(A0) + 81.467234; //lux reading
unsigned long refreshLCD = millis();
int upBut = digitalRead(2); //up button
int downBut = digitalRead(3); //down button
int changeAC_send = digitalRead(A1);//read button for change between heating and AC
unsigned long refreshBut = millis(); //for button for change between heating and AC
displayReadings(humi, temp, luxSEND, refreshLCD, desiredTemp); //display on LCD - humidity, temperature, lux
attachInterrupt(digitalPinToInterrupt(2), upButtonOn, FALLING);//Up in desired temp
attachInterrupt(digitalPinToInterrupt(3), downButtonOn, FALLING);//Down in desired temp
changeAC_on (changeAC_send, refreshBut); //change between heating and AC
if ((temp > desiredTemp) && (AC_on == true)) { //AC structure
digitalWrite(13, HIGH);
}
else if ((temp < desiredTemp) && (AC_on == false)) { //Heat Structure
digitalWrite(13, HIGH);
}
else {
digitalWrite(13, LOW);
}
}
void displayReadings (int h, int t, int lux, unsigned long refreshL, int desiT) {
if (refreshL >= (1000 + checkLCD)) {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("T:");
lcd.print(t);
lcd.print("C ");
lcd.print("H:");
lcd.print(h);
lcd.print("%");
Serial.print(h, 1);
Serial.print(",\t");
Serial.print(t, 1);
Serial.print(",\t");
lcd.setCursor(0, 1);
lcd.print("L:");
lcd.print(lux);
lcd.print(" ");
Serial.println(lux);
Serial.print(",\t");
lcd.print("Des.T:");
lcd.print(desiT);
lcd.print("C");
checkLCD = refreshL;
}
}
void testDHT () {
// READ DATA
lcd.clear();
Serial.print("DHT11, \t");
int chk = DHT.read11(DHT11_PIN);
switch (chk)
{
case DHTLIB_OK:
Serial.print("OK,\t");
break;
case DHTLIB_ERROR_CHECKSUM:
Serial.print("Checksum error,\t");
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Can't get reading");
lcd.setCursor(0, 1);
lcd.print("from DHT");
break;
case DHTLIB_ERROR_TIMEOUT:
Serial.print("Time out error,\t");
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Can't get reading");
lcd.setCursor(0, 1);
lcd.print("from DHT");
break;
default:
Serial.print("Unknown error,\t");
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Can't get reading");
lcd.setCursor(0, 1);
lcd.print("from DHT");
break;
}
}
void upButtonOn () {
if (millis() > bounceTime) {
Serial.println("Up Pressed");
desiredTemp = desiredTemp + 1;
bounceTime = millis() + BOUNCE_DURATION;
}
}
void downButtonOn () {
if (millis() > bounceTime) {
Serial.println("Down Pressed");
desiredTemp = desiredTemp - 1;
bounceTime = millis() + BOUNCE_DURATION;
}
}
void changeAC_on (int changeAC, unsigned long refL) {
if (refL >= (200 + checkBut)) {
if ((changeAC == LOW) && (AC_on == true)) {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("HEATING MODE");
Serial.println("Heat On");
AC_on = false;
checkBut = refL;
}
else if ((changeAC == LOW) && (AC_on == false)) {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("A/C MODE");
Serial.println("AC On");
AC_on = true;
checkBut = refL;
}
else {
checkBut = refL;
}
}
}
Additionally, here is a sample of the output from the serial monitor, while a hairdryer is being directed at it:
DHT TEST Type, status, Humidity (%), Temperature (C)
DHT11, OK, Current read temperature is: 29
40, 29, 53
, 40, 29, 53
, Current read temperature is: 29
40, 29, 53
, 40, 29, 53
, 40, 29, 53
, Current read temperature is: 29
40, 29, 54
, 40, 29, 54
, Current read temperature is: 29
40, 29, 52
, 40, 29, 53
, 40, 29, 53
, Current read temperature is: 29
40, 29, 53
, 40, 29, 53
, Current read temperature is: 29
40, 29, 53
, 40, 29, 53
, 40, 29, 53
, Current read temperature is: 29
40, 29, 53
, 40, 29, 53
, Current read temperature is: 29
40, 29, 53
, 40, 29, 51
, 40, 29, 52
, Current read temperature is: 29
40, 29, 52
, 40, 29, 52
, Current read temperature is: 29
40, 29, 54
, 40, 29, 54
, 40, 29, 54
, Current read temperature is: 29
40, 29, 53
, 40, 29, 54
, Current read temperature is: 29
40, 29, 53
, 40, 29, 53
, 40, 29, 54
, Current read temperature is: 29
40, 29, 54
, 40, 29, 54
, Current read temperature is: 29
40, 29, 54
, 40, 29, 54
, 40, 29, 54
, Current read temperature is: 29
40, 29, 54
, 40, 29, 54