//=========================================== //=========== VARIABLES DECLARATION ========= //=========================================== const byte ledhiv = 7; // Indication LED "Winter" const byte ledete = 8; // Indication LED "Summer" const byte interruptPin = 3; // Pin for Push Button to switch Summer to Winter const byte relay1 = 12; // Relay to operat the Electric Transformer (230V) volatile long jour = 86400000; // Millis in 24h unsigned long debut = 0; // Variable to start counting a 24h or 12h sequence unsigned long gHiv = 120000; // Time to irrigate during winter (120s) unsigned long gEte = 120000; // Time to irrigate during winter (160s) volatile int season = 0; // Variable for season in ISR int seasonapplied = season; // Variable for season in void Lood (Is it necessary? Does Volatile In works in "Switch" and "If"?) int trigger = 0; // Button trigger: variable used to detect a change of season and turn the adequat ledPin High volatile long debounce = 0; // To insure NO button bouncing volatile long test = 0; volatile long testint = 0; //=========================================== //===================== SETUP =============== //=========================================== void setup() { Serial.begin(9600); // Start Serial Monitor to follow step on Screen (Problem solving) Serial.println("--- Start Serial Monitor ---"); // Serial Monitor Information pinMode(ledhiv, OUTPUT); // Set ledhiv pin as Output pinMode(ledete, OUTPUT); // Set ledete pin as Output pinMode(relay1, OUTPUT); // Set relay1 pin as Output pinMode(interruptPin, INPUT_PULLUP); // Set button pin as Interrupt Serial.println("..."); // Serial Monitor Information Serial.println("--- PinMode Set"); // Serial Monitor Information attachInterrupt(digitalPinToInterrupt(interruptPin), timetogive, FALLING); // Interrupt mode Definition (Pin, ISR loop to execute, Mode) Serial.println("--- Interrupt Set"); // Serial Monitor Information digitalWrite (ledhiv, HIGH); digitalWrite (relay1, LOW); // Set relay1 pin LOW => Power transformer ON (the Relay trigger "LOW") Serial.println("--- Setup Watering initiated"); // Serial Monitor Information delay (120000); // Delay 120 seconds to water as Initialisation mode digitalWrite (relay1, HIGH); // Set relay1 pin LOW => Watering OFF (the Relay trigger "LOW") digitalWrite (ledhiv, LOW); Serial.println("--- Setup Watering Finished: 30 seconds"); // Serial Monitor Information season = 0; // Set season variable as Winter (0) - if Value = 1: Summer Serial.print("Season : "); Serial.println(season); } //=========================================== //======================== LOOP ============= //=========================================== void loop() { seasonapplied = season; // copy the ISR variable value to the Variable used in the loop if (seasonapplied == 0) { // Main IF : Season checking if ((millis() - debut >= (jour - gHiv)) && (digitalRead(relay1) == HIGH)) { // Compare "jour" Value - time to give in winter with the actual amount of millis (interval) digitalWrite (relay1, LOW); // Set relay1 pin LOW => Power transformer ON (the Relay trigger "LOW") Serial.print(" Season = "); // Serial Monitor Information Serial.print(seasonapplied); // Serial Monitor Information Serial.print(" --- Hiver watering ON: "); // Serial Monitor Information Serial.print(" millis = "); // Serial Monitor Information Serial.println(millis()); // Serial Monitor Information digitalWrite (ledhiv, HIGH); // Turn On the LED indicating the WINTER mode is giving water } else if ((millis() - debut >= jour) && (digitalRead(relay1) == LOW)) { // Compare "jour" Value with the actual amount of millis (jour is the laps of time during watering (here 23h:59m:30s) digitalWrite (relay1, HIGH); // Set relay1 pin HIGH => Watering OFF (the Relay trigger "LOW") debut = millis(); // Update of the value debut to check the following interval starting from this instant! Serial.print(" Season = "); // Serial Monitor Information Serial.print(seasonapplied); // Serial Monitor Information Serial.print(" --- Hiver watering OFF: "); // Serial Monitor Information Serial.print(" millis = "); // Serial Monitor Information Serial.println(millis()); // Serial Monitor Information digitalWrite (ledhiv, LOW); // Turn OHH the LED indicating the WINTER mode season = 0; } } if (seasonapplied == 1) { // Main IF : Season checking if ((millis() - debut >= ((jour / 2) - gEte)) && (digitalRead(relay1) == HIGH)) { // Compare "jour" Value - time to give in summer with the actual amount of millis (interval) digitalWrite (relay1, LOW); // Set relay1 pin LOW => Power transformer ON (the Relay trigger "LOW") Serial.print(" Season = "); // Serial Monitor Information Serial.print(seasonapplied); // Serial Monitor Information Serial.print(" --- Summer watering ON: "); // Serial Monitor Information Serial.print(" millis = "); // Serial Monitor Information Serial.println(millis()); // Serial Monitor Information digitalWrite (ledete, HIGH); // Turn On the LED indicating the SUMMER mode } else if ((millis() - debut >= (jour / 2)) && (digitalRead(relay1) == LOW)) { // Compare "jour" Value with the actual amount of millis (jour is the laps of time during watering (here 11h:59m:00s) digitalWrite (relay1, HIGH); // Set relay1 pin HIGH => Watering OFF (the Relay trigger "LOW") debut = millis(); // Update of the value debut to check the following interval starting from this instant! Serial.print(" Season = "); // Serial Monitor Information Serial.print(seasonapplied); // Serial Monitor Information Serial.print(" --- Summer watering OFF: "); // Serial Monitor Information Serial.print(" millis = "); // Serial Monitor Information Serial.println(millis()); // Serial Monitor Information digitalWrite (ledete, LOW); // Turn OFF the LED indicating the SUMMER mode season = 1; } } if (trigger != season) { // This if detect the change of season with the varible volatile in the ISR. Then it light the appropriate LED. trigger = season; // We reset trigger as Season to detect further change if (season == 0) { // If variable season = 0 : winter light activated digitalWrite (relay1, HIGH); // Set relay1 pin LOW => Power transformer OFF (the Relay trigger "LOW") FOR SAFETY : Don't let the water Run by mistake. digitalWrite (ledhiv, HIGH); // Turn ON the LED indicating the WINTER mode. It will stay On until next watering. digitalWrite (ledete, LOW); // Turn OFF the LED indicating the SUMMER mode Serial.println("button has been pressed now Hiver mode"); // Serial Monitor Information } else if (season == 1) { // If variable season = 1 : summer light activated digitalWrite (relay1, HIGH); // Set relay1 pin LOW => Power transformer OFF (the Relay trigger "LOW") FOR SAFETY : Don't let the water Run by mistake. digitalWrite (ledete, HIGH); // Turn ON the LED indicating the SUMMER mode. It will stay On until next watering. digitalWrite (ledhiv, LOW); // Turn OFF the LED indicating the WINTER mode Serial.println("button has been pressed now summer mode"); // Serial Monitor Information } } } //=========================================== //========================= ISR ============= //=========================================== //=== ISR === void timetogive() { // Name the ISR, it will be executed if the pushbutton pressed if (millis() - debounce > 500) { //Comparison to avoid Bouncing Button! if (season == 1) { // Change the Variable Season value IF == 1 turn it to 0 season = 0; } else if (season == 0) { // Change the Variable Season value IF == 0 turn it to 1 season = 1; } debounce = millis (); //New value for further comparison } }