Facebook
From Elias BEN AMAR, 3 Years ago, written in C++.
Embed
Download Paste or View Raw
Hits: 83
  1.  
  2. //===========================================
  3. //=========== VARIABLES DECLARATION =========
  4. //===========================================
  5.  
  6. const byte ledhiv = 7;                // Indication LED "Winter"
  7. const byte ledete = 8;                // Indication LED "Summer"
  8. const byte interruptPin = 3;          // Pin for Push Button to switch Summer to Winter
  9. const byte relay1 = 12;               // Relay to operat the Electric Transformer (230V)
  10. volatile long jour = 86400000;        // Millis in 24h
  11. unsigned long debut = 0;              // Variable to start counting a 24h or 12h sequence
  12. unsigned long gHiv = 120000;           // Time to irrigate during winter (120s)
  13. unsigned long gEte = 120000;           // Time to irrigate during winter (160s)
  14. volatile int season = 0;              // Variable for season in ISR
  15. int seasonapplied = season;           // Variable for season in void Lood (Is it necessary? Does Volatile In works in "Switch" and "If"?)
  16. int trigger = 0;                      // Button trigger: variable used to detect a change of season and turn the adequat ledPin High
  17. volatile long debounce = 0;           // To insure NO button bouncing
  18. volatile long test = 0;
  19. volatile long testint = 0;
  20.  
  21. //===========================================
  22. //===================== SETUP ===============
  23. //===========================================
  24.  
  25.  
  26.  
  27. void setup() {
  28.   Serial.begin(9600);                                                               // Start Serial Monitor to follow step on Screen (Problem solving)
  29.   Serial.println("--- Start Serial Monitor ---");                                   // Serial Monitor Information
  30.   pinMode(ledhiv, OUTPUT);                                                          // Set ledhiv pin as Output
  31.   pinMode(ledete, OUTPUT);                                                          // Set ledete pin as Output
  32.   pinMode(relay1, OUTPUT);                                                          // Set relay1 pin as Output
  33.   pinMode(interruptPin, INPUT_PULLUP);                                              // Set button pin as Interrupt
  34.   Serial.println("...");                                                            // Serial Monitor Information
  35.   Serial.println("--- PinMode Set");                                                // Serial Monitor Information
  36.   attachInterrupt(digitalPinToInterrupt(interruptPin), timetogive, FALLING);        // Interrupt mode Definition (Pin, ISR loop to execute, Mode)
  37.   Serial.println("--- Interrupt Set");                                              // Serial Monitor Information
  38.   digitalWrite (ledhiv, HIGH);
  39.   digitalWrite (relay1, LOW);                                                       // Set relay1 pin LOW => Power transformer ON (the Relay trigger "LOW")
  40.   Serial.println("--- Setup Watering initiated");                                   // Serial Monitor Information
  41.   delay (120000);                                                                    // Delay 120 seconds to water as Initialisation mode
  42.   digitalWrite (relay1, HIGH);                                                      // Set relay1 pin LOW => Watering OFF (the Relay trigger "LOW")
  43.   digitalWrite (ledhiv, LOW);
  44.   Serial.println("--- Setup Watering Finished: 30 seconds");                        // Serial Monitor Information
  45.   season = 0;                                                                       // Set season variable as Winter (0) - if Value = 1: Summer
  46.   Serial.print("Season : ");
  47.   Serial.println(season);
  48. }
  49.  
  50. //===========================================
  51. //======================== LOOP =============
  52. //===========================================
  53.  
  54.  
  55. void loop() {
  56.   seasonapplied = season;                                                           // copy the ISR variable value to the Variable used in the loop
  57.   if (seasonapplied == 0) {                                                         // Main IF : Season checking
  58.     if ((millis() - debut >= (jour - gHiv)) && (digitalRead(relay1) == HIGH)) {     // Compare "jour" Value - time to give in winter with the actual amount of millis (interval)
  59.       digitalWrite (relay1, LOW);                                                   // Set relay1 pin LOW => Power transformer ON (the Relay trigger "LOW")
  60.       Serial.print(" Season = ");                                                   // Serial Monitor Information
  61.       Serial.print(seasonapplied);                                                  // Serial Monitor Information
  62.       Serial.print(" --- Hiver watering ON: ");                                     // Serial Monitor Information
  63.       Serial.print(" millis = ");                                                   // Serial Monitor Information
  64.       Serial.println(millis());                                                     // Serial Monitor Information
  65.       digitalWrite (ledhiv, HIGH);                                                  // Turn On the LED indicating the WINTER mode is giving water
  66.     }
  67.  
  68.     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)
  69.       digitalWrite (relay1, HIGH);                                                    // Set relay1 pin HIGH => Watering OFF (the Relay trigger "LOW")
  70.       debut = millis();                                                               // Update of the value debut to check the following interval starting from this instant!
  71.       Serial.print(" Season = ");                                                     // Serial Monitor Information
  72.       Serial.print(seasonapplied);                                                    // Serial Monitor Information
  73.       Serial.print(" --- Hiver watering OFF: ");                                      // Serial Monitor Information
  74.       Serial.print(" millis = ");                                                     // Serial Monitor Information
  75.       Serial.println(millis());                                                       // Serial Monitor Information
  76.       digitalWrite (ledhiv, LOW);                                                     // Turn OHH the LED indicating the WINTER mode
  77.       season = 0;
  78.     }
  79.   }
  80.   if (seasonapplied == 1) {                                                             // Main IF : Season checking
  81.     if ((millis() - debut >= ((jour / 2) - gEte)) && (digitalRead(relay1) == HIGH)) {   // Compare "jour" Value - time to give in summer with the actual amount of millis (interval)
  82.       digitalWrite (relay1, LOW);                                                       // Set relay1 pin LOW => Power transformer ON (the Relay trigger "LOW")
  83.       Serial.print(" Season = ");                                                       // Serial Monitor Information
  84.       Serial.print(seasonapplied);                                                      // Serial Monitor Information
  85.       Serial.print(" --- Summer watering ON: ");                                        // Serial Monitor Information
  86.       Serial.print(" millis = ");                                                       // Serial Monitor Information
  87.       Serial.println(millis());                                                         // Serial Monitor Information
  88.       digitalWrite (ledete, HIGH);                                                      // Turn On the LED indicating the SUMMER mode
  89.     }
  90.     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)
  91.       digitalWrite (relay1, HIGH);                                                        // Set relay1 pin HIGH => Watering OFF (the Relay trigger "LOW")
  92.       debut = millis();                                                                   // Update of the value debut to check the following interval starting from this instant!
  93.       Serial.print(" Season = ");                                                         // Serial Monitor Information
  94.       Serial.print(seasonapplied);                                                        // Serial Monitor Information
  95.       Serial.print(" --- Summer watering OFF: ");                                         // Serial Monitor Information
  96.       Serial.print(" millis = ");                                                         // Serial Monitor Information
  97.       Serial.println(millis());                                                           // Serial Monitor Information
  98.       digitalWrite (ledete, LOW);                                                         // Turn OFF the LED indicating the SUMMER mode
  99.       season = 1;
  100.     }
  101.   }
  102.   if (trigger != season) {                                                              // This if detect the change of season with the varible volatile in the ISR. Then it light the appropriate LED.
  103.     trigger = season;                                                                   // We reset trigger as Season to detect further change
  104.     if (season == 0) {                                                                  // If variable season = 0 : winter light activated
  105.       digitalWrite (relay1, HIGH);                                                      // Set relay1 pin LOW => Power transformer OFF (the Relay trigger "LOW") FOR SAFETY : Don't let the water Run by mistake.
  106.       digitalWrite (ledhiv, HIGH);                                                      // Turn ON the LED indicating the WINTER mode. It will stay On until next watering.
  107.       digitalWrite (ledete, LOW);                                                       // Turn OFF the LED indicating the SUMMER mode
  108.       Serial.println("button has been pressed now Hiver mode");                         // Serial Monitor Information
  109.     }
  110.     else if (season == 1) {                                                                  // If variable season = 1 : summer light activated
  111.       digitalWrite (relay1, HIGH);                                                      // Set relay1 pin LOW => Power transformer OFF (the Relay trigger "LOW") FOR SAFETY : Don't let the water Run by mistake.
  112.       digitalWrite (ledete, HIGH);                                                      // Turn ON the LED indicating the SUMMER mode. It will stay On until next watering.
  113.       digitalWrite (ledhiv, LOW);                                                       // Turn OFF the LED indicating the WINTER mode
  114.       Serial.println("button has been pressed now summer mode");                        // Serial Monitor Information
  115.     }
  116.   }
  117. }
  118.  
  119. //===========================================
  120. //========================= ISR =============
  121. //===========================================
  122.  
  123. //=== ISR ===
  124. void timetogive() {                 // Name the ISR, it will be executed if the pushbutton pressed
  125. if (millis() - debounce > 500) {    //Comparison to avoid Bouncing Button!
  126. if (season == 1) {                  // Change the Variable Season value IF == 1 turn it to 0
  127. season = 0;
  128. }
  129. else if (season == 0) {             // Change the Variable Season value IF == 0 turn it to 1
  130. season = 1;
  131. }
  132. debounce = millis ();               //New value for further comparison
  133. }
  134. }
captcha