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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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. void Plants::setValvePins(int pins[]) {
  10. valves.setPinNumbers(pins);
  11. valves.setOutput();
  12. valves.setAll(false);
  13. }
  14. void Plants::setPumpPins(int pins[]) {
  15. pumps.setPinNumbers(pins);
  16. pumps.setOutput();
  17. pumps.setAll(false);
  18. }
  19. void Plants::setSwitchPins(int pins[], bool pullup) {
  20. switches.setPinNumbers(pins);
  21. switches.setInput(pullup);
  22. }
  23. void Plants::abort(void) {
  24. closeWaterInlet();
  25. stopAllFertilizers();
  26. stopAllPlants();
  27. }
  28. Plants::Waterlevel Plants::getWaterlevel(void) {
  29. bool low = !switches.getPin(0);
  30. bool high = !switches.getPin(1);
  31. if ((!low) && (!high)) {
  32. return empty;
  33. } else if (low && (!high)) {
  34. return inbetween;
  35. } else if (low && high) {
  36. return full;
  37. } else {
  38. return invalid;
  39. }
  40. }
  41. void Plants::openWaterInlet(void) {
  42. Serial.println("Plants::openWaterInlet");
  43. valves.setPin(countPlants(), true);
  44. }
  45. void Plants::closeWaterInlet(void) {
  46. Serial.println("Plants::closeWaterInlet");
  47. valves.setPin(countPlants(), false);
  48. }
  49. int Plants::countFertilizers(void) {
  50. return pumps.getSize();
  51. }
  52. void Plants::startFertilizer(int id) {
  53. Serial.print("Plants::startFertilizer ");
  54. Serial.println(id);
  55. if ((id >= 0) && (id < countFertilizers())) {
  56. pumps.setPin(id, true);
  57. }
  58. }
  59. void Plants::stopFertilizer(int id) {
  60. Serial.print("Plants::stopFertilizer ");
  61. Serial.println(id);
  62. if ((id >= 0) && (id < countFertilizers())) {
  63. pumps.setPin(id, false);
  64. }
  65. }
  66. void Plants::stopAllFertilizers(void) {
  67. for (int i = 0; i < countFertilizers(); i++) {
  68. stopFertilizer(i);
  69. }
  70. }
  71. int Plants::countPlants(void) {
  72. return valves.getSize() - 1;
  73. }
  74. void Plants::startPlant(int id) {
  75. Serial.print("Plants::startPlant ");
  76. Serial.println(id);
  77. if ((id >= 0) && (id < countPlants())) {
  78. valves.setPin(id, true);
  79. }
  80. }
  81. void Plants::stopPlant(int id) {
  82. Serial.print("Plants::stopPlant ");
  83. Serial.println(id);
  84. if ((id >= 0) && (id < countPlants())) {
  85. valves.setPin(id, false);
  86. }
  87. }
  88. void Plants::stopAllPlants(void) {
  89. for (int i = 0; i < countPlants(); i++) {
  90. stopPlant(i);
  91. }
  92. }