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.6KB

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