Target
Exchange humid air inside a box with fresh, dry air from outside. The difference between outside and inside air humidity should be small and (nearly) constant. The fan shall operate economic.
Frame
-> Humidity outside and inside the box are frequently changing.
-> The inside is always more humid than the outside.
-> Installed are:
- a PWM controlled fan in the boxes wall
- one air temperature/moisture sensor outside the box
- one air temperature/moisture sensor inside the box
Approach
The PWM controlled fan sucks fresh, dry air from outside in the box.
The two DHTs (22) are used to compare the air moisture values inside and outside the box. Based on their results the fan is being controlled.
The fan should rev as low as possible to save energy. It should be operated stepless.
Code
My code works somehow, but it's not correctly programmed.
I would want to understand how to do this better:
//-------------------------------------------
//----------Bibliotheken---------------------
//-------------------------------------------
#include <Arduino.h>
#include <DHT.h>
//-------------------------------------------
//----------DEFINITIONEN---------------------
//-------------------------------------------
#define DHTTYPE1 DHT22 // dht 22 sensor 1
#define DHTTYPE2 DHT22 // dht 22 sensor 2
//-----Pinbelegung Aktuatoren-----
const int Abluft = 11; //(PWM)
//-----Pinbelegung Sensoren-----
const int DHTPIN1 = A0; // dht 22 (NTC1) sensor 1
const int TachoAbluft = A3;
const int DHTPIN2 = A7; // dht 22 (NTC1_2)sensor 2
//-----Frequenz Serial Monitor/Display-----
unsigned long AnzeigeSensorwerte = 3000UL; // ANPASSEN !!!
//-----Millis-----
unsigned long previousTime3 = 0UL;
//-----Declarations NTC1-----
DHT dht1(DHTPIN1, DHTTYPE1);
DHT dht2(DHTPIN2, DHTTYPE2);
double h1, t1;
double h2, t2;
//-----Abluft-Drehzahl-----
int fanSpeed = 120; // Variable für die Lüftergeschwindigkeit
long tachoMillis = 1000UL; // Variable zum speichern der Zeit
float rps = 0; // Variable mit Kommastelle für die Berechnung der Umdrehungen pro Sekunde
int rpm = 0; // Variable für die gemittelte Drehzahl
float umdrZeit = 0; // Variable mit Kommastelle für die Zeit pro Umdrehung des Lüfters
float flankenZeit = 0; // Variable mit Kommastelle für die Zeit pro Puls des Lüfters
//---Abluft---
float RampSpeed = 0.2; // vorher: 0.5, Controls the changing fan speed ANPASSEN!!!
float CurrentSpeed = 100.00; // 1 Lüfter für Abluft 100% = 2500rpm ANPASSEN!!!
float DesiredSpeed = 0; // 1 Lüfter für Abluft 100% = 2500rpm ANPASSEN!!!
float FanMin = 0; // Minimum fan speed 0-79
float FanMax = 255; // Maximum fan speed 0-79
//---------------------------------------------------------------------------------------------------------------------------
//--------------------------------------- V O I D S E T U P ------------------------------------------------------------
//---------------------------------------------------------------------------------------------------------------------------
void setup() {
dht1.begin();
dht2.begin();
Serial.begin(115200);
//-----Pin-Mode Aktuatoren-----
pinMode(7, OUTPUT); // Abluft
digitalWrite(7, LOW);
pinMode(Abluft, OUTPUT);
analogWrite(Abluft, 255);
//-----Pin-Mode Sensoren-----
pinMode(A0, INPUT); //NTC1 //Inside moisture/temperature
pinMode(A3, INPUT); //Tacho Pin
pinMode(A7, INPUT); //NTC1_2 //Outside moisture/temperature
//-----Lüfterfrequenz-----
TCCR1B = TCCR1B & 0b11111000 | 0x01; // Setzt Timer1 (Pin 11 und 12) auf 31300Hz
TCCR3B = TCCR3B & 0b11111000 | 0x01; // Setzt Timer3 (Pin 2 etc.) auf 31300Hz
}
//---------------------------------------------------------------------------------------------------------------------------
//-------------------------------------------------- V O I D L O O P ----------------------------------------------------
//---------------------------------------------------------------------------------------------------------------------------
void loop() {
//-----NTC1/NTC1_2-----
float h1 = dht1.readHumidity();
float t1 = dht1.readTemperature();
float h2 = dht2.readHumidity();
float t2 = dht2.readTemperature();
//----------LÜFTER STEUERUNG----------
//---Abluftdefinition---
float FeuchteSoll = (h2) + 6; //Sollwert über Außenfeuchte.
float FeuchteSollMin = (h2);
//---Definition Lüftergeschwindigkeit---
if (h1 > FeuchteSoll) {
DesiredSpeed = FanMax;
}
if (h1 <= FeuchteSoll) {
DesiredSpeed = map(h1, FeuchteSollMin, FeuchteSoll, FanMin, FanMax); // Map the Fan Speed to the Duty Cycle
}
//---Abluftansteuerung---
if ((DesiredSpeed < CurrentSpeed) && ((CurrentSpeed - RampSpeed) > RampSpeed) ) {
CurrentSpeed = CurrentSpeed - (1 * RampSpeed);
} else if (DesiredSpeed < CurrentSpeed) {
CurrentSpeed = CurrentSpeed - 1;
}
if ((DesiredSpeed > CurrentSpeed) && ((CurrentSpeed + RampSpeed) > RampSpeed) ) {
CurrentSpeed = CurrentSpeed + (1 * RampSpeed);
} else if (DesiredSpeed > CurrentSpeed) {
CurrentSpeed = CurrentSpeed + 1;
}
analogWrite(Abluft, CurrentSpeed);
//-----Werte TachoAbluft-----
if ((millis() - tachoMillis) >= 1000UL) {
flankenZeit = pulseIn(TachoAbluft, LOW); // Abfrage der Zeit pro Puls in Mikrosekunden
umdrZeit = ((flankenZeit * 4) / 1000); // Berechnung der Zeit pro Umdrehung in Millisekunden Standard=4
rps = (1000 / umdrZeit); // Umrechnung auf Umdrehungen pro Sekunde
rpm = (rps * 6); // Schritt 1 zur Rundung auf 10er Schritte der Drehzahl
rpm = (rpm * 10); // Schritt 2 zur Rundung auf 10er Schritte der Drehzahl
tachoMillis = millis(); //update timing for next event
}
//-----------ANZEIGE----------
unsigned long currentTime3 = millis();
if (currentTime3 - previousTime3 >= AnzeigeSensorwerte) {
Serial.println(("NTC1_innen IST Temp|Hum: " + String(t1) + "°C") + ("|") + (String(h1) + "%") );
Serial.println("NTC1_innen SOLL |Hum: |" + String(FeuchteSoll) + "%" +(" Δ SOLL/IST: /" + String(h1 - FeuchteSoll) + "%"));
Serial.println(("NTC1_außen IST Temp|Hum: " + String(t2) + "°C") + ("|") + (String(h2) + "%")+(" Δ innen/außen: " + String(t1 - t2) + "°C") + ("/") + (String(h1 - h2) + "%") );
Serial.println((("Current|Desired Speed Abluft: "+ String(rpm) + " 1/min,") +" " + String(CurrentSpeed)+ "|" + String(DesiredSpeed)));
Serial.println("---------------------------------------------------------------------------------");
previousTime3 = currentTime3;
}
}
Help would be great!