Simple melioration system with 5 water channels
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

247 lines
6.9KB

  1. // melioration 0.1
  2. #include <Arduino.h>
  3. #include <Thread.h>
  4. #include <DS1307RTC.h>
  5. const byte channel1 = 2;
  6. const byte channel2 = 3;
  7. const byte channel3 = 4;
  8. const byte channel4 = 5;
  9. const byte channel5 = 6;
  10. const byte led = 13;
  11. const byte button = 14; // A0
  12. long previousButtonMillis = 0;
  13. int buttonState = 0;
  14. long buttonPressed = 0;
  15. byte ledState = 0;
  16. byte activeChannel = 0;
  17. long channel5workingtime = 0;
  18. Thread pressButtonThread = Thread();
  19. void setup() {
  20. Serial.begin(9600);
  21. while (!Serial);
  22. setSyncProvider(RTC.get);
  23. if(timeStatus()!= timeSet)
  24. Serial.println("tt");
  25. pinMode(channel1, OUTPUT);
  26. digitalWrite(channel1, HIGH);
  27. pinMode(channel2, OUTPUT);
  28. digitalWrite(channel2, HIGH);
  29. pinMode(channel3, OUTPUT);
  30. digitalWrite(channel3, HIGH);
  31. pinMode(channel4, OUTPUT);
  32. digitalWrite(channel4, HIGH);
  33. pinMode(channel5, OUTPUT);
  34. digitalWrite(channel5, HIGH);
  35. pinMode(button, INPUT);
  36. pinMode(led, OUTPUT);
  37. pressButtonThread.onRun(pressButton);
  38. pressButtonThread.setInterval(200); // Interval for checking button pressing - 200ms
  39. digitalWrite(led, HIGH);
  40. signalblinking();
  41. serStr("strt");
  42. }
  43. void loop() {
  44. // Thread init:
  45. if (pressButtonThread.shouldRun())
  46. pressButtonThread.run();
  47. // Check RTC.
  48. if (timeStatus() == timeSet) {
  49. checkTime();
  50. } else {
  51. serStr("settime");
  52. delay (10000);
  53. }
  54. }
  55. // Check button pressing thread
  56. void pressButton() {
  57. unsigned long currentMillis = millis();
  58. if (channel5workingtime > 0) {
  59. if ((currentMillis - channel5workingtime) > 900000) { // 15 min channel5
  60. channelOff();
  61. delay(5000);
  62. channel5workingtime = 0;
  63. }
  64. }
  65. // Button on/off channel 5
  66. if (digitalRead(button) == LOW) buttonState = 1;
  67. else buttonState = 0;
  68. if (buttonState == 1) {
  69. buttonPressed=buttonPressed+200;
  70. if (buttonPressed > 5678) {
  71. serStr("long press");
  72. // nothing
  73. buttonPressed = 0;
  74. }
  75. previousButtonMillis = currentMillis;
  76. if (activeChannel == 5) {
  77. channelOff();
  78. channel5workingtime = 0;
  79. } else {
  80. channel5on();
  81. channel5workingtime = currentMillis;
  82. }
  83. }
  84. // Led indication
  85. ledState = ledState + 1;
  86. if (ledState < 6) {
  87. digitalWrite(led, LOW);
  88. } else if (ledState > 5) {
  89. digitalWrite(led, HIGH);
  90. }
  91. if (ledState > 9) ledState = 0;
  92. }
  93. // Send string to serial monitor with millis() counter and date/time
  94. void serStr(String serString) {
  95. long millisTime = millis();
  96. String delimiter = "|";
  97. String currentTime = "";
  98. currentTime = currentTime + year() + "-" + month() + "-" + day() + "-" + hour() + "-" + minute() + "-" + second();
  99. String stringToPrint = millisTime + delimiter + currentTime + delimiter + serString;
  100. Serial.println(stringToPrint);
  101. }
  102. void signalblinking() {
  103. int signalblink;
  104. for (signalblink = 1; signalblink <= 5; signalblink++) {
  105. digitalWrite(led, HIGH);
  106. delay(50);
  107. digitalWrite(led, LOW);
  108. delay(50);
  109. }
  110. }
  111. void channel1on() {
  112. if (activeChannel != 1) {
  113. signalblinking();
  114. digitalWrite(channel1, LOW);
  115. delay(1000);
  116. digitalWrite(channel2, HIGH);
  117. digitalWrite(channel3, HIGH);
  118. digitalWrite(channel4, HIGH);
  119. digitalWrite(channel5, HIGH);
  120. activeChannel = 1;
  121. }
  122. }
  123. void channel2on() {
  124. if (activeChannel != 2) {
  125. signalblinking();
  126. digitalWrite(channel2, LOW);
  127. delay(1000);
  128. digitalWrite(channel1, HIGH);
  129. digitalWrite(channel3, HIGH);
  130. digitalWrite(channel4, HIGH);
  131. digitalWrite(channel5, HIGH);
  132. activeChannel = 2;
  133. }
  134. }
  135. void channel3on() {
  136. if (activeChannel != 3) {
  137. signalblinking();
  138. digitalWrite(channel3, LOW);
  139. delay(1000);
  140. digitalWrite(channel1, HIGH);
  141. digitalWrite(channel2, HIGH);
  142. digitalWrite(channel4, HIGH);
  143. digitalWrite(channel5, HIGH);
  144. activeChannel = 3;
  145. }
  146. }
  147. void channel4on() {
  148. if (activeChannel != 4) {
  149. signalblinking();
  150. digitalWrite(channel4, LOW);
  151. delay(1000);
  152. digitalWrite(channel1, HIGH);
  153. digitalWrite(channel2, HIGH);
  154. digitalWrite(channel3, HIGH);
  155. digitalWrite(channel5, HIGH);
  156. activeChannel = 4;
  157. }
  158. }
  159. void channel5on() {
  160. if (activeChannel != 5) {
  161. unsigned long currentMillis = millis();
  162. if (channel5workingtime > 0) {
  163. if ((currentMillis - channel5workingtime) > 900000) { // 15 min channel5
  164. channelOff();
  165. delay(5000);
  166. channel5workingtime = 0;
  167. }
  168. }
  169. signalblinking();
  170. digitalWrite(channel5, LOW);
  171. delay(1000);
  172. digitalWrite(channel1, HIGH);
  173. digitalWrite(channel2, HIGH);
  174. digitalWrite(channel3, HIGH);
  175. digitalWrite(channel4, HIGH);
  176. channel5workingtime = currentMillis;
  177. activeChannel = 5;
  178. }
  179. digitalWrite(led, HIGH);
  180. }
  181. void channelOff() {
  182. if (activeChannel != 0) {
  183. signalblinking();
  184. digitalWrite(channel1, HIGH);
  185. digitalWrite(channel2, HIGH);
  186. digitalWrite(channel3, HIGH);
  187. digitalWrite(channel4, HIGH);
  188. digitalWrite(channel5, HIGH);
  189. activeChannel = 0;
  190. }
  191. }
  192. // Main Timer:
  193. void checkTime() {
  194. if (channel5workingtime == 0) {
  195. // 8-10 melioration
  196. if (hour() == 8 && minute() < 15) channel1on();
  197. if (hour() == 8 && minute() > 14 && minute() < 30) channel2on();
  198. if (hour() == 8 && minute() > 29 && minute() < 45) channel3on();
  199. if (hour() == 8 && minute() == 45) channelOff();
  200. if (hour() == 9 && minute() < 15) channel1on();
  201. if (hour() == 9 && minute() > 14 && minute() < 30) channel2on();
  202. if (hour() == 9 && minute() > 29 && minute() < 45) channel3on();
  203. if (hour() == 9 && minute() == 45) channelOff();
  204. // 13-15 melioration
  205. if (hour() == 13 && minute() < 15) channel1on();
  206. if (hour() == 13 && minute() > 14 && minute() < 30) channel2on();
  207. if (hour() == 13 && minute() > 29 && minute() < 45) channel3on();
  208. if (hour() == 13 && minute() == 45) channelOff();
  209. if (hour() == 14 && minute() < 15) channel1on();
  210. if (hour() == 14 && minute() > 14 && minute() < 30) channel2on();
  211. if (hour() == 14 && minute() > 29 && minute() < 45) channel3on();
  212. if (hour() == 14 && minute() == 45) channelOff();
  213. // 18-20 melioration
  214. if (hour() == 18 && minute() < 15) channel1on();
  215. if (hour() == 18 && minute() > 14 && minute() < 30) channel2on();
  216. if (hour() == 18 && minute() > 29 && minute() < 45) channel3on();
  217. if (hour() == 18 && minute() == 45) channelOff();
  218. if (hour() == 19 && minute() < 15) channel1on();
  219. if (hour() == 19 && minute() > 14 && minute() < 30) channel2on();
  220. if (hour() == 19 && minute() > 29 && minute() < 45) channel3on();
  221. if (hour() == 19 && minute() == 45) channelOff();
  222. if ((day() % 2) == 0) {
  223. // 1 hour per 2 days greenhouse melioration
  224. if (hour() == 8 && minute() > 45) channel4on();
  225. if (hour() == 9 && minute() > 45) channel4on();
  226. if (hour() == 10 && minute() == 2) channelOff();
  227. if (hour() == 18 && minute() > 45) channel4on();
  228. if (hour() == 19 && minute() > 45) channel4on();
  229. if (hour() == 20 && minute() == 2) channelOff();
  230. }
  231. }
  232. }