DIY fertilizer mixer and plant watering machine https://www.xythobuz.de/giessomat.html
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.

Plants.cpp 2.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. #include <Arduino.h>
  2. #include "DebugLog.h"
  3. #include "Plants.h"
  4. #include "config.h"
  5. #include "config_pins.h"
  6. // valves: no of plants + 1 for water inlet
  7. // pumps: no of fertilizers
  8. // switches: 2, low and high level
  9. Plants::Plants(int valve_count, int pump_count, int switch_count) :
  10. valves(valve_count), pumps(pump_count), switches(switch_count) {
  11. }
  12. GPIOBank *Plants::getValves(void) {
  13. return &valves;
  14. }
  15. GPIOBank *Plants::getPumps(void) {
  16. return &pumps;
  17. }
  18. GPIOBank *Plants::getSwitches(void) {
  19. return &switches;
  20. }
  21. void Plants::setValvePins(int pins[]) {
  22. valves.setPinNumbers(pins);
  23. valves.setOutput();
  24. valves.setAll(false);
  25. }
  26. void Plants::setPumpPins(int pins[]) {
  27. pumps.setPinNumbers(pins);
  28. pumps.setOutput();
  29. pumps.setAll(false);
  30. }
  31. void Plants::setSwitchPins(int pins[], bool pullup) {
  32. switches.setPinNumbers(pins);
  33. switches.setInput(pullup);
  34. }
  35. void Plants::abort(void) {
  36. closeWaterInlet();
  37. stopAllFertilizers();
  38. stopAllPlants();
  39. }
  40. Plants::Waterlevel Plants::getWaterlevel(void) {
  41. bool low = switches.getPin(0);
  42. bool high = switches.getPin(1);
  43. #ifdef INVERT_SENSOR_BOTTOM
  44. low = !low;
  45. #endif // INVERT_SENSOR_BOTTOM
  46. #ifdef INVERT_SENSOR_TOP
  47. high = !high;
  48. #endif // INVERT_SENSOR_TOP
  49. if ((!low) && (!high)) {
  50. return empty;
  51. } else if (low && (!high)) {
  52. return inbetween;
  53. } else if (low && high) {
  54. return full;
  55. } else {
  56. return invalid;
  57. }
  58. }
  59. void Plants::openWaterInlet(void) {
  60. debug.println("Plants::openWaterInlet");
  61. valves.setPin(countPlants(), true);
  62. }
  63. void Plants::closeWaterInlet(void) {
  64. debug.println("Plants::closeWaterInlet");
  65. valves.setPin(countPlants(), false);
  66. }
  67. int Plants::countFertilizers(void) {
  68. return pumps.getSize();
  69. }
  70. void Plants::startFertilizer(int id) {
  71. debug.print("Plants::startFertilizer ");
  72. debug.println(id);
  73. if ((id >= 0) && (id < countFertilizers())) {
  74. pumps.setPin(id, true);
  75. }
  76. }
  77. void Plants::stopFertilizer(int id) {
  78. debug.print("Plants::stopFertilizer ");
  79. debug.println(id);
  80. if ((id >= 0) && (id < countFertilizers())) {
  81. pumps.setPin(id, false);
  82. }
  83. }
  84. void Plants::stopAllFertilizers(void) {
  85. for (int i = 0; i < countFertilizers(); i++) {
  86. stopFertilizer(i);
  87. }
  88. }
  89. int Plants::countPlants(void) {
  90. return valves.getSize() - 1;
  91. }
  92. void Plants::startPlant(int id) {
  93. debug.print("Plants::startPlant ");
  94. debug.println(id);
  95. if ((id >= 0) && (id < countPlants())) {
  96. valves.setPin(id, true);
  97. }
  98. }
  99. void Plants::stopPlant(int id) {
  100. debug.print("Plants::stopPlant ");
  101. debug.println(id);
  102. if ((id >= 0) && (id < countPlants())) {
  103. valves.setPin(id, false);
  104. }
  105. }
  106. void Plants::stopAllPlants(void) {
  107. for (int i = 0; i < countPlants(); i++) {
  108. stopPlant(i);
  109. }
  110. }