der Interrupt soll die Sekunden hochzählen, problem ist halt, dass der loop in einer Sekunde mehrmals durchlaufen kann. hätte aber auch ein einfache boolean geregelt. EEEglaaal
161 lines
3.8 KiB
Arduino
161 lines
3.8 KiB
Arduino
#include <DHT.h> //Setup Sensoren
|
|
#include <DHT_U.h>
|
|
#define DHTPIN1 8
|
|
#define DHTPIN2 9
|
|
#define DHTTYPE DHT22
|
|
DHT dht1(DHTPIN1, DHTTYPE);
|
|
DHT dht2(DHTPIN2, DHTTYPE);
|
|
|
|
#include <SD.h> //Setup SD SDK=D13, MOSI=D11, MISO=D12
|
|
|
|
#include <Wire.h> //Setup LCD
|
|
#include <LiquidCrystal_I2C.h>
|
|
LiquidCrystal_I2C lcd(0x3F, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE); //0x3F = Adresse des Displays
|
|
|
|
const int REL1 = 2; //Pin des Relais
|
|
const int REL2 = 3;
|
|
int stat1 = 1; //Status-Variable
|
|
int stat2 = 1;
|
|
|
|
float T1 = 0; //Temperatur-Variablen
|
|
float T2 = 0;
|
|
|
|
long logsek = 0;
|
|
String logdata1 = "";
|
|
String logdata2 = "";
|
|
|
|
//* **EINSTELLUNGEN** *//
|
|
const int mess = 10; //Messintervall in s !!! muss >2 wegen Sensoren
|
|
const int schalt = 15*60; //Schaltintervall in s
|
|
const int uT = 0; //Abschalt-Temperatur in °C
|
|
const int oT = 6; //Einschalt-Temperatur in °C
|
|
|
|
void setup() {
|
|
//Serial.begin(9600);
|
|
//Timer Setup: https://www.instructables.com/id/Arduino-Timer-Interrupts/
|
|
cli();//stop interrupts
|
|
//set timer1 interrupt at 1Hz
|
|
TCCR1A = 0;// set entire TCCR1A register to 0
|
|
TCCR1B = 0;// same for TCCR1B
|
|
TCNT1 = 0;//initialize counter value to 0
|
|
// set compare match register for 1hz increments
|
|
OCR1A = 15624;// = (16*10^6) / (1*1024) - 1 (must be <65536)
|
|
// turn on CTC mode
|
|
TCCR1B |= (1 << WGM12);
|
|
// Set CS10 and CS12 bits for 1024 prescaler
|
|
TCCR1B |= (1 << CS12) | (1 << CS10);
|
|
// enable timer compare interrupt
|
|
TIMSK1 |= (1 << OCIE1A);
|
|
sei();//allow interrupts
|
|
|
|
//Sensoren
|
|
dht1.begin(); //Starten der Sensoren
|
|
dht2.begin();
|
|
delay(2000);
|
|
|
|
//LCD
|
|
lcd.begin(16,2); //Starten des LCD, 16 Zeichen, 2 Zeilen
|
|
lcd.backlight(); //Beleuchtung des Displays einschalten
|
|
delay(1000);
|
|
|
|
//Initialsierugn SD
|
|
lcd.setCursor(0,0); //...(Zeichen,Zeile);
|
|
lcd.print("Initialisierung");
|
|
delay(2000);
|
|
if (!SD.begin(4)) { //Initialisiere SD_Karte mit CS auf Pin D4
|
|
lcd.setCursor(0,1);
|
|
lcd.print("fehlgeschlagen!");
|
|
delay(1000);
|
|
return;
|
|
}
|
|
lcd.setCursor(0,1);
|
|
lcd.print("abgeschlossen");
|
|
delay(1000);
|
|
|
|
pinMode(REL1, OUTPUT);
|
|
pinMode(REL2, OUTPUT);
|
|
digitalWrite(REL1, HIGH);
|
|
digitalWrite(REL2, HIGH);
|
|
delay(1000);
|
|
digitalWrite(REL1, LOW);
|
|
digitalWrite(REL2, LOW);
|
|
|
|
File logfile = SD.open("logTruhe.txt", FILE_WRITE); //Erstelle bzw. öffne log-Datei
|
|
logfile.println("t(min)\tTruhe\tT(°C)\tStatus");
|
|
logfile.close();
|
|
}
|
|
|
|
ISR(TIMER1_COMPA_vect){ //timer1 interrupt 1Hz
|
|
logsek++;
|
|
}
|
|
|
|
void loop() {
|
|
//Messung, LOG und LCD
|
|
if (logsek%mess == 0){
|
|
T1 = dht1.readTemperature();
|
|
logdata1 = String(logsek) + "\t1\t" + String(T1) + "\t" + String(stat1);
|
|
T2 = dht2.readTemperature();
|
|
logdata2 = String(logsek) + "\t2\t" + String(T2) + "\t" + String(stat2);
|
|
File logfile = SD.open("logTruhe.txt", FILE_WRITE);
|
|
logfile.println(logdata1);
|
|
logfile.println(logdata2);
|
|
logfile.println();
|
|
logfile.close();
|
|
//Serial.println("Messung + String(logsek)");
|
|
//LCD-Anzeige
|
|
lcd.setCursor(0,0); //...(Zeichen,Zeile);
|
|
lcd.print("Truhe1 ");
|
|
lcd.setCursor(7,0);
|
|
lcd.print(String(T1));
|
|
lcd.setCursor(11,0);
|
|
lcd.print("\337C");
|
|
lcd.setCursor(13,0);
|
|
if (stat1 == 1){
|
|
lcd.print(" an");
|
|
}
|
|
else{
|
|
lcd.print("aus");
|
|
}
|
|
lcd.setCursor(0,1);
|
|
lcd.print("Truhe2 ");
|
|
lcd.setCursor(7,1);
|
|
lcd.print(String(T2));
|
|
lcd.setCursor(11,1);
|
|
lcd.print("\337C");
|
|
lcd.setCursor(13,1);
|
|
if (stat2 == 1){
|
|
lcd.print(" an");
|
|
}
|
|
else{
|
|
lcd.print("aus");
|
|
}
|
|
delay(999);
|
|
}
|
|
|
|
//Schaltung
|
|
if (logsek%schalt == 0){
|
|
//Truhe 1
|
|
if (T1 >= oT){
|
|
digitalWrite(REL1, LOW);
|
|
stat1 = 1;
|
|
}
|
|
else {
|
|
if (T1 <= uT){
|
|
digitalWrite(REL1, HIGH);
|
|
stat1 = 0;
|
|
}
|
|
}
|
|
//Truhe 2
|
|
if (T2 >= oT){
|
|
digitalWrite(REL2, LOW);
|
|
stat2 = 1;
|
|
}
|
|
else {
|
|
if (T2 <= uT){
|
|
digitalWrite(REL2, HIGH);
|
|
stat2 = 0;
|
|
}
|
|
}
|
|
}
|
|
}
|