#include <Arduino.h> // Include the Arduino library
// Input pin definitions
const int inputComfort = 2;
const int inputExit = 3;
const int inputLock = 8;
const int inputUpFunction = A3; // Ændret fra 9 til A3
const int inputDownFunction = A4; // Ændret fra 10 til A4
const int inputWeather = A2; // Ændret fra 11 til A2
const int inputSpringSwitchRoofUp = A0; // Ændret fra 12 til A0
const int inputSpringSwitchRoofDown = A1; // Ændret fra 13 til A1
// Output pin definitions
const int outputUp = 5;
const int outputDown = 4;
const int outputWeatherDown = 6;
// Timing variables
const unsigned long weatherActivationDelay = 8000;
const unsigned long scenario1Timer = 10000;
const unsigned long scenario2Timer = 8000; // Ændret til 4000 millisekunder
const unsigned long scenario3FirstTrigger = 10000;
const unsigned long scenario3SecondTrigger = 10000;
const unsigned long minActionDelay = 1000;
const unsigned long scenario4Timer = 80000; // New timer for Scenario 4
const unsigned long scenario4Delay = 1000; // Delay i millisekunder (f.eks. 2 sekunder)
const unsigned long dailyClosingInterval = 120000; // 24 timer i millisekunder
const unsigned long weeklyMaintenanceDelay = 2000; // Ny konstant for 10000 millisekunder
const unsigned long weeklyMaintenanceInterval = 5000; // 7 dage i millisekunder
// Initial variables
unsigned long startMillis;
unsigned long timerStartMillis;
unsigned long lastWeatherActivation = 0;
unsigned long scenario4DeactivationTime = 0; // Tidsstempel for deaktivering
unsigned long lastDailyClosingTime = 0; // Tidsstempel for sidste daglige lukning
unsigned long lastWeeklyMaintenanceTime = 0; // Tidsstempel for sidste ugentlige vedligehold
unsigned long lastInputTime = 0; // Tidsstempel for sidste input
bool scenario1Triggered = false;
bool scenario2Triggered = false;
bool scenario3Triggered = false;
bool scenario4Triggered = false; // New scenario flag
bool scenario5Triggered = false;
bool scenario6Triggered = false;
bool scenario7Triggered = false;
bool scenario8Triggered = false;
bool weatherClosed = false;
bool firstRun = true; // Indikerer, at det er første kørsel
bool dailyClosingEnabled = false; // Manuelt aktiv/deaktiv flag for daglig lukning - skal ikke sættes til true her!!! men lidt længere nede
bool dailyClosingActive = false; // Global variabel til at spore, om daglig lukning er aktiv.
bool weeklyMaintenanceEnabled = true; // Manuelt aktiv/deaktiv flag for ugentlig vedligehold - skal ikke sættes til true her!!! men lidt længere nede
bool weeklyMaintenanceActive = false;
bool weeklyMaintenanceCompleted = false;
int upFunctionCounter = 0;
int activeScenario = 0;
bool previousComfort = HIGH;
bool previousExit = HIGH;
bool previousLock = HIGH;
bool initialRun = false; // Track if it’s the first program run
static int scenario4DeactivationCount = 0; // Tæller for antal deaktiveringer af Scenario 4
static int scenario2DeactivationCount = 0; // Tæller for antal deaktiveringer af Scenario 2
void setup() {
pinMode(inputComfort, INPUT_PULLUP);
pinMode(inputExit, INPUT_PULLUP);
pinMode(inputLock, INPUT_PULLUP);
pinMode(inputUpFunction, INPUT_PULLUP);
pinMode(inputDownFunction, INPUT_PULLUP);
pinMode(inputWeather, INPUT_PULLUP);
pinMode(inputSpringSwitchRoofUp, INPUT_PULLUP);
pinMode(inputSpringSwitchRoofDown, INPUT_PULLUP);
pinMode(outputUp, OUTPUT);
pinMode(outputDown, OUTPUT);
pinMode(outputWeatherDown, OUTPUT);
Serial.begin(9600);
// Manuelt sæt dailyClosingEnabled og weeklyMaintenanceEnabled til true eller false
dailyClosingEnabled = false; // Ændr til false, hvis du vil deaktivere scenariet
weeklyMaintenanceEnabled = false; // Ændr til false, hvis du vil deaktivere scenariet
lastWeeklyMaintenanceTime = 0;
}
void deactivateAllOutputs() {
digitalWrite(outputUp, LOW);
digitalWrite(outputDown, LOW);
digitalWrite(outputWeatherDown, LOW);
}
void handleScenarios(bool comfort, bool exit, bool lock, bool upFunction, bool downFunction, bool weather, bool springSwitchRoofUp, bool springSwitchRoofDown, unsigned long currentMillis) {
static bool lastUpFunction = upFunction;
static bool lastDownFunction = downFunction;
static bool upFunctionChanged = false;
static bool downFunctionChanged = false;
if (comfort || exit || lock || upFunction || downFunction || weather || springSwitchRoofUp || springSwitchRoofDown) {
lastInputTime = currentMillis; // Opdater sidste input tidspunkt
}
// Check for changes in upFunction and downFunction
if (upFunction != lastUpFunction) {
upFunctionChanged = true;
lastUpFunction = upFunction;
}
if (downFunction != lastDownFunction) {
downFunctionChanged = true;
lastDownFunction = downFunction;
}
// Scenario 1 logic with unique activation and reactivation delay
if (!comfort && !exit) {
if (upFunctionChanged || downFunctionChanged) {
/* scenario1Triggered = true; */
/* deactivateAllOutputs();*/
digitalWrite(outputWeatherDown, HIGH);
activeScenario = 1;
upFunctionCounter = 0;
Serial.println("Scenario 1 activated");
upFunctionChanged = false;
downFunctionChanged = false;
}
} else if (comfort && !exit) {
if (scenario1Triggered) {
digitalWrite(outputWeatherDown, LOW);
/* scenario1Triggered = false; */
activeScenario = 0;
Serial.println("Scenario 1 deactivated due to comfort change");
}
}
/*
void handleScenarios(bool comfort, bool exit, bool lock, bool upFunction, bool downFunction, bool weather, bool springSwitchRoofUp, bool springSwitchRoofDown, unsigned long currentMillis) {
static bool lastUpFunction = upFunction;
static bool lastDownFunction = downFunction;
static bool upFunctionChanged = false;
static bool downFunctionChanged = false;
if (comfort || exit || lock || upFunction || downFunction || weather || springSwitchRoofUp || springSwitchRoofDown) {
lastInputTime = currentMillis; // Opdater sidste input tidspunkt
}
// Check for changes in upFunction and downFunction
if (upFunction != lastUpFunction) {
upFunctionChanged = true;
lastUpFunction = upFunction;
}
if (downFunction != lastDownFunction) {
downFunctionChanged = true;
lastDownFunction = downFunction;
}
// Scenario 1 logic with unique activation and reactivation delay
if (!comfort && !exit && lock && upFunction && downFunction && !weather && springSwitchRoofUp && springSwitchRoofDown &&
(lastWeatherActivation == 0 || (currentMillis - lastWeatherActivation >= weatherActivationDelay)) && !scenario4Triggered && (currentMillis - scenario4DeactivationTime >= scenario4Delay) && !scenario3Triggered) {
if (upFunctionChanged || downFunctionChanged) {
scenario1Triggered = true;
deactivateAllOutputs();
digitalWrite(outputWeatherDown, HIGH);
activeScenario = 1;
upFunctionCounter = 0;
Serial.println("Scenario 1 activated");
upFunctionChanged = false;
downFunctionChanged = false;
}
} else if (!comfort && exit) {
if (scenario1Triggered) {
digitalWrite(outputWeatherDown, LOW);
scenario1Triggered = false;
activeScenario = 0;
Serial.println("Scenario 1 deactivated due to comfort change");
}
}
*/
/*
void handleScenarios(bool comfort, bool exit, bool lock, bool upFunction, bool downFunction, bool weather, bool springSwitchRoofUp, bool springSwitchRoofDown, unsigned long currentMillis) {
static bool lastUpFunction = upFunction;
static bool lastDownFunction = downFunction;
static bool upFunctionChanged = false;
static bool downFunctionChanged = false;
if (comfort || exit || lock || upFunction || downFunction || weather || springSwitchRoofUp || springSwitchRoofDown) {
lastInputTime = currentMillis; // Opdater sidste input tidspunkt
}
if (comfort || exit) {
if (scenario1Triggered || scenario2Triggered || scenario3Triggered || scenario4Triggered) {
deactivateAllOutputs();
scenario1Triggered = false;
scenario2Triggered = false;
scenario3Triggered = false;
scenario4Triggered = false;
upFunctionCounter = 0;
activeScenario = 0;
Serial.println("Comfort went high - Deactivating all scenarios.");
}
}
// Check for changes in upFunction and downFunction
if (upFunction != lastUpFunction) {
upFunctionChanged = true;
lastUpFunction = upFunction;
}
if (downFunction != lastDownFunction) {
downFunctionChanged = true;
lastDownFunction = downFunction;
}
// Scenario 1 logic with unique activation and reactivation delay
if (!scenario1Triggered && !comfort && !exit && lock && upFunction && downFunction && !weather && springSwitchRoofUp && springSwitchRoofDown &&
(lastWeatherActivation == 0 || (currentMillis - lastWeatherActivation >= weatherActivationDelay)) && !scenario4Triggered && (currentMillis - scenario4DeactivationTime >= scenario4Delay) && !scenario3Triggered) {
if (upFunctionChanged || downFunctionChanged) {
scenario1Triggered = true;
startMillis = currentMillis;
deactivateAllOutputs();
digitalWrite(outputWeatherDown, HIGH);
activeScenario = 1;
upFunctionCounter = 0;
Serial.println("Scenario 1 activated");
upFunctionChanged = false;
downFunctionChanged = false;
}
}
if (scenario1Triggered && (currentMillis - startMillis >= scenario1Timer)) {
digitalWrite(outputWeatherDown, LOW);
scenario1Triggered = false;
lastWeatherActivation = currentMillis;
activeScenario = 0;
Serial.println("Scenario 1 deactivated after timer");
}
*/
/*
void handleScenarios(bool comfort, bool exit, bool lock, bool upFunction, bool downFunction, bool weather, bool springSwitchRoofUp, bool springSwitchRoofDown, unsigned long currentMillis) {
if (comfort || exit || lock || upFunction || downFunction || weather || springSwitchRoofUp || springSwitchRoofDown) {
lastInputTime = currentMillis; // Opdater sidste input tidspunkt
}
if (comfort || exit) {
if (scenario1Triggered || scenario2Triggered || scenario3Triggered || scenario4Triggered) {
deactivateAllOutputs();
scenario1Triggered = false;
scenario2Triggered = false;
scenario3Triggered = false;
scenario4Triggered = false;
upFunctionCounter = 0;
activeScenario = 0;
Serial.println("Comfort went high - Deactivating all scenarios.");
}
}
// Scenario 1 logic with unique activation and reactivation delay
if (!scenario1Triggered && !comfort && !exit && lock && upFunction && downFunction && !weather && springSwitchRoofUp && springSwitchRoofDown &&
(lastWeatherActivation == 0 || (currentMillis - lastWeatherActivation >= weatherActivationDelay)) && !scenario4Triggered && (currentMillis - scenario4DeactivationTime >= scenario4Delay) && !scenario3Triggered) {
scenario1Triggered = true;
startMillis = currentMillis;
deactivateAllOutputs();
digitalWrite(outputWeatherDown, HIGH);
activeScenario = 1;
upFunctionCounter = 0;
Serial.println("Scenario 1 activated");
}
if (scenario1Triggered && (currentMillis - startMillis >= scenario1Timer)) {
digitalWrite(outputWeatherDown, LOW);
scenario1Triggered = false;
lastWeatherActivation = currentMillis;
activeScenario = 0;
Serial.println("Scenario 1 deactivated after timer");
}
*/
// Scenario 2 logic
if (!scenario3Triggered) {
handleScenario2(currentMillis, comfort, exit, lock, upFunction, downFunction, springSwitchRoofUp, springSwitchRoofDown);
}
// Scenario 3 logic if Scenario 2 is not active
if (!scenario2Triggered) {
handleScenario3(currentMillis, comfort, exit, lock, upFunction, downFunction, springSwitchRoofUp, springSwitchRoofDown);
}
// Scenario 4 logic (deactivated)
// handleScenario4(currentMillis, comfort, exit, lock, upFunction, downFunction, weather, springSwitchRoofUp, springSwitchRoofDown);
// Scenario 5 logic (previously Scenario 4)
handleScenario5(currentMillis, comfort, exit, lock, upFunction, downFunction, weather, springSwitchRoofUp, springSwitchRoofDown);
// Scenario 6 logic (previously Scenario 5)
handleScenario6(currentMillis, comfort, exit, lock, upFunction, downFunction, weather, springSwitchRoofUp, springSwitchRoofDown);
// Scenario 7 logic (previously Scenario 6)
handleScenario7(currentMillis, comfort, exit, lock, upFunction, downFunction, weather, springSwitchRoofUp, springSwitchRoofDown);
// Scenario 8 logic (previously Scenario 7)
handleScenario8(currentMillis, comfort, exit, lock, upFunction, downFunction, weather, springSwitchRoofUp, springSwitchRoofDown);
// Handle daily closing
handleDailyClosing(currentMillis, comfort, exit, lock, upFunction, downFunction, springSwitchRoofUp, springSwitchRoofDown);
// Weekly Maintenance logic
handleWeeklyMaintenance(currentMillis, comfort, exit, lock, upFunction, downFunction, springSwitchRoofUp, springSwitchRoofDown);
}
void handleScenario2(unsigned long currentMillis, bool comfort, bool exit, bool lock, bool upFunction, bool downFunction, bool springSwitchRoofUp, bool springSwitchRoofDown) {
if (!comfort && !exit && lock && upFunction && !downFunction && springSwitchRoofUp && springSwitchRoofDown &&
!scenario2Triggered && !scenario4Triggered && (currentMillis - scenario4DeactivationTime >= scenario4Delay)) {
scenario2Triggered = true;
timerStartMillis = currentMillis;
deactivateAllOutputs();
digitalWrite(outputDown, HIGH);
upFunctionCounter = 0;
Serial.println("Scenario 2 activated and outputDown set to HIGH");
}
if (scenario2Triggered) {
if (currentMillis - timerStartMillis >= scenario2Timer) {
// Deaktiver Scenario 2, når timeren løber ud
digitalWrite(outputDown, LOW);
scenario2Triggered = false;
scenario2DeactivationCount++; // Tæl deaktiveringer
Serial.println("Scenario 2 deactivated after timer");
} else if (comfort || exit || !lock || !upFunction ||
(!downFunction && (currentMillis - timerStartMillis > 1000)) ||
!springSwitchRoofUp || !springSwitchRoofDown) {
// Deaktiver Scenario 2, hvis et input ændrer sig
digitalWrite(outputDown, LOW);
scenario2Triggered = false;
scenario2DeactivationCount++; // Tæl deaktiveringer
scenario4DeactivationTime = currentMillis; // Registrer deaktiveringstidspunkt
Serial.println("Scenario 2 deactivated due to input change");
}
}
// Reset deaktiverings-tæller efter to deaktiveringer
if (scenario2DeactivationCount >= 2) {
scenario2DeactivationCount = 0;
scenario2Triggered = false;
Serial.println("Scenario 2 deactivation count reset, scenario2Triggered set to FALSE.");
}
}
void handleScenario3(unsigned long currentMillis, bool comfort, bool exit, bool lock, bool upFunction, bool downFunction, bool springSwitchRoofUp, bool springSwitchRoofDown) {
if (!comfort && !exit && lock && !upFunction && downFunction && springSwitchRoofUp && springSwitchRoofDown && !scenario3Triggered && !scenario4Triggered && (currentMillis - scenario4DeactivationTime >= scenario4Delay)) {
// Check if Scenario 2 is active
if (scenario2Triggered) {
// Deactivate Scenario 2
digitalWrite(outputDown, LOW);
scenario2Triggered = false;
Serial.println("Scenario 2 deactivated due to Scenario 3 activation");
}
scenario3Triggered = true;
upFunctionCounter++;
timerStartMillis = currentMillis;
if (upFunctionCounter == 1) {
digitalWrite(outputUp, HIGH);
Serial.println("Scenario 3 activated (1st trigger) and outputUp set to HIGH for 10 seconds");
} else if (upFunctionCounter == 2) {
digitalWrite(outputUp, HIGH);
Serial.println("Scenario 3 activated (2nd trigger) and outputUp set to HIGH for 10 seconds");
}
}
if (scenario3Triggered) {
Serial.print("Scenario 3 Triggered: upFunctionCounter = ");
Serial.print(upFunctionCounter);
Serial.print(", currentMillis - timerStartMillis = ");
Serial.println(currentMillis - timerStartMillis);
if (upFunctionCounter == 1 && (currentMillis - timerStartMillis >= scenario3FirstTrigger)) {
digitalWrite(outputUp, LOW);
scenario3Triggered = false;
Serial.println("Scenario 3 (1st trigger) deactivated after timer");
} else if (upFunctionCounter == 2 && (currentMillis - timerStartMillis >= scenario3SecondTrigger)) {
digitalWrite(outputUp, LOW);
scenario3Triggered = false;
Serial.println("Scenario 3 (2nd trigger) deactivated after timer");
}
else if (upFunctionCounter >= 3) {
digitalWrite(outputUp, LOW);
scenario3Triggered = false;
Serial.println("Scenario 3 (2nd trigger) deactivated after timer");
}
}
// Reset upFunctionCounter only when comfort changes from !comfort to comfort
if (previousComfort && !comfort && !exit) {
upFunctionCounter = 0;
}
}
void handleScenario5(unsigned long currentMillis, bool comfort, bool exit, bool lock, bool upFunction, bool downFunction, bool weather, bool springSwitchRoofUp, bool springSwitchRoofDown) {
if (exit && !comfort && lock && upFunction && !downFunction && springSwitchRoofUp && springSwitchRoofDown && !scenario5Triggered && (currentMillis - startMillis >= minActionDelay)) {
scenario5Triggered = true;
digitalWrite(outputDown, HIGH);
Serial.println("Scenario 5 activated and outputDown set to HIGH");
}
if (scenario5Triggered && (comfort || downFunction)) {
digitalWrite(outputDown, LOW);
scenario5Triggered = false;
Serial.println("Scenario 5 deactivated");
}
}
void handleScenario6(unsigned long currentMillis, bool comfort, bool exit, bool lock, bool upFunction, bool downFunction, bool weather, bool springSwitchRoofUp, bool springSwitchRoofDown) {
if (exit && !comfort && lock && !upFunction && downFunction && springSwitchRoofUp && springSwitchRoofDown && !scenario6Triggered && (currentMillis - startMillis >= minActionDelay)) {
scenario6Triggered = true;
digitalWrite(outputUp, HIGH);
Serial.println("Scenario 6 activated and outputUp set to HIGH");
}
if (scenario6Triggered && (comfort || upFunction)) {
digitalWrite(outputUp, LOW);
scenario6Triggered = false;
Serial.println("Scenario 6 deactivated");
}
}
void handleScenario7(unsigned long currentMillis, bool comfort, bool exit, bool lock, bool upFunction, bool downFunction, bool weather, bool springSwitchRoofUp, bool springSwitchRoofDown) {
if (exit && comfort && lock && upFunction && downFunction && springSwitchRoofUp && !springSwitchRoofDown && !scenario7Triggered && (currentMillis - startMillis >= minActionDelay)) {
scenario7Triggered = true;
digitalWrite(outputDown, HIGH);
Serial.println("Scenario 7 activated and outputDown set to HIGH");
}
if (scenario7Triggered && (!lock || springSwitchRoofDown)) {
digitalWrite(outputDown, LOW);
scenario7Triggered = false;
Serial.println("Scenario 7 deactivated");
}
}
void handleScenario8(unsigned long currentMillis, bool comfort, bool exit, bool lock, bool upFunction, bool downFunction, bool weather, bool springSwitchRoofUp, bool springSwitchRoofDown) {
if (exit && comfort && lock && upFunction && downFunction && !springSwitchRoofUp && springSwitchRoofDown && !scenario8Triggered && (currentMillis - startMillis >= minActionDelay)) {
scenario8Triggered = true;
digitalWrite(outputUp, HIGH);
Serial.println("Scenario 8 activated and outputUp set to HIGH");
}
if (scenario8Triggered && (!lock || springSwitchRoofUp)) {
digitalWrite(outputUp, LOW);
scenario8Triggered = false;
Serial.println("Scenario 8 deactivated");
}
}
void handleDailyClosing(unsigned long currentMillis, bool comfort, bool exit, bool lock, bool upFunction, bool downFunction, bool springSwitchRoofUp, bool springSwitchRoofDown) {
static unsigned long previousMillis = 0;
static int state = 0;
if (!dailyClosingEnabled) {
Serial.println("Daily Closing is disabled.");
return; // Hvis dailyClosingEnabled er false, skal vi ikke gøre noget
}
if (firstRun) {
lastDailyClosingTime = currentMillis;
firstRun = false;
Serial.println("First run, setting lastDailyClosingTime.");
return;
}
if (currentMillis - lastDailyClosingTime >= dailyClosingInterval) {
if (!comfort && !exit && !scenario1Triggered && !scenario2Triggered && !scenario3Triggered && dailyClosingEnabled) {
switch (state) {
case 0:
if (!dailyClosingActive) {
Serial.println("Activating Daily Closing.");
digitalWrite(outputDown, HIGH);
previousMillis = currentMillis;
dailyClosingActive = true;
state = 1;
}
break;
case 1:
if (currentMillis - previousMillis >= scenario2Timer) {
digitalWrite(outputDown, LOW);
lastDailyClosingTime = currentMillis;
dailyClosingActive = false;
state = 0;
Serial.println("Daily Closing completed. Resetting lastDailyClosingTime.");
}
break;
}
} else {
Serial.println("Comfort or Exit is not HIGH, not activating Daily Closing.");
}
} else {
Serial.println("Not enough time has passed since last Daily Closing.");
}
// Check if any input (except inputWeather) is active and dailyClosing is enabled
if (dailyClosingActive && (comfort || exit || !lock || !upFunction || !downFunction || !springSwitchRoofUp || !springSwitchRoofDown)) {
digitalWrite(outputDown, LOW);
dailyClosingActive = false; // Deactivate daily closing
lastDailyClosingTime = currentMillis; // Reset the timer
state = 0;
Serial.println("Daily Closing deactivated due to input change. Resetting timer.");
}
// Ensure outputs are not both active at the same time
if (digitalRead(outputUp) == HIGH && digitalRead(outputDown) == HIGH) {
digitalWrite(outputUp, LOW);
digitalWrite(outputDown, LOW);
Serial.println("Both outputUp and outputDown were HIGH. Resetting both to LOW.");
}
}
void handleWeeklyMaintenance(unsigned long currentMillis, bool comfort, bool exit, bool lock, bool upFunction, bool downFunction, bool springSwitchRoofUp, bool springSwitchRoofDown) {
static unsigned long previousMillis = 0;
static int state = 0;
static bool weeklyMaintenanceActive = false;
if (!weeklyMaintenanceEnabled) {
Serial.println("Weekly Maintenance is disabled.");
return;
}
if (firstRun) {
lastWeeklyMaintenanceTime = currentMillis;
firstRun = false;
Serial.println("First run, setting lastWeeklyMaintenanceTime.");
return;
}
if (currentMillis - lastWeeklyMaintenanceTime >= weeklyMaintenanceInterval) {
if (!comfort && !exit && !scenario1Triggered && !scenario2Triggered && !scenario3Triggered && weeklyMaintenanceEnabled) {
switch (state) {
case 0:
if (!weeklyMaintenanceActive) {
Serial.println("Activating Weekly Maintenance.");
digitalWrite(outputUp, HIGH);
previousMillis = currentMillis;
weeklyMaintenanceActive = true;
state = 1;
}
break;
case 1:
if (currentMillis - previousMillis >= scenario3FirstTrigger) {
digitalWrite(outputUp, LOW);
previousMillis = currentMillis;
state = 2;
}
break;
case 2:
if (currentMillis - previousMillis >= weeklyMaintenanceDelay) {
digitalWrite(outputDown, HIGH);
previousMillis = currentMillis;
state = 3;
}
break;
case 3:
if (currentMillis - previousMillis >= scenario2Timer) {
digitalWrite(outputDown, LOW);
lastWeeklyMaintenanceTime = currentMillis;
weeklyMaintenanceCompleted = true;
weeklyMaintenanceActive = false;
state = 0;
Serial.println("Weekly Maintenance completed. Resetting lastWeeklyMaintenanceTime.");
}
break;
}
} else {
Serial.println("Comfort or Exit is not HIGH, not activating Weekly Maintenance.");
}
} else {
Serial.println("Not enough time has passed since last Weekly Maintenance.");
}
// Check if any input (except inputWeather) is active and weeklyMaintenance is enabled
if (weeklyMaintenanceActive && (comfort || exit || !lock || !upFunction || !downFunction || !springSwitchRoofUp || !springSwitchRoofDown)) {
digitalWrite(outputUp, LOW);
digitalWrite(outputDown, LOW);
weeklyMaintenanceActive = false; // Deactivate weekly maintenance
lastWeeklyMaintenanceTime = currentMillis; // Reset the timer
state = 0;
Serial.println("Weekly Maintenance deactivated due to input change. Resetting timer.");
}
// Ensure outputs are not both active at the same time
if (digitalRead(outputUp) == HIGH && digitalRead(outputDown) == HIGH) {
digitalWrite(outputUp, LOW);
digitalWrite(outputDown, LOW);
Serial.println("Both outputUp and outputDown were HIGH. Resetting both to LOW.");
}
}
void loop() {
bool comfort = digitalRead(inputComfort) == HIGH;
bool exit = digitalRead(inputExit) == HIGH;
bool lock = digitalRead(inputLock) == HIGH;
bool upFunction = digitalRead(inputUpFunction) == HIGH;
bool downFunction = digitalRead(inputDownFunction) == HIGH;
bool weather = digitalRead(inputWeather) == HIGH;
bool springSwitchRoofUp = digitalRead(inputSpringSwitchRoofUp) == HIGH;
bool springSwitchRoofDown = digitalRead(inputSpringSwitchRoofDown) == HIGH;
unsigned long currentMillis = millis();
handleScenarios(comfort, exit, lock, upFunction, downFunction, weather, springSwitchRoofUp, springSwitchRoofDown, currentMillis);
previousComfort = comfort && exit;
previousExit = comfort;
previousLock = lock;
if (firstRun) {
firstRun = false; // Efter første gennemløb af loopet
}
Serial.print("Output Up: ");
Serial.println(digitalRead(outputUp) == HIGH ? "OPEN" : "CLOSED");
Serial.print("Output Down: ");
Serial.println(digitalRead(outputDown) == HIGH ? "OPEN" : "CLOSED");
Serial.print("Output Weather Down: ");
Serial.println(digitalRead(outputWeatherDown) == HIGH ? "OPEN" : "CLOSED");
}