Guest User

Untitled

a guest
Nov 29th, 2023
17
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.83 KB | None | 0 0
  1. #include "schedule.h"
  2.  
  3. const int floatSensorPin = A1;
  4. const int refillSensorPin = A0;
  5.  
  6. const int dosingRelay1 = 7;
  7. const int dosingRelay2 = 3;
  8. const int dosingRelay3 = 4;
  9. const int dosingRelay4 = 5;
  10.  
  11. const int cleanerRelay = 2;
  12. const int circulationRelay = 6;
  13.  
  14. bool dosingRelaysActivated = false;
  15.  
  16. // set cleaner on period
  17. // set cleaner off period to 24 hours - cleaner on period
  18. const unsigned long cleanerOnInterval = 15*1000; // 15 seconds on
  19. const unsigned long cleanerOffInterval = 24*60*60*1000 - cleanerOnInterval;
  20.  
  21. // circulation on & off periods
  22. const unsigned long circulationOnTime = 60*1000; // 1 minute in milliseconds
  23. const unsigned long circulationOffTime = 2*60*1000; // 2 minutes in milliseconds
  24.  
  25. // check sensors every 1 second
  26. const unsigned long sensorReadInterval = 1*1000; // 1 second in milliseconds
  27.  
  28. // Individual dosing relay durations (in milliseconds)
  29. const unsigned long dosingRelayDuration1 = 75*1000; // Water refill duration
  30. const unsigned long dosingRelayDuration2 = 10*1000; // Nutrient A duration
  31. const unsigned long dosingRelayDuration3 = 10*1000; // Nutrient B duration
  32. const unsigned long dosingRelayDuration4 = 10*100; // Acid duration
  33.  
  34. // create the scheduler object, 4 slots and no abort handler
  35. Schedule schedule(4);
  36.  
  37.  
  38. // event handler to turn circulation off for two minutes
  39. void circ_event_off(void)
  40. {
  41. // turn off circulation relay, schedule turn-off in one minute
  42. digitalWrite(circulationRelay, HIGH);
  43. schedule.schedule(circulationOffTime, circ_event_off);
  44. }
  45.  
  46. // event handler to run circulation for circulationOnTime period
  47. void circ_event_on(void)
  48. {
  49. // Read the float sensor value
  50. int floatSensorValue = analogRead(floatSensorPin);
  51.  
  52. Serial.print("Float Sensor Value: ");
  53. Serial.println(floatSensorValue);
  54.  
  55. // Check if the float sensor is low (assuming a threshold value for low condition)
  56. if (floatSensorValue < 500)
  57. {
  58. // If the float sensor is low, do not activate the circulation pump
  59. digitalWrite(circulationRelay, HIGH);
  60. // and schedule turn-on again after a period (try again)
  61. schedule.schedule(circulationOffTime, circ_event_on);
  62. }
  63. else
  64. {
  65. // turn on circulation relay, schedule turn-off in circulationOnTime millis
  66. digitalWrite(circulationRelay, LOW);
  67. schedule.schedule(circulationOnTime, circ_event_off);
  68. }
  69. }
  70.  
  71. void read_sensors(void)
  72. {
  73. // Read the float sensor value
  74. int floatSensorValue = analogRead(floatSensorPin);
  75.  
  76. Serial.print("Float Sensor Value: ");
  77. Serial.println(floatSensorValue);
  78.  
  79. // Read the refill sensor value
  80. int refillSensorValue = analogRead(refillSensorPin);
  81.  
  82. Serial.print("Refill Sensor Value: ");
  83. Serial.println(refillSensorValue);
  84.  
  85. // Check if the refill sensor is non-buoyant
  86. if (refillSensorValue < 500) { // Assuming a threshold value for non-buoyant condition
  87. // Deactivate dosing relays
  88. deactivateDosingRelays();
  89. } else {
  90. // Check if the float sensor indicates a loss of buoyancy
  91. if (floatSensorValue < 500) { // Assuming a threshold value for loss of buoyancy
  92. if (!dosingRelaysActivated) {
  93. dosingRelaysActivated = true;
  94. activateDosingRelays();
  95. }
  96. } else {
  97. // If the float sensor is buoyant, deactivate dosing relays
  98. deactivateDosingRelays();
  99. }
  100. }
  101. }
  102.  
  103. void cleaner_on(void)
  104. {
  105. // run the cleaner for a period, then turn off
  106. digitalWrite(cleanerRelay, LOW);
  107. schedule.schedule(cleanerOnInterval, cleaner_off);
  108. }
  109.  
  110. void cleaner_off(void)
  111. {
  112. // turn the cleaner off, schedule turn-on for later
  113. digitalWrite(cleanerRelay, HIGH);
  114. schedule.schedule(cleanerOffInterval, cleaner_on);
  115. }
  116.  
  117. void activateDosingRelays()
  118. {
  119. // Activate the dosing relays with their specified durations
  120. digitalWrite(dosingRelay1, LOW);
  121. delay(dosingRelayDuration1);
  122. digitalWrite(dosingRelay1, HIGH);
  123. delay(100); // Delay to prevent interference
  124. digitalWrite(dosingRelay2, LOW);
  125. delay(dosingRelayDuration2);
  126. digitalWrite(dosingRelay2, HIGH);
  127. delay(100); // Delay to prevent interference
  128. digitalWrite(dosingRelay3, LOW);
  129. delay(dosingRelayDuration3);
  130. digitalWrite(dosingRelay3, HIGH);
  131. delay(100); // Delay to prevent interference
  132. digitalWrite(dosingRelay4, LOW);
  133. delay(dosingRelayDuration4);
  134. digitalWrite(dosingRelay4, HIGH);
  135. // Reset the dosing relay activation state
  136. dosingRelaysActivated = false;
  137. }
  138.  
  139. void deactivateDosingRelays()
  140. {
  141. // Deactivate all dosing relays
  142. digitalWrite(dosingRelay1, HIGH);
  143. digitalWrite(dosingRelay2, HIGH);
  144. digitalWrite(dosingRelay3, HIGH);
  145. digitalWrite(dosingRelay4, HIGH);
  146.  
  147. // Delay to prevent interference
  148. delay(100);
  149. }
  150.  
  151. void setup()
  152. {
  153. // Initialize serial communication
  154. Serial.begin(9600);
  155.  
  156. // Initialize relay pins as OUTPUT and set them to LOW (normally closed configuration)
  157. pinMode(dosingRelay1, OUTPUT);
  158. pinMode(dosingRelay2, OUTPUT);
  159. pinMode(dosingRelay3, OUTPUT);
  160. pinMode(dosingRelay4, OUTPUT);
  161. pinMode(cleanerRelay, OUTPUT);
  162. pinMode(circulationRelay, OUTPUT);
  163.  
  164. digitalWrite(dosingRelay1, HIGH);
  165. digitalWrite(dosingRelay2, HIGH);
  166. digitalWrite(dosingRelay3, HIGH);
  167. digitalWrite(dosingRelay4, HIGH);
  168. digitalWrite(cleanerRelay, HIGH);
  169.  
  170. pinMode(floatSensorPin, INPUT_PULLUP); // Use internal pull-up for float sensor
  171. pinMode(refillSensorPin, INPUT_PULLUP); // Use internal pull-up for refill sensor
  172.  
  173. // schedule initial events
  174. // this can be changed to start circulation after a delay, etc, if you want
  175. schedule.schedule(0, circ_event_on); // start circulation immediately
  176. schedule.schedule(sensorReadInterval, read_sensors); // read sensors every minute
  177. schedule.schedule(cleanerOffInterval, cleaner_on); // start cleaner in roughly 24 hours
  178. }
  179.  
  180. void loop()
  181. {
  182. // check if it's time for something to happen
  183. schedule.tick();
  184. }
  185.  
  186.  
Add Comment
Please, Sign In to add comment