Browse Source

replaced some vars by define statements

Signed-off-by: Stephan Richter <s.richter@srsoftware.de>
main
Stephan Richter 5 months ago
parent
commit
e10ebdb0d6
  1. 147
      Taupunkt/Taupunkt.ino

147
Taupunkt/Taupunkt.ino

@ -1,24 +1,40 @@
// include the library code: // load library for display
#include <LiquidCrystal.h> #include <LiquidCrystal.h>
// load library for DHT22 sensor
#include <DHT22.h> #include <DHT22.h>
int pinDHT22a = 7; #define TRESHOLD 2
DHT22 dht22a(pinDHT22a); #define HYSTERESIS 1
int status1 = 0; #define MIN_OUT_TEMP 5 // °C
int pinDHT22b = 6; #define RELAY_PIN 5
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);
float temp1 = 0, temp2 = 0, humi1 = 0, humi2 = 0; #define DHT22_OUTSIDE_PIN 6
float treshold = 2; #define DHT22_INSIDE_PIN 7
float hyster = 1;
float min_temp = 5;
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] = { byte degree[8] = {
0b00010, 0b00010,
0b00101, 0b00101,
@ -65,15 +81,14 @@ byte off[8] = {
void setup() { void setup() {
// set up the LCD's number of columns and rows: // set up the LCD
lcd.begin(16, 4); lcd.begin(16, 4);
lcd.createChar(0, degree); lcd.createChar(0, degree);
lcd.createChar(1, tau); lcd.createChar(1, tau);
lcd.createChar(2, on); lcd.createChar(2, on);
lcd.createChar(3, off); lcd.createChar(3, off);
pinMode(relay,OUTPUT);
pinMode(4,OUTPUT); // prepare "table" on LCD
digitalWrite(4,LOW);
lcd.setCursor(0,0); lcd.setCursor(0,0);
lcd.print(" IN OUT"); lcd.print(" IN OUT");
@ -91,6 +106,14 @@ void setup() {
lcd.print(" . . "); lcd.print(" . . ");
lcd.write((int)0); lcd.write((int)0);
lcd.print("C"); 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){ void printVal(float v){
@ -101,13 +124,13 @@ void printVal(float v){
void relayOff(){ void relayOff(){
lcd.setCursor(0,0); lcd.setCursor(0,0);
lcd.print("OFF"); lcd.print("OFF");
digitalWrite(relay,LOW); digitalWrite(RELAY_PIN,LOW);
} }
void relayOn(){ void relayOn(){
lcd.setCursor(0,0); lcd.setCursor(0,0);
lcd.print("ON "); lcd.print("ON ");
digitalWrite(relay,HIGH); digitalWrite(RELAY_PIN,HIGH);
} }
void fehler(int col, int code){ void fehler(int col, int code){
@ -147,84 +170,84 @@ float taupunkt(float t, float r) {
} }
void loop() { void loop() {
delay(2500); delay(2500); // dht 22 sensor may only be read every two seconds
temp1 = dht22a.getTemperature(); inside_temp = dht22inside.getTemperature();
humi1 = dht22a.getHumidity(); inside_humi = dht22inside.getHumidity();
status1 = dht22a.getLastError(); inside_state = dht22inside.getLastError();
temp2 = dht22b.getTemperature(); outside_temp = dht22outside.getTemperature();
humi2 = dht22b.getHumidity(); outside_humi = dht22outside.getHumidity();
status2 = dht22b.getLastError(); outside_state = dht22outside.getLastError();
boolean err1 = false; boolean inside_error = false, outside_error = false;
boolean err2 = false;
if (status1 != dht22a.OK || isnan(temp1)||isnan(humi1)) { if (inside_state != dht22inside.OK || isnan(inside_temp)||isnan(inside_humi)) {
fehler(5,status1); fehler(5,inside_state);
err1 = true; inside_error = true;
} else { } else {
if (temp1<-40 || temp1>80){ if (inside_temp<-40 || inside_temp>80){
lcd.setCursor(5,1); lcd.setCursor(5,1);
lcd.print("oor!"); lcd.print("OOR!"); // out of range!
err1 = true; inside_error = true;
} else { } else {
lcd.setCursor(5,1); 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.setCursor(5,2);
lcd.print("oor!"); lcd.print("OOR!"); // out of range
err1 = true; inside_error = true;
} else { } else {
lcd.setCursor(5,2); lcd.setCursor(5,2);
printVal(humi1); printVal(inside_humi);
} }
} }
if (status2 != dht22b.OK) { if (outside_state != dht22outside.OK) {
fehler(10,status2); fehler(10,outside_state);
err2 = true; outside_error = true;
} else { } else {
if (temp2<-40 || temp2>80){ if (outside_temp<-40 || outside_temp>80){
lcd.setCursor(10,1); lcd.setCursor(10,1);
lcd.print("oor!"); lcd.print("OOR!"); // out of range!
err2 = true; outside_error = true;
} else { } else {
lcd.setCursor(10,1); 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.setCursor(10,2);
lcd.print("oor!"); lcd.print("ORR!"); // out of range!
err2 = true; outside_error = true;
} else { } else {
lcd.setCursor(10,2); lcd.setCursor(10,2);
printVal(humi2); printVal(outside_humi);
} }
} }
lcd.setCursor(5,3); lcd.setCursor(5,3);
float tau1, tau2; float tau_outside, tau_inside;
if (err1){ if (inside_error){
lcd.print("----"); lcd.print("----");
} else { } else {
tau1 = taupunkt(temp1,humi1); tau_inside = taupunkt(inside_temp,inside_humi);
printVal(tau1); printVal(tau_inside);
} }
lcd.setCursor(10,3); lcd.setCursor(10,3);
if (err2){ if (outside_error){
lcd.print("----"); lcd.print("----");
} else { } else {
tau2 = taupunkt(temp2,humi2); tau_outside = taupunkt(outside_temp,outside_humi);
printVal(tau2); printVal(tau_outside);
} }
if (err1 || err2){ if (inside_error || outside_error || outside_temp < MIN_OUT_TEMP){
relayOff(); relayOff();
} else { } else {
if (tau1 > tau2 + treshold + hyster) { if (tau_inside > tau_outside + TRESHOLD + HYSTERESIS) {
relayOn(); relayOn();
} }
if (tau1 < tau2 + treshold) { if (tau_inside < tau_outside + TRESHOLD) {
relayOff(); relayOff();
} }
} }

Loading…
Cancel
Save