I have a program where I'm trying to average multiple temperature readings from a DHT11 sensor and display the averaged values within the serial monitor.
My issue comes down to how I ultimately want the data displayed. I would like to have the header line displayed and then two sets of the averaged data displayed and then this process repeated. Essentially the header line displayed, two rows of averaged data and then loop through again.
I'm guessing I need another for loop but can't pinpoint who, what or where exactly... maybe something like this :
for( j = 0 ; j < 2 ; j++)
Here's the working code that displays an averaged value and the header line each time through the loop:
// Includes SimpleDHT.h from the SimpleDHT library.
#include <SimpleDHT.h>
// Pin Declarations:
int pinDHT11 = 8; // DHT11 Signal pin set to pin 8
int Vpin = 9; // DHT11 Voltage pin set to pin 9
int GNDpin = 10; // DHT11 Ground pin set to pin 10
// Master Time Delay:
int masterDelay = 2000; // 2 seconds = 2 Hz
// ...
SimpleDHT11 dht11;
void setup()
{
// Set baud rate: 9600 bps
Serial.begin(9600);
// Setup 5V and GND pins:
pinMode( Vpin , OUTPUT); // sets Voltage pin as OUTPUT
pinMode( GNDpin , OUTPUT); // sets Ground pin as OUTPUT
digitalWrite( Vpin , HIGH ); // sets Voltage pin to HIGH (5V)
digitalWrite( GNDpin , LOW ); // sets Ground pin to LOW (GND)
}
// Reads temperature and humidity sample data from DHT11:
void loop()
{
// Reads the raw sample data.
// Sets temperature, humidity and data[] to 0:
byte temperature = 0;
byte humidity = 0;
byte data[40] = {0};
//...
if (dht11.read(pinDHT11, &temperature, &humidity, NULL))
{
Serial.println("Read DHT11 failed");
return;
}
// Declare local arrays to store 5 temperature and humidity data points:
int tempRead[5]; // Array for the temperature readings
int humiRead[5]; // Array for the humidity readings
// Declare variable for 2nd FOR statement:
int j; // Iterative Variable
// Declare variables for temperature and humidity sums:
int tempSum = 0; // Initializes tempSum to 0
int humiSum = 0; // Initializes humiSum to 0
// Declare variables for average temperature and humidity:
float tempAvg; // Creates a float variable for Average Temperature to display two decimals
int humiAvg; // Creates a float variable for Average Humidity to display two decimals
// Fills tempRead[] & humiRead[] Arrays with 5 data points and then Sums the 5 data points:
for ( j = 0 ; j < 5 ; j++)
{
// Fills tempRead[] and humiRead[] arrays with temperature and humidity data for 5 readings:
tempRead[j] = (float)temperature; // Fills tempRead[] with 5 (int)temperature data points
humiRead[j] = (float)humidity; // Fills humiRead[] with 5 (int)humidity data points
// Sums 5 data points from tempRead[] and humiRead[]:
tempSum += tempRead[j]; // Sums the five data points in tempRead[]
humiSum += humiRead[j]; // Sums the five data points in humiRead[]
}
// Compute the average temperature and humidity for the 5 summed data points:
tempAvg = tempSum / 5; // Average Temp = Temp Sum / 5
humiAvg = humiSum / 5; // Average Humidity = Humidity Sum / 5
// Serially display the data: Average Temperature in C & F and Average Humidity
// Print Header Line at program start once good data has been recieved:
Serial.println();
Serial.print("TEMP ( *C ):");
// ...
Serial.print("\t");
// ...
Serial.print("TEMP ( *F ):");
// ...
Serial.print("\t");
// ...
Serial.print("HUMIDITY ( % ):");
// ...
Serial.println("\t");
// ...
Serial.println("================================================");
// Print Average Temperatures and Average Humidity Data:
// Serially display the computed average temperature in *C
Serial.print(tempAvg);
// tab over twice
Serial.print("\t"); Serial.print("\t");
// Serially display the computed average temperature in *F
Serial.print(tempAvg * 1.8 + 32); // *F = (*C)*1.8 + 32
// tab over twice
Serial.print("\t"); Serial.print("\t");
// Serially display the computed average humidity
Serial.print(humiAvg);
Serial.println();
// DHT-11 Sampling Rate:
// Sampling rate set to 2 Hz:
delay(masterDelay);
}