Versuch mit Interrupt
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
This commit is contained in:
parent
7dda56c78d
commit
2632278569
@ -14,139 +14,147 @@ LiquidCrystal_I2C lcd(0x3F, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE); //0x3F = Adresse
|
||||
|
||||
const int REL1 = 2; //Pin des Relais
|
||||
const int REL2 = 3;
|
||||
int stat1 = 0; //Status-Variable
|
||||
int stat2 = 0;
|
||||
int stat1 = 1; //Status-Variable
|
||||
int stat2 = 1;
|
||||
|
||||
float T1 = 0; //Temperatur-Variablen
|
||||
float T2 = 0;
|
||||
|
||||
unsigned long zeit1 = 0; //Zeitstempel für Relais
|
||||
unsigned long zeit2 = 0;
|
||||
int uptime = 0; //Zeitstempel für log in min
|
||||
int logmin = 0;
|
||||
int logsek = 0;
|
||||
long logsek = 0;
|
||||
String logdata1 = "";
|
||||
String logdata2 = "";
|
||||
|
||||
//* **EINSTELLUNGEN** *//
|
||||
const int mess = 5; //Messintervall in s !!! muss >2 wegen Sensoren
|
||||
const int truhe = 30; //Schaltintervall in min
|
||||
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(2000);
|
||||
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(2000);
|
||||
delay(1000);
|
||||
return;
|
||||
}
|
||||
lcd.setCursor(0,1);
|
||||
lcd.print("abgeschlossen");
|
||||
delay(2000);
|
||||
|
||||
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 und log
|
||||
T1 = dht1.readTemperature();
|
||||
logdata1 = String(logmin) + ":" + String(logsek) + "\t1\t" + String(T1) + "\t" + String(stat1);
|
||||
T2 = dht2.readTemperature();
|
||||
logdata2 = String(logmin) + ":" + 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");
|
||||
//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 Truhe 1
|
||||
if (millis() >= zeit1){
|
||||
//Schaltung
|
||||
if (logsek%schalt == 0){
|
||||
//Truhe 1
|
||||
if (T1 >= oT){
|
||||
digitalWrite(REL1, LOW);
|
||||
stat1 = 1;
|
||||
zeit1 = millis()+(truhe*60*1000);
|
||||
}
|
||||
else {
|
||||
if (T1 <= uT){
|
||||
digitalWrite(REL1, HIGH);
|
||||
stat1 = 0;
|
||||
zeit1 = millis()+(truhe*60*1000);
|
||||
}
|
||||
}
|
||||
}
|
||||
else{
|
||||
}
|
||||
//Schaltung Truhe 2
|
||||
if (millis() >= zeit2){
|
||||
//Truhe 2
|
||||
if (T2 >= oT){
|
||||
digitalWrite(REL2, LOW);
|
||||
stat2 = 1;
|
||||
zeit2 = millis()+(truhe*60*1000);
|
||||
}
|
||||
else {
|
||||
if (T2 <= uT){
|
||||
digitalWrite(REL2, HIGH);
|
||||
stat2 = 0;
|
||||
zeit2 = millis()+(truhe*60*1000);
|
||||
}
|
||||
}
|
||||
}
|
||||
else{
|
||||
}
|
||||
//Serial.println("Schaltung");
|
||||
|
||||
//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(mess*1000);
|
||||
uptime += mess;
|
||||
logmin = uptime/60;
|
||||
logsek = uptime%60;
|
||||
//Serial.println("LCD");
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user