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.

main.cpp 6.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. #include <Arduino.h>
  2. #include "Keymatrix.h"
  3. #include "SerialLCD.h"
  4. #include "Statemachine.h"
  5. #include "Plants.h"
  6. #include "WifiStuff.h"
  7. #include "config.h"
  8. void blink_lcd(int n, int wait = 200);
  9. void write_to_all(const char *a, const char *b,
  10. const char *c, const char *d, int num_input);
  11. void backspace(void);
  12. SerialLCD lcd(9);
  13. Keymatrix keys(4, 3);
  14. int keymatrix_pins[4 + 3] = { 5, 6, 7, 8, 2, 3, 4 };
  15. Plants plants(5, 3, 2);
  16. int valve_pins[5] = { 10, 11, 12, 13, 14 };
  17. int pump_pins[3] = { 15, 16, 17 };
  18. int switch_pins[2] = { 18, 19 };
  19. Statemachine sm(write_to_all, backspace);
  20. unsigned long last_input_time = 0;
  21. bool backlight_state = true;
  22. bool doing_multi_input = false;
  23. void setup() {
  24. Serial.begin(115200);
  25. Serial.println("Initializing Giess-o-mat");
  26. keys.setPins(keymatrix_pins);
  27. plants.setValvePins(valve_pins);
  28. plants.setPumpPins(pump_pins);
  29. plants.setSwitchPins(switch_pins, true);
  30. Serial.println("Setting up LCD, please wait");
  31. delay(1000); // give LCD some time to boot
  32. lcd.init();
  33. #ifdef DEBUG_WAIT_FOR_SERIAL_CONN
  34. lcd.write(0, "Waiting for serial");
  35. lcd.write(1, "connection on debug");
  36. lcd.write(2, "USB port...");
  37. while (!Serial);
  38. lcd.clear();
  39. #endif
  40. #if defined(ARDUINO_ARCH_ESP8266) || defined(ARDUINO_ARCH_ESP32)
  41. wifi_setup();
  42. #endif
  43. Serial.println("Ready, starting main loop");
  44. sm.begin();
  45. }
  46. void loop() {
  47. #if defined(ARDUINO_ARCH_ESP8266) || defined(ARDUINO_ARCH_ESP32)
  48. wifi_run();
  49. #endif
  50. keys.scan();
  51. while (keys.hasEvent()) {
  52. auto ke = keys.getEvent();
  53. if (ke.getType() == Keymatrix::Event::button_down) {
  54. last_input_time = millis();
  55. if (!backlight_state) {
  56. backlight_state = true;
  57. lcd.setBacklight(255);
  58. // swallow input when used to activate light
  59. continue;
  60. }
  61. int n = ke.getNum();
  62. Serial.print("Got keypad input: \"");
  63. if (n < 0) {
  64. Serial.print((n == -1) ? '*' : '#');
  65. } else {
  66. Serial.print(n);
  67. if (doing_multi_input) {
  68. char s[2] = { (char)(n + '0'), '\0' };
  69. lcd.write(s);
  70. }
  71. }
  72. Serial.println("\"");
  73. blink_lcd(1, 100);
  74. sm.input(n);
  75. }
  76. }
  77. #ifdef DEBUG_ENABLE_KEYPAD_INPUT_ON_SERIAL
  78. if (Serial.available() > 0) {
  79. last_input_time = millis();
  80. if (!backlight_state) {
  81. backlight_state = true;
  82. lcd.setBacklight(255);
  83. }
  84. int c = Serial.read();
  85. if (c == '*') {
  86. Serial.write(c);
  87. Serial.write('\n');
  88. if (doing_multi_input) {
  89. char s[2] = { (char)(c), '\0' };
  90. lcd.write(s);
  91. }
  92. sm.input(-1);
  93. } else if (c == '#') {
  94. Serial.write(c);
  95. Serial.write('\n');
  96. if (doing_multi_input) {
  97. char s[2] = { (char)(c), '\0' };
  98. lcd.write(s);
  99. }
  100. sm.input(-2);
  101. } else if (c == '\n') {
  102. Serial.write('#');
  103. Serial.write('\n');
  104. if (doing_multi_input) {
  105. char s[2] = { '#', '\0' };
  106. lcd.write(s);
  107. }
  108. sm.input(-2);
  109. } else if (c == '\b') {
  110. Serial.write(c);
  111. sm.input(-1);
  112. } else if ((c >= '0') && (c <= '9')) {
  113. Serial.write(c);
  114. if (!doing_multi_input) {
  115. Serial.write('\n');
  116. }
  117. if (doing_multi_input) {
  118. char s[2] = { (char)(c), '\0' };
  119. lcd.write(s);
  120. }
  121. sm.input(c - '0');
  122. }
  123. }
  124. #endif
  125. sm.act();
  126. if (backlight_state && (millis() >= (last_input_time + DISPLAY_BACKLIGHT_TIMEOUT))) {
  127. backlight_state = false;
  128. lcd.setBacklight(0);
  129. }
  130. }
  131. void write_to_all(const char *a, const char *b,
  132. const char *c, const char *d, int num_input) {
  133. lcd.clear();
  134. if (num_input >= 0) {
  135. lcd.write(0, a);
  136. if (num_input >= 1) {
  137. lcd.write(1, b);
  138. }
  139. if (num_input >= 2) {
  140. lcd.write(2, c);
  141. }
  142. if (num_input >= 3) {
  143. lcd.write(3, d);
  144. }
  145. lcd.cursor(3);
  146. doing_multi_input = true;
  147. } else {
  148. lcd.write(0, a);
  149. lcd.write(1, b);
  150. lcd.write(2, c);
  151. lcd.write(3, d);
  152. lcd.cursor(0);
  153. doing_multi_input = false;
  154. }
  155. #ifdef DEBUG_ENABLE_LCD_OUTPUT_ON_SERIAL
  156. int la = strlen(a);
  157. int lb = strlen(b);
  158. int lc = strlen(c);
  159. int ld = strlen(d);
  160. Serial.println();
  161. Serial.println(" ----------------------");
  162. Serial.print("| ");
  163. Serial.print(a);
  164. if (la < 20) {
  165. for (int i = 0; i < (20 - la); i++) {
  166. Serial.print(' ');
  167. }
  168. }
  169. Serial.println(" |");
  170. Serial.print("| ");
  171. Serial.print(b);
  172. if (lb < 20) {
  173. for (int i = 0; i < (20 - lb); i++) {
  174. Serial.print(' ');
  175. }
  176. }
  177. Serial.println(" |");
  178. Serial.print("| ");
  179. Serial.print(c);
  180. if (lc < 20) {
  181. for (int i = 0; i < (20 - lc); i++) {
  182. Serial.print(' ');
  183. }
  184. }
  185. Serial.println(" |");
  186. Serial.print("| ");
  187. Serial.print(d);
  188. if (ld < 20) {
  189. for (int i = 0; i < (20 - ld); i++) {
  190. Serial.print(' ');
  191. }
  192. }
  193. Serial.println(" |");
  194. Serial.println(" ----------------------");
  195. Serial.println("Please provide keypad input:");
  196. #endif
  197. }
  198. void backspace(void) {
  199. lcd.write("\b");
  200. }
  201. void blink_lcd(int n, int wait) {
  202. for (int i = 0; i < n; i++) {
  203. lcd.setBacklight(0);
  204. delay(wait);
  205. lcd.setBacklight(255);
  206. if (i < (n - 1))
  207. delay(wait);
  208. }
  209. }