From e10ebdb0d622c2ad0c07f0af657634e8c380d5ce Mon Sep 17 00:00:00 2001 From: Stephan Richter Date: Sat, 13 Jul 2024 21:15:49 +0200 Subject: [PATCH] replaced some vars by define statements Signed-off-by: Stephan Richter --- Taupunkt/Taupunkt.ino | 145 ++++++++++++++++++++++++------------------ 1 file changed, 84 insertions(+), 61 deletions(-) diff --git a/Taupunkt/Taupunkt.ino b/Taupunkt/Taupunkt.ino index 5a46527..82be2c5 100644 --- a/Taupunkt/Taupunkt.ino +++ b/Taupunkt/Taupunkt.ino @@ -1,24 +1,40 @@ -// include the library code: +// load library for display #include +// load library for DHT22 sensor #include -int pinDHT22a = 7; -DHT22 dht22a(pinDHT22a); -int status1 = 0; +#define TRESHOLD 2 +#define HYSTERESIS 1 +#define MIN_OUT_TEMP 5 // °C -int pinDHT22b = 6; -DHT22 dht22b(pinDHT22b); -int status2 = 0; -const int rs = 13, en = 12, d4 = 11, d5 = 10, d6 = 9, d7 = 8; -LiquidCrystal lcd(rs, en, d4, d5, d6, d7); +#define RELAY_PIN 5 -float temp1 = 0, temp2 = 0, humi1 = 0, humi2 = 0; -float treshold = 2; -float hyster = 1; -float min_temp = 5; +#define DHT22_OUTSIDE_PIN 6 +#define DHT22_INSIDE_PIN 7 -int relay = 5; +#define LCD_D7_PIN 8 +#define LCD_D6_PIN 9 +#define LCD_D5_PIN 10 +#define LCD_D4_PIN 11 +#define LCD_EN_PIN 12 +#define LCD_RS_PIN 13 + +// create objects for sensor communication +DHT22 dht22inside(DHT22_INSIDE_PIN); +DHT22 dht22outside(DHT22_OUTSIDE_PIN); + +// create object for LCD communication +LiquidCrystal lcd(LCD_RS_PIN, LCD_EN_PIN, LCD_D4_PIN, LCD_D5_PIN, LCD_D6_PIN, LCD_D7_PIN); + +// these variables are used to capture error states of the sensors: +int inside_state = 0; +int outside_state = 0; + +// these variables hold measures from the sensors +float inside_temp = 0, outside_temp = 0, inside_humi = 0, outside_humi = 0; + +// these arrays are used to create some special characters for the LCD byte degree[8] = { 0b00010, 0b00101, @@ -65,15 +81,14 @@ byte off[8] = { void setup() { - // set up the LCD's number of columns and rows: + // set up the LCD lcd.begin(16, 4); lcd.createChar(0, degree); lcd.createChar(1, tau); lcd.createChar(2, on); lcd.createChar(3, off); - pinMode(relay,OUTPUT); - pinMode(4,OUTPUT); - digitalWrite(4,LOW); + + // prepare "table" on LCD lcd.setCursor(0,0); lcd.print(" IN OUT"); @@ -91,6 +106,14 @@ void setup() { lcd.print(" . . "); lcd.write((int)0); lcd.print("C"); + + + // set up the relay + pinMode(RELAY_PIN,OUTPUT); + + // TODO: is this needed any longer? + pinMode(4,OUTPUT); + digitalWrite(4,LOW); } void printVal(float v){ @@ -101,13 +124,13 @@ void printVal(float v){ void relayOff(){ lcd.setCursor(0,0); lcd.print("OFF"); - digitalWrite(relay,LOW); + digitalWrite(RELAY_PIN,LOW); } void relayOn(){ lcd.setCursor(0,0); lcd.print("ON "); - digitalWrite(relay,HIGH); + digitalWrite(RELAY_PIN,HIGH); } void fehler(int col, int code){ @@ -147,84 +170,84 @@ float taupunkt(float t, float r) { } void loop() { - delay(2500); - temp1 = dht22a.getTemperature(); - humi1 = dht22a.getHumidity(); - status1 = dht22a.getLastError(); - temp2 = dht22b.getTemperature(); - humi2 = dht22b.getHumidity(); - status2 = dht22b.getLastError(); + delay(2500); // dht 22 sensor may only be read every two seconds + inside_temp = dht22inside.getTemperature(); + inside_humi = dht22inside.getHumidity(); + inside_state = dht22inside.getLastError(); + outside_temp = dht22outside.getTemperature(); + outside_humi = dht22outside.getHumidity(); + outside_state = dht22outside.getLastError(); + + boolean inside_error = false, outside_error = false; - boolean err1 = false; - boolean err2 = false; - if (status1 != dht22a.OK || isnan(temp1)||isnan(humi1)) { - fehler(5,status1); - err1 = true; + if (inside_state != dht22inside.OK || isnan(inside_temp)||isnan(inside_humi)) { + fehler(5,inside_state); + inside_error = true; } else { - if (temp1<-40 || temp1>80){ + if (inside_temp<-40 || inside_temp>80){ lcd.setCursor(5,1); - lcd.print("oor!"); - err1 = true; + lcd.print("OOR!"); // out of range! + inside_error = true; } else { lcd.setCursor(5,1); - printVal(temp1); + printVal(inside_temp); } - if (humi1<0 || humi1>100){ + if (inside_humi<0 || inside_humi>100){ lcd.setCursor(5,2); - lcd.print("oor!"); - err1 = true; + lcd.print("OOR!"); // out of range + inside_error = true; } else { lcd.setCursor(5,2); - printVal(humi1); + printVal(inside_humi); } } - if (status2 != dht22b.OK) { - fehler(10,status2); - err2 = true; + if (outside_state != dht22outside.OK) { + fehler(10,outside_state); + outside_error = true; } else { - if (temp2<-40 || temp2>80){ + if (outside_temp<-40 || outside_temp>80){ lcd.setCursor(10,1); - lcd.print("oor!"); - err2 = true; + lcd.print("OOR!"); // out of range! + outside_error = true; } else { lcd.setCursor(10,1); - printVal(temp2); + printVal(outside_temp); } - if (humi2<0 || humi2>100){ + if (outside_humi<0 || outside_humi>100){ lcd.setCursor(10,2); - lcd.print("oor!"); - err2 = true; + lcd.print("ORR!"); // out of range! + outside_error = true; } else { lcd.setCursor(10,2); - printVal(humi2); + printVal(outside_humi); } } lcd.setCursor(5,3); - float tau1, tau2; - if (err1){ + float tau_outside, tau_inside; + if (inside_error){ lcd.print("----"); } else { - tau1 = taupunkt(temp1,humi1); - printVal(tau1); + tau_inside = taupunkt(inside_temp,inside_humi); + printVal(tau_inside); } lcd.setCursor(10,3); - if (err2){ + if (outside_error){ lcd.print("----"); } else { - tau2 = taupunkt(temp2,humi2); - printVal(tau2); + tau_outside = taupunkt(outside_temp,outside_humi); + printVal(tau_outside); } - if (err1 || err2){ + if (inside_error || outside_error || outside_temp < MIN_OUT_TEMP){ relayOff(); } else { - if (tau1 > tau2 + treshold + hyster) { + if (tau_inside > tau_outside + TRESHOLD + HYSTERESIS) { relayOn(); } - if (tau1 < tau2 + treshold) { + if (tau_inside < tau_outside + TRESHOLD) { relayOff(); } }