2

I am trying to modify this sketch; https://github.com/itsallvoodoo/home_automation/blob/master/Arduino_HVAC_Controller/HVAC_Controller/HVAC_Controller.ino

But I have a different temperature sensor, a DHT11, and I am using the libraries from Adafruit.

The DHT11 can't be constantly polled, 1 second intervals is the maximum rate.

I am currently working (fighting) with the portion towards the bottom in the section:

boolean getTemp() {

As my sketch stands, I have "currentTemp" statically assigned to 79 degrees. When I upload, the arduino acts as I would expect it to given that data.

If I change it to 0 (zero) and try to set the temp by reading the sensor in the section starting at line 237  like this;

boolean getTemp() {

  currentTemp = 0;
  long temp = 0;

  if ((millis() - TempLastCheck) > 2000) {
    TempLastCheck = millis();
    //  currentTemp = dht.readTemperature(true);
      temp = dht.readTemperature(true);
     Serial.println(dht.readTemperature(true));
  }

The LCD is blank after it restarts, and the arduino triggers the "heat" relay because the temperature is 0 (zero)

I'm sure I'm missing something simple, because the DHT11 outputs floats, or something... I'm too new to this language to know what I'm missing. Any help will be greatly appreciated.

// name:     HVAC_Controller.ino
// author:   Chad Hobbs


// -------------LIBRARIES---------------- THESE ARE REQUIRED TO BE IN YOUR SKETCHBOOK\LIBRARY FOLDER FOR COMPILING ----
#include <Wire.h>                   // Protocol to communicate with I2C devices
#include <Adafruit_MCP23017.h>      // LCD protocol
#include <Adafruit_RGBLCDShield.h>  // LCD + Button shield protocol
#include "DHT.h"                // DHT11 Temp/Humidity Sensor 
// --------------PARAMETERS---------------------
int heat = 55;                    // This is the default heater trigger temp setting
int cool = 70;                    // This is the default cooling trigger temp setting
int buffer = 1;                   // This is the range away from the setpoint the heat or AC will overcool/heat to prevent shorter cycles
int heatPin = 7;                  // Digital pin used for turning on the heater relay
int coolPin = 8;                  // Digital pin used for turning on the AC relay
int cycleTime = 60;               // The length of time to delay running the AC or heat to prevent short cycling
boolean heatRunning = false;      // Stores whether the heater is currently running or not
boolean coolRunning = false;      // Stores whether the AC is currently running or not
long currentTemp = 0;             // Current room temperature

int serialSpeed = 9600;           // Default serial comm speed
long oldTemp = 0;                 // Used to only update the currentTemp if the temp has changed
unsigned long timeOut;            // Backlight timeout variable
boolean editable = false;         // Determines whether or not button presses will do anything, used to avoid accidental changes
char* menu[] = {"Cooling", "Heating"};  // Menu display for either setting the high point or the low point of the temp range
int menuPosition = 1;             // Current position in the setting menu
unsigned long heatLastRan;        // Stores the time the heater last ran
unsigned long coolLastRan;        // Stores the time the AC last ran
unsigned long TempLastCheck;      // Stores the last sensor reading ran

// -------------Library Interaction--------------
// Create our LCD Shield instance
Adafruit_RGBLCDShield lcd = Adafruit_RGBLCDShield();
#define ON 0x7            // For single color LCD, set on to white
#define OFF 0x0           // For single color LCD, set off to black

#define DHTTYPE DHT11   // DHT 11 
#define DHTPIN 4     // what pin we're connected to
DHT dht(DHTPIN, DHTTYPE);


// ----------------------------------------------------------------------------------------
// Function Name: setup()
// Parameters:    None
// Returns:       None
// Description:   This function executes housekeeping duties and staging for the loop() function; executed once
// ----------------------------------------------------------------------------------------
void setup() {

  //  pinMode(busPin,INPUT);  // Designate temperature bus pin data direction
  pinMode(heatPin, OUTPUT);               // Heat circuit relay
  pinMode(coolPin, OUTPUT);               // Cooling circuit relay
  Serial.begin(serialSpeed);


  lcd.begin(16, 2);       // set up the LCD's number of columns and rows
  lcd.setBacklight(ON);   // Start off with backlight on until time-out
  timeOut = millis();     // Set the initial backlight time
  TempLastCheck = millis();  // Set the initial sensor timing

  dht.begin();
}

// ----------------------------------------------------------------------------------------
// Function Name: loop()
// Parameters:    None
// Returns:       None
// Description:   This is the main executing block of the program, calling all ancillary functions to operate the Arduino
// ----------------------------------------------------------------------------------------
void loop() {
  if ((millis() - timeOut) > 30000) {                   // Turn on backlight for 30 seconds, else turn it off
    lcd.setBacklight(OFF);
    editable == false;
  } else {
    lcd.setBacklight(ON);
  }

  lcd.setCursor(0, 1);                  // Starting position of character printing

  uint8_t buttons = lcd.readButtons();  // Constantly check to see if something has been put on the bus

  // --------------Handle updating Temp Change Display---------------------
  if (getTemp()) {                      // Constantly check to see if the temperature has changed, and update appropriately
    lcd.clear();
    lcd.print("Temp:");
    lcd.print(currentTemp);
  }

  // --------------Handle Button Presses---------------------
  if (buttons) {
    timeOut = millis();
    lcd.setBacklight(ON);


    if (buttons & BUTTON_SELECT) {      // Allow parameters to be changed only if the Select button has been pressed first
      editable = true;
    }
    if (editable) {                     // go to buttonHandler to handle menu switching and parameter manipulations
      buttonHandler(buttons);
    }
  }

  // --------------Handle Heat and Cooling Cycles---------------------
  powerControl();

}

// ----------------------------------------------------------------------------------------
// Function Name: powerControlHandler()
// Parameters:    None
// Returns:       None
// Description:   This function will handle turning AC or Heat off and on based on current temp, timers, and setpoints
// ----------------------------------------------------------------------------------------
void powerControl() {

  // TODO: Add in fan relay control


  // This executes if the heater is running and it gets warm enough to turn off.
  if (heatRunning && (currentTemp > (heat + buffer))) {
    heatRunning = false;
    heatLastRan = millis();
    digitalWrite(heatPin, HIGH);
    return;
  }

  // This executes if the AC is running and it gets cool enough to turn off
  if (coolRunning && (currentTemp < (cool - buffer))) {
    coolRunning = false;
    coolLastRan = millis();
    digitalWrite(coolPin, HIGH);
    return;
  }

  // This executes if it gets cold enough and the heater has not run for a minimum time (short cycle protection)
  if ((currentTemp < heat) && ((millis() - heatLastRan) > cycleTime)) {
    digitalWrite(heatPin, HIGH);
    heatRunning = true;
    return;
  }

  // This executes if it gets hot enough and the AC has not run for 10 minutes (short cycle protection)
  if ((currentTemp > cool) && ((millis() - coolLastRan) > 60)) {
    digitalWrite(coolPin, HIGH);
    coolRunning = true;
    return;
  }

  return;
}


// ----------------------------------------------------------------------------------------
// Function Name: buttonHandler()
// Parameters:    None
// Returns:       Boolean, True if the temperature has changed, else False
// Description:   This function polls the temp sensors, retrieves the values, and then returns
// ----------------------------------------------------------------------------------------
boolean buttonHandler(uint8_t buttons) {

  // --------------PARAMETERS---------------------
  int heatModifier = 0;
  int coolModifier = 0;


  // --------------SETUP---------------------
  lcd.clear();

  // If the UP Button is pushed, increasing the setpoint
  if (buttons & BUTTON_UP) {
    if (menuPosition == 0) {
      coolModifier = 1;
    }
    if (menuPosition == 1) {
      heatModifier = 1;
    }
  }

  // If the Down Button is pushed, decreasing the setpoint
  if (buttons & BUTTON_DOWN) {
    if (menuPosition == 0) {
      coolModifier = -1;
    }
    if (menuPosition == 1) {
      heatModifier = -1;
    }
  }

  // If the Left Button is pushed, change the menu context
  if (buttons & BUTTON_LEFT) {
    if (menuPosition == 1) {
      menuPosition = 0;
    } else {
      menuPosition = 1;
    }
  }

  // If the Right Button is pushed, change the menu context
  if (buttons & BUTTON_RIGHT) {
    if (menuPosition == 1) {
      menuPosition = 0;
    } else {
      menuPosition = 1;
    }
  }

  // If anything has been modified, update accordingly
  heat = heat + heatModifier;
  cool = cool + coolModifier;

  // Print proper menu title and setpoint
  lcd.setCursor(0, 0);
  lcd.print(menu[menuPosition]);
  lcd.setCursor(0, 1);
  lcd.print("Setpoint: ");

  if (menuPosition == 0) {
    lcd.print(cool);
  }
  if (menuPosition == 1) {
    lcd.print(heat);
  }

}

// ----------------------------------------------------------------------------------------
// Function Name: getTemp()
// Parameters:    None
// Returns:       Boolean, True if the temperature has changed, else False
// Description:   This function polls the temp sensors, retrieves the values, and then returns
// ----------------------------------------------------------------------------------------
boolean getTemp() {

  currentTemp = 79;
  long temp = 0;

  if ((millis() - TempLastCheck) > 2000) {
    TempLastCheck = millis();
    //  currentTemp = dht.readTemperature(true);
     //temp = dht.readTemperature(true);
     Serial.println(dht.readTemperature(true));
  }

  /////////////////////////////////////////////////////////////////////////////////////////////////////
  //  sensors.requestTemperatures(); // Send the command to the device to get temperatures

  // Go through devices and read the temperatures
  //  for(int x = 0; x < numOfDevices; x++){
  //   temp = sensors.getTempF(addrs[x]);
  //  if(temp != currentTemp){
  //   currentTemp = temp;
  //    }
  //  }
  /////////////////////////////////////////////////////////////////////////////////////////////////////
  // Check to see if temperature has changed
  if (currentTemp != oldTemp) {
    oldTemp = currentTemp;
    return true;
  }
  else {
    return false;
  }
}
1

First of, you read the temperature and put it into local variable temp. You need to copy the value from temp into currentTemp.

Secondly, you are reading the temperature twice in a row. Once to assign it to temp and again for the Serial.println.

  if ((millis() - TempLastCheck) > 2000) {
    TempLastCheck = millis();
    currentTemp = dht.readTemperature(true);
    Serial.println(currentTemp);
  }

When setting the currentTemp to 0 inside getTemp, getTemp never return true, because you initialized the value of currentTemp with 0 (long currentTemp = 0;). So it never changed, and the lcd is never updated. Just change it to something like:

long currentTemp = -100;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.