Hi everyone,
I am quite new to Arduino and I could solve every problem on my own in the past. I studied Arduino on my own, therfore please be pacient with me if the following problem might be simple to solve...
I have two DHT22 sensors to power a fan with two if-statements, but I can't get it to work propperly. In this example
the temperature inside (tempIN) needs to be in between 5 and under 25 degrees of celsius, AND
the average temperature outside (averageTempOUT) has to be higher than the temperature inside minus 2 degrees celsius AND
the humidity outside (humOUT) has to be smaller or equal to the average humidity inside.
When I run the code the fan starts even though the temperature difference is not met. The same with the humidity. In this example the humidity inside should be at least 15% higher than that of the average humidity outside.
My guess is your calculation of average is wrong, but it can be anywhere in the code you are hiding from us.
You could print your values to serial monitor as debugging tool.
Well, this is the code - IN TWO PARTS - as it is longer than 9000 characters...
PART ONE
// PARAMETERS
#define LOOPS_VENTILATOR 5
// DHT
#include "DHT.h"
DHT dht22in(3, DHT22);
DHT dht22out(2, DHT22);
// VARIABLES
// [several integers]
int relay_fan(7); // Relay for FAN on pin 7
// SETUP
void setup(void) {
pinMode(7, OUTPUT); // Relay FAN
digitalWrite(7, HIGH); // Relay FAN is OFF!
Serial.begin(9600); // Open serial communications
dht22in.begin();
dht22out.begin();
// SMOOTHING
for (int thisReadingTempIN = 0; thisReadingTempIN < SMOOTHING_TEMP_INSIDE; thisReadingTempIN++) {
ReadingsTempIN[thisReadingTempIN]; // Initialize all readings to 0
}
for (int thisReadingTempOUT = 0; thisReadingTempOUT < SMOOTHING_TEMP_OUTSIDE; thisReadingTempOUT++) {
ReadingsTempOUT[thisReadingTempOUT] = 0; // Initialize all readings to 0
}
for (int thisReadingHumIN = 0; thisReadingHumIN < SMOOTHING_HUMIDITY_INSIDE; thisReadingHumIN++) {
ReadingsHumIN[thisReadingHumIN] = 0; // Initialize all readings to 0
}
for (int thisReadingHumOUT = 0; thisReadingHumOUT < SMOOTHING_HUMIDITY_OUTSIDE; thisReadingHumOUT++) {
ReadingsHumOUT[thisReadingHumOUT] = 0; // Initialize all readings to 0
}
}
// LOOP
void loop(void) {
int i = 0; // Integer for operations and calculations
resetLoopcount++; // Counting loops since last reset / new power source
dayLoopcount++; // Counting loops of the day
float humIN = dht22in.readHumidity();
float tempIN = dht22in.readTemperature(); // Read temperature as Celsius
for (tempIN == 0; i < 7; i++) { // Read sensor 7 times if temp == 0°C
tempIN = dht22in.readTemperature();
delay(2000);
}
float humOUT = dht22out.readHumidity();
float tempOUT = dht22out.readTemperature(); // Read temperature as Celsius
for (tempOUT == 0; i < 7; i++) { // Read sensor 7 times if temp == 0°C
tempOUT = dht22out.readTemperature();
delay(2000);
}
// SMOOTHING
totalTempIN = totalTempIN - ReadingsTempIN[readTempIN]; // Subtract the last reading
ReadingsTempIN[readTempIN] = tempIN; // Read from the sensor
totalTempIN = totalTempIN + ReadingsTempIN[readTempIN]; // Add the reading to the <totalRadiation>
readTempIN = readTempIN + 1; // Advance to the next position in the array
if (readTempIN >= SMOOTHING_TEMP_INSIDE) { // If we're at the end of the array...
readTempIN = 0; // ...wrap around to the beginning
}
averageTempIN = totalTempIN / SMOOTHING_TEMP_INSIDE;// Calculate the <averageRadiationA>
totalTempOUT = totalTempOUT - ReadingsTempOUT[readTempOUT]; // Subtract the last reading
ReadingsTempOUT[readTempOUT] = tempOUT; // Read from the sensor
totalTempOUT = totalTempOUT + ReadingsTempOUT[readTempOUT];// Add the reading to the <totalRadiation>
readTempOUT = readTempOUT + 1; // Advance to the next position in the array
if (readTempOUT >= SMOOTHING_TEMP_OUTSIDE) { // If we're at the end of the array...
readTempOUT = 0; // ...wrap around to the beginning
}
averageTempOUT = totalTempOUT / SMOOTHING_TEMP_OUTSIDE; // Calculate the <averageRadiationA>
totalHumIN = totalHumIN - ReadingsHumIN[readHumIN]; // Subtract the last reading
ReadingsHumIN[readHumIN] = humIN; // Read from the sensor
totalHumIN = totalHumIN + ReadingsHumIN[readHumIN]; // Add the reading to the <totalRadiation>
readHumIN = readHumIN + 1; // Advance to the next position in the array
if (readHumIN >= SMOOTHING_HUMIDITY_INSIDE) { // If we're at the end of the array...
readHumIN = 0; // ...wrap around to the beginning
}
averageHumIN = totalHumIN / SMOOTHING_HUMIDITY_INSIDE; // Calculate the average
totalHumOUT = totalHumOUT - ReadingsHumOUT[readHumOUT]; // Subtract the last reading
ReadingsHumOUT[readHumOUT] = humOUT; // Read from the sensor
totalHumOUT = totalHumOUT + ReadingsHumOUT[readHumOUT]; // Add the reading to the <totalRadiation>
readHumOUT = readHumOUT + 1; // Advance to the next position in the array
if (readHumOUT >= SMOOTHING_HUMIDITY_OUTSIDE) { // If we're at the end of the array...
readHumOUT = 0; // ...wrap around to the beginning
}
averageHumOUT = totalHumOUT / SMOOTHING_HUMIDITY_OUTSIDE;// Calculate the average
As several people have a look at it, someone might have an improvement for building the average a more efficient way as the first loops start close to zero, therefore the switching for the fan waits for five loops to start when the if statements are true.
Thank you again for your help!
Good day,
I am a bit surprised that nobody responded... :o Did I mess up everything in the code or does nobody have an idea why the if statements aren't working properly?
Felipe2017:
Good day,
I am a bit surprised that nobody responded... :o Did I mess up everything in the code or does nobody have an idea why the if statements aren't working properly?
I guess people were hoping you'd whittled your code down sufficiently to put in a single post, and, in the process, figure out where you'd gone wrong.
for (int thisReadingTempIN = 0; thisReadingTempIN < SMOOTHING_TEMP_INSIDE; thisReadingTempIN++) {
ReadingsTempIN[thisReadingTempIN]; // Initialize all readings to 0
}
It's a good job they were all already zero, isn't it? for (tempIN == 0; i < 7; i++) {Back to "ForLoops:101" for you, my lad.
Thank you Gabriel,
Yes indeed I did, as a matter of fact I fumbled around another whole day before I wanted to respond with childish problems one can usually solve alone: First I found out that I can delete the codes quoted from you this morning - and it hasn't any negative effect on the sketch in terms of "missing something" for compiling or running on the Arduino.
In my original code I do use the serial monitor to see the results from each sensor and they do a great job (with correct measurements), therefore your last recommendation wasn't necessary to implement even though I did try that as well. The relay does it's job also, nevertheless the temperature or humidity differences still aren't met correctly, e.g. it turns on even if the temperature outside is equal instead of being higher at least two degrees as it is defined with
(averageTempOUT > (tempIN - 2))
Looks to me that I'm stuck with this project at the moment. Could there be another error in the code? Maybe it would be easier to calculate different averages that are used in the if statements; it annoys me also that those values start close to zero the first loop instead of using the actual data from the sensors. Sometimes bigger changes solve the problem at its first place...
That being said I didn't even manage to do the simple math for the percentage the relay is turned on each day after a declaration in the header of the sketch with "float activityPercentage = 0;"
Thank you very much, that is it! You made my day for this feeling of success after being stuck for such a long time!
Groove:
Have you fixed the ones I pointed out earlier?
Of course, I am grateful for each comment - and it looks like I do have a long way to go regarding my understanding of Arduino! That is what I meant with my posting an hour ago: I simply deleted both of the codes and the sketch is still running as no change happened.
I think the pic in the attachment with the sensor data shows exactly the problem.
// SMOOTHING
totalTempIN -= ReadingsTempIN[readTempIN]; // Subtract the last reading
ReadingsTempIN[readTempIN] = tempIN; // Read from the sensor
totalTempIN += tempIN; // Add the reading to the <totalRadiation>
readTempIN++; // Advance to the next position in the array
if (readTempIN >= SMOOTHING_TEMP_INSIDE) { // If we're at the end of the array...
readTempIN = 0; // ...wrap around to the beginning
}
Felipe2017:
I think the pic in the attachment with the sensor data shows exactly the problem.
I don't see it. What should I look for?
20.3 is between 5 and 25.
24 is larger than 20.3, and even much larger than 20.3 - 5 (=15.3)
51.4 is less or equal to 61.0