I am working on building a "simple" HVAC thermostat. This is my current sketch.
// include the libraries for gizmos
#include <LiquidCrystal.h>
#include <dht11.h>
dht11 DHT11; // Figure out what this line does
LiquidCrystal lcd(12, 11, 5, 4, 3, 2); // initialize the library with the numbers of the interface pins
// These constants won't change:
const int ACledPin = 8; // Air Conditioner LED (RED)
const int HTledPin = 9; // Heat LED (YELLOW)
const int threshold = 77; // an arbitrary threshold level that's in the range of the analog input
const int RELAY_01 = 6; // RELAY 1 PIN 9
const int RELAY_02 = 10; // RELAY 2 PIN 10
void setup() {
DHT11.attach(7); // Attach DHT11 sensor to pin 7
lcd.begin(16, 2); // set up the LCD's number of columns and rows:
Serial.begin(9600); // Serial setup
pinMode(ACledPin, OUTPUT); // initialize pin 8 as output
pinMode(HTledPin, OUTPUT); // initialize pin 9 as output
pinMode(RELAY_01, OUTPUT); // initialize pin 6 as output
pinMode(RELAY_02, OUTPUT); // initialize pin 10 as output
// Leaves the relays in default "off" (no noise/click)
digitalWrite(RELAY_01, HIGH);
digitalWrite(RELAY_02, HIGH);
}
void loop() {
float analogValue = (DHT11.fahrenheit()); // trying to set up the temp reading as an int variable
////////////// if then ////////////////
// if the analog value is high enough, turn on the LED:
if (analogValue > threshold) {
digitalWrite(ACledPin, HIGH);
digitalWrite(HTledPin, LOW);
digitalWrite(RELAY_01, HIGH);
digitalWrite(RELAY_02, LOW);
}
else {
digitalWrite(ACledPin, LOW);
digitalWrite(HTledPin, HIGH);
digitalWrite(RELAY_01, LOW);
digitalWrite(RELAY_02, HIGH);
}
Serial.println(analogValue); // print the analog value:
// Set up the LCD Stuff
lcd.setCursor(0, 0); // Set cursor to top left
lcd.setCursor(0, 1);
lcd.print(analogValue, 1);
lcd.print("F");
delay(1000);
}
I want to add "smoothing" to the temperature reading. I Found this tutorial http://arduino.cc/en/Tutorial/Smoothing and have it working the way I want it.
// Define the number of samples to keep track of. The higher the number,
// the more the readings will be smoothed, but the slower the output will
// respond to the input. Using a constant rather than a normal variable lets
// use this value to determine the size of the readings array.
#include <dht11.h>
dht11 DHT11; // Figure out what this line does
const int numReadings = 5;
float readings[numReadings]; // the readings from the analog input
int index = 0; // the index of the current reading
float total = 0; // the running total
float average = 0; // the average
void setup()
{
DHT11.attach(7); // Attach DHT11 sensor to pin 7
Serial.begin(9600); // initialize serial communication with computer:
for (int thisReading = 0; thisReading < numReadings; thisReading++) readings[thisReading] = 0; // initialize all the readings to 0:
}
void loop() {
// subtract the last reading:
total= total - readings[index];
// read from the sensor:
readings[index] = (DHT11.fahrenheit()); //analogRead(inputPin);
// add the reading to the total:
total= total + readings[index];
// advance to the next position in the array:
index = index + 1;
// if we're at the end of the array...
if (index >= numReadings)
// ...wrap around to the beginning:
index = 0;
// calculate the average:
average = total / numReadings;
// send it to the computer as ASCII digits
Serial.println("");
Serial.print("Realtime: ");
Serial.println(DHT11.fahrenheit(),2);
Serial.print("Average: ");
Serial.println(average);
delay(2000); // delay in between reads for stability
}
I'm new to Arduino and don't know the best way to do this. I'm not certain how to do multiple loops/delays in a single project, or if I even need to.
EDIT:
I've ended up with this based on the comments from @Craig. The only thing that needs some adjustment are the first 4 readings are low, so it would be nice to somehow ignore, probably not a big deal in the long run.
Example readings on startup;
Realtime: 77.00
Average: 15.40
Realtime: 77.00
Average: 30.80
Realtime: 77.00
Average: 46.20
Realtime: 77.00
Average: 61.60
Realtime: 77.00
Average: 77.00
Final code
// include the libraries for gizmos
#include <LiquidCrystal.h>
#include <dht11.h>
dht11 DHT11; // Figure out what this line does
LiquidCrystal lcd(12, 11, 5, 4, 3, 2); // initialize the library with the numbers of the interface pins
// These constants won't change:
const int ACledPin = 8; // Air Conditioner LED (RED)
const int HTledPin = 9; // Heat LED (YELLOW)
const int threshold = 77; // an arbitrary threshold level that's in the range of the analog input
const int RELAY_01 = 6; // RELAY 1 PIN 9
const int RELAY_02 = 10; // RELAY 2 PIN 10
// START ADDITIONS ////////////////////////////////////////////////////////////////////////////////////////////////
const int numReadings = 5;
float readings[numReadings]; // the readings from the analog input
int index = 0; // the index of the current reading
float total = 0; // the running total
float get_smoothed_temp() {
total = total - readings[index]; // remove oldest reading
readings[index] = DHT11.fahrenheit();
total += readings[index];
index = (index + 1) % numReadings;
return (total / numReadings);
}
// END ADDITIONS ////////////////////////////////////////////////////////////////////////////////////////////////
void setup() {
DHT11.attach(7); // Attach DHT11 sensor to pin 7
lcd.begin(16, 2); // set up the LCD's number of columns and rows:
Serial.begin(9600); // Serial setup
for (int thisReading = 0; thisReading < numReadings; thisReading++) readings[thisReading] = 0; // initialize all the readings to 0:
pinMode(ACledPin, OUTPUT); // initialize pin 8 as output
pinMode(HTledPin, OUTPUT); // initialize pin 9 as output
pinMode(RELAY_01, OUTPUT); // initialize pin 6 as output
pinMode(RELAY_02, OUTPUT); // initialize pin 10 as output
// Leaves the relays in default "off" (no noise/click)
digitalWrite(RELAY_01, HIGH);
digitalWrite(RELAY_02, HIGH);
}
void loop() {
float analogValue = (get_smoothed_temp()); // trying to set up the temp reading as an int variable
////////////// if then ////////////////
// if the analog value is high enough, turn on the LED:
if (analogValue > threshold) {
digitalWrite(ACledPin, HIGH);
digitalWrite(HTledPin, LOW);
digitalWrite(RELAY_01, HIGH);
digitalWrite(RELAY_02, LOW);
}
else {
digitalWrite(ACledPin, LOW);
digitalWrite(HTledPin, HIGH);
digitalWrite(RELAY_01, LOW);
digitalWrite(RELAY_02, HIGH);
}
Serial.println(analogValue); // print the analog value:
// Set up the LCD Stuff
lcd.setCursor(0, 0); // Set cursor to top left
lcd.print("Real F:");
lcd.setCursor(0, 1);
lcd.print("AVG F:");
lcd.print(analogValue, 1);
delay(1000);
}