Und wenn das programm1 am ende ist soll das programm1 resetet werden (also auch fade auf 0) und verlassen werden, so das ich es wiederverwenden kann wenn ich den knopf drücke.
Nach Deiner Definition von Ende passiert das bereits:
analogWrite(ledPin, 0);
Die Variable wird zwar nicht auf 0 gesetzt, das passiert aber beim nächsten Durchlaufen der Routine programm1 automatisch, wenn sie wieder gebraucht wird.
Und der reset knopf während des programms funktioniert auch nicht, die led fadet komischerweise nicht und die relais ( k1,..) bleiben auch an.
Ich denke, es wird Zeit, dass Du erklärst, wass die Relais machen, welche Temperatur wofür gemessen wird, etc. Damit kann ich mir ein Bild machen, was das eigentlich abläuft und kann erkennen, was sinnvoll ist. Im Moment ist das Programm so gestaltet, dass es das macht, was Du beschrieben hast. Da Deine Beschreibung scheinbar nur einen Teil der Wirklichkeit enthält, macht das Programm noch nicht was Du willst. Das bessert sich hoffentlich, wenn ich auch verstehe, was dahinter steckt.
Diese Version sollte Deine Pause enthalten, Du musst aber einen neuen Taster für die Pause anschliessen.
#include <OneWire.h>
#include <DallasTemperature.h>
#include <LiquidCrystal.h>
#define ONE_WIRE_BUS 30 // Temperatursensor pin 30
#define FADE_DELAY 3 // Verzögerung beim Faden
#define SENSOR_PERIODE 400 // wie oft werden die Sensoren ausgelesen
LiquidCrystal lcd(22, 23, 24, 25, 26, 27, 28);
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);
DeviceAddress Sensor[] = {{0x28, 0xCD, 0x9B, 0xDA, 0x03, 0x00, 0x00, 0xF2},
{0x28, 0x1F, 0xA3, 0xDA, 0x03, 0x00, 0x00, 0xE2},
{0x28, 0x2D, 0xB9, 0xDA, 0x03, 0x00, 0x00, 0xA0},
{0x28, 0x37, 0x88, 0xDA, 0x03, 0x00, 0x00, 0x7D}};
int backLight = 29;
uint8_t k[] = {31, 32, 33, 35}; //relais HIGH = Aus; LOW = An
uint8_t startButton = 34;
uint8_t pauseButton = 30;
uint8_t startButtonPushCounter = 0;
uint8_t pauseButtonPushCounter = 0;
uint8_t lastStartButtonState = 0;
uint8_t lastPauseButtonState = 0;
uint32_t previousMillis = 0;
uint32_t interval = 31000;
uint32_t zeit = 31000;
int mpxPin = 5; //drucksensor
int mpx;
float pkPa;
uint8_t ledPin = 9; // (kemo)
uint8_t fade;
uint8_t status;
uint32_t sensorTime = 0;
uint32_t nextMillis = 0;
uint32_t thresholdMillis = 0;
uint32_t nmSave = 0;
uint32_t tmSave = 0;
void setup() {
pinMode(backLight, OUTPUT);
digitalWrite(backLight, HIGH);
lcd.begin(20, 4);
sensors.begin();
uint8_t i;
for (i = 0; i < 4; i++) {
pinMode(k[i], OUTPUT);
digitalWrite(k[i], HIGH);
}
pinMode(knopf1, INPUT);
status=0;
sensors.setWaitForConversion(false);
sensors.requestTemperatures();
sensorTime = millis() + SENSOR_PERIODE;
}
void loop(){
// check if conversion is done
if (millis() > sensorTime && oneWire.read_bit()) {
uint8_t i;
sensorTime += SENSOR_PERIODE;
for (i = 0; i < 4; i++) {
lcd.setCursor(0, i);
lcd.print("T");
lcd.print(i+1, DEC);
lcd.print(":");
if (sensors.isConnected(Sensor[i])) {
lcd.print(sensors.getTempC(Sensor[i]));
lcd.print("C");
} else {
lcd.print("AUS ");
}
}
// start next conversion
sensors.requestTemperatures();
mpx = analogRead(mpxPin);
pkPa = (mpx/1023.0-0.04)/0.0018;
lcd.setCursor(10, 0);
lcd.print(pkPa);
lcd.print("mb ");
}
uint8_t buttonState = digitalRead(startButton);
if (buttonState != lastStartButtonState && buttonState == HIGH) {
startButtonPushCounter++;
}
lastStartButtonState = buttonState;
buttonState = digitalRead(pauseButton);
if (buttonState != lastPauseButtonState && buttonState == HIGH) {
pauseButtonPushCounter++;
}
lastPauseButtonState = buttonState;
if (pauseButtonPushCounter & 0x01) {
// activate paused state
nmSaved = nextMillis;
nextMillis = 0;
tmSaved = thresholdMillis;
thresholdMillis = 0;
} else {
// recover after paused state
nextMillis = nmSaved;
nmSaved = 0;
thresholdMillis = tmSaved;
tmSaved = 0;
}
if (startButtonPushCounter & 0x01 == 0) {
// deactivate all relays, turn off LED and reset status
status = 0;
analogWrite(ledPin, 0);
for (uint8_t i = 0; i < 4; i++) {
digitalWrite(k[i], HIGH);
}
} else if (nmSaved == 0) { // run programm1 only if not in paused state
programm1();
}
}
void programm1() {
if (sensors.getTempC(Sensor[0]) >= 25.8 && status == 0) {
digitalWrite(k[0], HIGH);
status = 1;
nextMillis = millis() + 60000L;
} else if (status == 1 && millis() > nextMillis) {
digitalWrite(k[1], LOW);
digitalWrite(k[2], LOW);
digitalWrite(k[3], LOW);
fade = 0;
analogWrite(ledPin, fade);
nextMillis += FADE_DELAY;
status = 2;
} else if (status == 2 && millis() > nextMillis) {
nextMillis += FADE_DELAY;
analogWrite(ledPin, ++fade);
if (fade >= 51) {
status = 3;
nextMillis = millis() + 120000L;
}
} else if (status == 3 && millis() > nextMillis) {
nextMillis += FADE_DELAY;
analogWrite(ledPin, ++fade);
status = 4;
} else if (status == 4 && millis() > nextMillis) {
nextMillis += FADE_DELAY;
analogWrite(ledPin, ++fade);
if (fade >= 102) {
status = 5;
nextMillis = millis() + 180000L;
}
} else if (status == 5 && millis() > nextMillis) {
nextMillis += FADE_DELAY;
analogWrite(ledPin, ++fade);
status = 6;
} else if (status == 6 && millis() > nextMillis) {
nextMillis += FADE_DELAY;
analogWrite(ledPin, ++fade);
if (fade >= 153) {
status = 7;
nextMillis = millis() + 240000L;
}
} else if (status == 7 && millis() > nextMillis) {
nextMillis += FADE_DELAY;
analogWrite(ledPin, ++fade);
status = 8;
} else if (status == 8 && millis() > nextMillis) {
nextMillis += FADE_DELAY;
analogWrite(ledPin, ++fade);
if (fade >= 255) {
status = 9;
}
}
if (sensors.getTempC(Sensor[0]) >= 99.0 && thresholdMillis == 0) {
digitalWrite(k[1], HIGH);
thresholdMillis = millis() + 300000L;
} else if (thresholdMillis && millis() > thresholdMillis) {
digitalWrite(k[2], HIGH);
digitalWrite(k[3], HIGH);
analogWrite(ledPin, 0);
thresholdMillis = 0;
nextMillis = 0;
status = 0;
}
}