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.

Statemachine.cpp 77KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124
  1. /*
  2. * Copyright (c) 2021 Thomas Buck <thomas@xythobuz.de>
  3. *
  4. * This file is part of Giess-o-mat.
  5. *
  6. * Giess-o-mat is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * Giess-o-mat is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with Giess-o-mat. If not, see <https://www.gnu.org/licenses/>.
  18. */
  19. #include "Plants.h"
  20. #include "DebugLog.h"
  21. #include "WifiStuff.h"
  22. #include "Statemachine.h"
  23. #include "config.h"
  24. #include "config_pins.h"
  25. #ifdef FUNCTION_CONTROL
  26. Statemachine::DigitBuffer::DigitBuffer(int _size) {
  27. size = _size;
  28. pos = 0;
  29. digits = new int[size];
  30. }
  31. Statemachine::DigitBuffer::~DigitBuffer() {
  32. delete digits;
  33. }
  34. bool Statemachine::DigitBuffer::spaceLeft(void) {
  35. return (pos < size);
  36. }
  37. bool Statemachine::DigitBuffer::hasDigits(void) {
  38. return (pos > 0);
  39. }
  40. int Statemachine::DigitBuffer::countDigits(void) {
  41. return pos;
  42. }
  43. void Statemachine::DigitBuffer::addDigit(int d) {
  44. if (spaceLeft()) {
  45. digits[pos] = d;
  46. pos++;
  47. }
  48. }
  49. void Statemachine::DigitBuffer::removeDigit(void) {
  50. if (hasDigits()) {
  51. pos--;
  52. }
  53. }
  54. void Statemachine::DigitBuffer::clear(void) {
  55. pos = 0;
  56. }
  57. uint32_t Statemachine::DigitBuffer::getNumber(void) {
  58. uint32_t fact = 1;
  59. uint32_t sum = 0;
  60. for (int i = (pos - 1); i >= 0; i--) {
  61. sum += digits[i] * fact;
  62. fact *= 10;
  63. }
  64. return sum;
  65. }
  66. static const char *state_names[] = {
  67. stringify(init),
  68. stringify(door_select),
  69. stringify(menu_a),
  70. stringify(menu_b),
  71. stringify(menu_c),
  72. stringify(auto_mode_a),
  73. stringify(auto_mode_b),
  74. stringify(auto_mode_c),
  75. stringify(auto_fert_a),
  76. stringify(auto_fert_b),
  77. stringify(auto_fert_run),
  78. stringify(auto_tank_run),
  79. stringify(auto_stirr_run),
  80. stringify(auto_plant),
  81. stringify(auto_plant_kickstart_run),
  82. stringify(auto_plant_run),
  83. stringify(auto_done),
  84. stringify(fillnwater_plant),
  85. stringify(fillnwater_tank_run),
  86. stringify(fillnwater_kickstart_run),
  87. stringify(fillnwater_plant_run),
  88. stringify(fullauto_fert),
  89. stringify(fullauto_plant),
  90. stringify(fullauto_stirr_run),
  91. stringify(fullauto_fert_run),
  92. stringify(fullauto_tank_run),
  93. stringify(fullauto_kickstart_run),
  94. stringify(fullauto_plant_run),
  95. stringify(fullauto_plant_overrun),
  96. stringify(fullauto_tank_purge_run),
  97. stringify(fullauto_kickstart_purge_run),
  98. stringify(fullauto_plant_purge_run),
  99. stringify(fullauto_plant_purge_overrun),
  100. stringify(fullauto_plant_purge_over_pump_run),
  101. stringify(fullauto_done),
  102. stringify(automation_mode),
  103. stringify(menu_pumps),
  104. stringify(menu_pumps_time),
  105. stringify(menu_pumps_go),
  106. stringify(menu_pumps_run),
  107. stringify(menu_pumps_done),
  108. stringify(menu_valves),
  109. stringify(menu_valves_time),
  110. stringify(menu_valves_go),
  111. stringify(menu_valves_run),
  112. stringify(menu_valves_done),
  113. stringify(menu_aux),
  114. stringify(menu_aux_time),
  115. stringify(menu_aux_go),
  116. stringify(menu_aux_run),
  117. stringify(menu_aux_done),
  118. stringify(error)
  119. };
  120. static int auto_pump_runtime[PUMP_COUNT] = AUTO_PUMP_RUNTIME;
  121. const char *Statemachine::getStateName(void) {
  122. return state_names[state];
  123. }
  124. bool Statemachine::isIdle(void) {
  125. return state == init;
  126. }
  127. Statemachine::Statemachine(print_fn _print, backspace_fn _backspace)
  128. : db(7), selected_plants(plants.countPlants()),
  129. selected_ferts(plants.countFertilizers()) {
  130. state = init;
  131. old_state = init;
  132. print = _print;
  133. backspace = _backspace;
  134. selected_id = 0;
  135. selected_time = 0;
  136. start_time = 0;
  137. stop_time = 0;
  138. last_animation_time = 0;
  139. error_condition = "";
  140. into_state_time = 0;
  141. filling_started_empty = false;
  142. watering_started_full = false;
  143. menu_entered_digits = "";
  144. }
  145. void Statemachine::begin(void) {
  146. switch_to(init);
  147. }
  148. void Statemachine::input(int n) {
  149. if (state == init) {
  150. #if (LOCK_COUNT > 0) && defined(DOOR_LOCK_PIN)
  151. if (n == -1) {
  152. if (db.hasDigits()) {
  153. backspace();
  154. db.removeDigit();
  155. if (menu_entered_digits.length() > 0) {
  156. menu_entered_digits.remove(menu_entered_digits.length() - 1);
  157. switch_to(state);
  158. }
  159. } else {
  160. switch_to(menu_a);
  161. }
  162. } else if (n == -2) {
  163. switch_to(menu_a);
  164. } else {
  165. if (db.spaceLeft()) {
  166. db.addDigit(n);
  167. //menu_entered_digits += String(n);
  168. menu_entered_digits += String("*");
  169. switch_to(state);
  170. } else {
  171. backspace();
  172. }
  173. uint32_t n = db.getNumber();
  174. if (n == DOOR_LOCK_PIN) {
  175. db.clear();
  176. menu_entered_digits = "";
  177. selected_plants.clear();
  178. switch_to(door_select);
  179. } else if (db.countDigits() >= DOOR_LOCK_PIN_MAX_DIGITS) {
  180. db.clear();
  181. menu_entered_digits = "";
  182. switch_to(state);
  183. }
  184. }
  185. #else
  186. switch_to(menu_a);
  187. #endif
  188. } else if (state == door_select) {
  189. #if (LOCK_COUNT > 0)
  190. if (n == -1) {
  191. if (db.hasDigits()) {
  192. backspace();
  193. db.removeDigit();
  194. if (menu_entered_digits.length() > 0) {
  195. menu_entered_digits.remove(menu_entered_digits.length() - 1);
  196. switch_to(state);
  197. }
  198. } else {
  199. switch_to(init);
  200. }
  201. } else if (n == -2) {
  202. if (!db.hasDigits()) {
  203. for (int i = 0; i < LOCK_COUNT; i++) {
  204. if (selected_plants.isSet(i)) {
  205. plants.startAux(STIRRER_COUNT + i);
  206. delay(DOOR_LOCK_ON_TIME);
  207. plants.stopAux(STIRRER_COUNT + i);
  208. delay(DOOR_LOCK_NEXT_DELAY);
  209. }
  210. }
  211. plants.stopAllAux();
  212. switch_to(menu_a);
  213. } else {
  214. selected_id = number_input();
  215. if ((selected_id <= 0) || (selected_id > LOCK_COUNT)) {
  216. error_condition = F("Invalid lock ID!");
  217. switch_to(error);
  218. } else {
  219. selected_plants.set(selected_id - 1);
  220. menu_entered_digits = "";
  221. switch_to(state);
  222. }
  223. }
  224. } else {
  225. if (db.spaceLeft()) {
  226. db.addDigit(n);
  227. menu_entered_digits += String(n);
  228. switch_to(state);
  229. } else {
  230. backspace();
  231. }
  232. }
  233. #else
  234. // should never be reached
  235. switch_to(menu_a);
  236. #endif
  237. } else if ((state == menu_a) || (state == menu_b) || (state == menu_c)) {
  238. if (n == 1) {
  239. switch_to(auto_mode_a);
  240. } else if (n == 2) {
  241. switch_to(automation_mode);
  242. } else if (n == 3) {
  243. switch_to(menu_pumps);
  244. } else if (n == 4) {
  245. switch_to(menu_valves);
  246. } else if (n == 5) {
  247. switch_to(menu_aux);
  248. #if (LOCK_COUNT > 0) && !defined(DOOR_LOCK_PIN)
  249. } else if (n == 6) {
  250. switch_to(door_select);
  251. #endif
  252. } else if (n == -1) {
  253. switch_to(init);
  254. } else if (n == -2) {
  255. switch_to((state == menu_a) ? menu_b : ((state == menu_b) ? menu_c : menu_a));
  256. }
  257. } else if (state == automation_mode) {
  258. // TODO
  259. switch_to(menu_a);
  260. } else if ((state == auto_mode_a) || (state == auto_mode_b) || (state == auto_mode_c)) {
  261. if (n == 1) {
  262. selected_ferts.clear();
  263. switch_to(fullauto_fert);
  264. } else if (n == 2) {
  265. switch_to(auto_fert_a);
  266. } else if (n == 3) {
  267. selected_plants.clear();
  268. switch_to(fillnwater_plant);
  269. } else if (n == 4) {
  270. auto wl = plants.getWaterlevel();
  271. if ((wl != Plants::full) && (wl != Plants::invalid)) {
  272. plants.openWaterInlet();
  273. selected_id = plants.countPlants() + 1;
  274. selected_time = MAX_TANK_FILL_TIME;
  275. start_time = millis();
  276. switch_to(auto_tank_run);
  277. } else if (wl == Plants::full) {
  278. stop_time = millis();
  279. switch_to(auto_mode_a);
  280. } else if (wl == Plants::invalid) {
  281. error_condition = F("Invalid sensor state");
  282. state = auto_mode_a;
  283. switch_to(error);
  284. }
  285. } else if (n == 5) {
  286. selected_plants.clear();
  287. switch_to(auto_plant);
  288. } else if (n == -1) {
  289. switch_to(menu_a);
  290. } else if (n == -2) {
  291. switch_to((state == auto_mode_a) ? auto_mode_b : ((state == auto_mode_b) ? auto_mode_c : auto_mode_a));
  292. }
  293. } else if ((state == auto_fert_a) || (state == auto_fert_b)) {
  294. // TODO fertilizer number currently "hardcoded" to 3 in UI
  295. if ((n >= 1) && (n <= 3)) {
  296. auto wl = plants.getWaterlevel();
  297. if ((wl != Plants::full) && (wl != Plants::invalid)) {
  298. plants.startFertilizer(n - 1);
  299. selected_id = n;
  300. selected_time = auto_pump_runtime[n - 1];
  301. start_time = millis();
  302. switch_to(auto_fert_run);
  303. } else if (wl == Plants::full) {
  304. stop_time = millis();
  305. switch_to(auto_mode_a);
  306. } else if (wl == Plants::invalid) {
  307. error_condition = F("Invalid sensor state");
  308. state = auto_mode_a;
  309. switch_to(error);
  310. }
  311. } else if (n == 4) {
  312. for (int i = 0; i < STIRRER_COUNT; i++) {
  313. plants.startAux(i);
  314. }
  315. selected_id = 1;
  316. selected_time = AUTO_STIRR_RUNTIME;
  317. start_time = millis();
  318. switch_to(auto_stirr_run);
  319. } else if (n == -1) {
  320. switch_to(auto_mode_a);
  321. } else if (n == -2) {
  322. switch_to((state == auto_fert_a) ? auto_fert_b : auto_fert_a);
  323. }
  324. } else if (state == fullauto_stirr_run) {
  325. // allow user to stop stirring and continue with fertilizers
  326. plants.abort();
  327. stop_time = millis();
  328. if (selected_ferts.countSet() > 0) {
  329. selected_time = auto_pump_runtime[0];
  330. for (int i = 0; i < plants.countFertilizers(); i++) {
  331. if (auto_pump_runtime[i] > selected_time) {
  332. selected_time = auto_pump_runtime[i];
  333. }
  334. if (selected_ferts.isSet(i)) {
  335. plants.startFertilizer(i);
  336. }
  337. }
  338. start_time = millis();
  339. switch_to(fullauto_fert_run);
  340. } else {
  341. auto wl = plants.getWaterlevel();
  342. if ((wl != Plants::full) && (wl != Plants::invalid)) {
  343. // if the waterlevel is currently empty, we
  344. // set a flag to record the time to fill
  345. filling_started_empty = (wl == Plants::empty);
  346. plants.openWaterInlet();
  347. selected_id = plants.countPlants() + 1;
  348. selected_time = MAX_TANK_FILL_TIME;
  349. start_time = millis();
  350. switch_to(fullauto_tank_run);
  351. } else if (wl == Plants::full) {
  352. // check if kickstart is required for this
  353. bool need_kickstart = false;
  354. for (int i = 0; i < plants.countPlants(); i++) {
  355. if (selected_plants.isSet(i)) {
  356. if (plants.getKickstart()->getPinNumber(i) >= 0) {
  357. need_kickstart = true;
  358. }
  359. }
  360. }
  361. // start kickstart/valve as needed
  362. for (int i = 0; i < plants.countPlants(); i++) {
  363. if (selected_plants.isSet(i)) {
  364. plants.startPlant(i, need_kickstart);
  365. }
  366. }
  367. watering_started_full = (wl == Plants::full);
  368. selected_time = MAX_AUTO_PLANT_RUNTIME;
  369. start_time = millis();
  370. if (need_kickstart) {
  371. switch_to(fullauto_kickstart_run);
  372. } else {
  373. switch_to(fullauto_plant_run);
  374. }
  375. } else if (wl == Plants::invalid) {
  376. plants.abort();
  377. error_condition = F("Invalid sensor state");
  378. state = fullauto_fert;
  379. switch_to(error);
  380. }
  381. }
  382. } else if (state == fullauto_fert_run) {
  383. // allow user to stop fertizilers and continue with tank filling
  384. plants.abort();
  385. stop_time = millis();
  386. auto wl = plants.getWaterlevel();
  387. if ((wl != Plants::full) && (wl != Plants::invalid)) {
  388. // if the waterlevel is currently empty, we
  389. // set a flag to record the time to fill
  390. filling_started_empty = (wl == Plants::empty);
  391. plants.openWaterInlet();
  392. selected_id = plants.countPlants() + 1;
  393. selected_time = MAX_TANK_FILL_TIME;
  394. start_time = millis();
  395. switch_to(fullauto_tank_run);
  396. } else if (wl == Plants::full) {
  397. // check if kickstart is required for this
  398. bool need_kickstart = false;
  399. for (int i = 0; i < plants.countPlants(); i++) {
  400. if (selected_plants.isSet(i)) {
  401. if (plants.getKickstart()->getPinNumber(i) >= 0) {
  402. need_kickstart = true;
  403. }
  404. }
  405. }
  406. // start kickstart/valve as needed
  407. for (int i = 0; i < plants.countPlants(); i++) {
  408. if (selected_plants.isSet(i)) {
  409. plants.startPlant(i, need_kickstart);
  410. }
  411. }
  412. watering_started_full = (wl == Plants::full);
  413. selected_time = MAX_AUTO_PLANT_RUNTIME;
  414. start_time = millis();
  415. if (need_kickstart) {
  416. switch_to(fullauto_kickstart_run);
  417. } else {
  418. switch_to(fullauto_plant_run);
  419. }
  420. } else if (wl == Plants::invalid) {
  421. plants.abort();
  422. error_condition = F("Invalid sensor state");
  423. state = fullauto_fert;
  424. switch_to(error);
  425. }
  426. } else if ((state == fullauto_tank_run)
  427. || (state == fullauto_tank_purge_run)) {
  428. // allow user to stop tank and continue with kickstart
  429. plants.abort();
  430. stop_time = millis();
  431. auto wl = plants.getWaterlevel();
  432. if ((wl != Plants::empty) && (wl != Plants::invalid)) {
  433. // check if kickstart is required for this
  434. bool need_kickstart = false;
  435. for (int i = 0; i < plants.countPlants(); i++) {
  436. if (selected_plants.isSet(i)) {
  437. if (plants.getKickstart()->getPinNumber(i) >= 0) {
  438. need_kickstart = true;
  439. }
  440. }
  441. }
  442. // start kickstart/valve as needed
  443. for (int i = 0; i < plants.countPlants(); i++) {
  444. if (selected_plants.isSet(i)) {
  445. plants.startPlant(i, need_kickstart);
  446. }
  447. }
  448. watering_started_full = (wl == Plants::full);
  449. selected_time = MAX_AUTO_PLANT_RUNTIME;
  450. start_time = millis();
  451. if (need_kickstart) {
  452. if (state == fullauto_tank_run) {
  453. switch_to(fullauto_kickstart_run);
  454. } else {
  455. switch_to(fullauto_kickstart_purge_run);
  456. }
  457. } else {
  458. if (state == fullauto_tank_run) {
  459. switch_to(fullauto_plant_run);
  460. } else {
  461. switch_to(fullauto_plant_purge_run);
  462. }
  463. }
  464. } else if (wl == Plants::empty) {
  465. stop_time = millis();
  466. switch_to(auto_done);
  467. } else if (wl == Plants::invalid) {
  468. error_condition = F("Invalid sensor state");
  469. state = auto_mode_a;
  470. switch_to(error);
  471. }
  472. } else if ((state == fullauto_kickstart_run)
  473. || (state == fullauto_kickstart_purge_run)) {
  474. // allow user to stop kickstarting and continue with plants
  475. plants.abort();
  476. selected_time = MAX_AUTO_PLANT_RUNTIME;
  477. start_time = millis();
  478. // start required valves
  479. for (int i = 0; i < plants.countPlants(); i++) {
  480. if (selected_plants.isSet(i)) {
  481. plants.startPlant(i, false);
  482. }
  483. }
  484. if (state == fullauto_kickstart_run) {
  485. switch_to(fullauto_plant_run);
  486. } else {
  487. switch_to(fullauto_plant_purge_run);
  488. }
  489. } else if ((state == auto_fert_run) || (state == auto_tank_run)
  490. || (state == auto_stirr_run) || (state == auto_plant_run)
  491. || (state == auto_plant_kickstart_run)
  492. || (state == fillnwater_kickstart_run)
  493. || (state == fullauto_plant_run)
  494. || (state == fullauto_plant_overrun)
  495. || (state == fullauto_plant_purge_run)
  496. || (state == fullauto_plant_purge_overrun)
  497. || (state == fullauto_plant_purge_over_pump_run)) {
  498. plants.abort();
  499. stop_time = millis();
  500. switch_to(auto_done);
  501. } else if (state == auto_plant) {
  502. if (n == -1) {
  503. if (db.hasDigits()) {
  504. backspace();
  505. db.removeDigit();
  506. if (menu_entered_digits.length() > 0) {
  507. menu_entered_digits.remove(menu_entered_digits.length() - 1);
  508. switch_to(state);
  509. }
  510. } else {
  511. switch_to(auto_mode_b);
  512. }
  513. } else if (n == -2) {
  514. if (!db.hasDigits()) {
  515. auto wl = plants.getWaterlevel();
  516. if ((wl != Plants::empty) && (wl != Plants::invalid)) {
  517. // check if kickstart is required for this
  518. bool need_kickstart = false;
  519. for (int i = 0; i < plants.countPlants(); i++) {
  520. if (selected_plants.isSet(i)) {
  521. if (plants.getKickstart()->getPinNumber(i) >= 0) {
  522. need_kickstart = true;
  523. }
  524. }
  525. }
  526. // start kickstart/valve as needed
  527. for (int i = 0; i < plants.countPlants(); i++) {
  528. if (selected_plants.isSet(i)) {
  529. plants.startPlant(i, need_kickstart);
  530. }
  531. }
  532. selected_time = MAX_AUTO_PLANT_RUNTIME;
  533. start_time = millis();
  534. if (need_kickstart) {
  535. switch_to(auto_plant_kickstart_run);
  536. } else {
  537. switch_to(auto_plant_run);
  538. }
  539. } else if (wl == Plants::empty) {
  540. stop_time = millis();
  541. switch_to(auto_mode_b);
  542. } else if (wl == Plants::invalid) {
  543. error_condition = F("Invalid sensor state");
  544. state = auto_mode_b;
  545. switch_to(error);
  546. }
  547. } else {
  548. selected_id = number_input();
  549. if ((selected_id <= 0) || (selected_id > plants.countPlants())) {
  550. error_condition = F("Invalid plant ID!");
  551. switch_to(error);
  552. } else {
  553. selected_plants.set(selected_id - 1);
  554. menu_entered_digits = "";
  555. switch_to(auto_plant);
  556. }
  557. }
  558. } else {
  559. if (db.spaceLeft()) {
  560. db.addDigit(n);
  561. menu_entered_digits += String(n);
  562. switch_to(state);
  563. } else {
  564. backspace();
  565. }
  566. }
  567. } else if (state == fullauto_fert) {
  568. if (n == -1) {
  569. if (db.hasDigits()) {
  570. backspace();
  571. db.removeDigit();
  572. if (menu_entered_digits.length() > 0) {
  573. menu_entered_digits.remove(menu_entered_digits.length() - 1);
  574. switch_to(state);
  575. }
  576. } else {
  577. switch_to(auto_mode_a);
  578. }
  579. } else if (n == -2) {
  580. if (!db.hasDigits()) {
  581. selected_plants.clear();
  582. switch_to(fullauto_plant);
  583. } else {
  584. selected_id = number_input();
  585. if ((selected_id <= 0) || (selected_id > plants.countFertilizers())) {
  586. error_condition = F("Invalid fert. ID!");
  587. switch_to(error);
  588. } else {
  589. selected_ferts.set(selected_id - 1);
  590. menu_entered_digits = "";
  591. switch_to(fullauto_fert);
  592. }
  593. }
  594. } else {
  595. if (db.spaceLeft()) {
  596. db.addDigit(n);
  597. menu_entered_digits += String(n);
  598. switch_to(state);
  599. } else {
  600. backspace();
  601. }
  602. }
  603. } else if (state == fullauto_plant) {
  604. if (n == -1) {
  605. if (db.hasDigits()) {
  606. backspace();
  607. db.removeDigit();
  608. if (menu_entered_digits.length() > 0) {
  609. menu_entered_digits.remove(menu_entered_digits.length() - 1);
  610. switch_to(state);
  611. }
  612. } else {
  613. selected_ferts.clear();
  614. switch_to(fullauto_fert);
  615. }
  616. } else if (n == -2) {
  617. if (!db.hasDigits()) {
  618. if (selected_plants.countSet() <= 0) {
  619. // user has not selected a plant yet...
  620. return;
  621. }
  622. #ifdef FULLAUTO_MIN_PLANT_COUNT
  623. if (selected_plants.countSet() < FULLAUTO_MIN_PLANT_COUNT) {
  624. return;
  625. }
  626. #endif
  627. // check if we need to run fertilizers
  628. if (selected_ferts.countSet() > 0) {
  629. // stirr before pumping fertilizers
  630. for (int i = 0; i < STIRRER_COUNT; i++) {
  631. plants.startAux(i);
  632. }
  633. selected_id = 1;
  634. selected_time = AUTO_STIRR_RUNTIME;
  635. start_time = millis();
  636. switch_to(fullauto_stirr_run);
  637. } else {
  638. // immediately continue with filling tank
  639. auto wl = plants.getWaterlevel();
  640. if ((wl != Plants::full) && (wl != Plants::invalid)) {
  641. // if the waterlevel is currently empty, we
  642. // set a flag to record the time to fill
  643. filling_started_empty = (wl == Plants::empty);
  644. plants.openWaterInlet();
  645. selected_id = plants.countPlants() + 1;
  646. selected_time = MAX_TANK_FILL_TIME;
  647. start_time = millis();
  648. switch_to(fullauto_tank_run);
  649. } else if (wl == Plants::full) {
  650. // check if kickstart is required for this
  651. bool need_kickstart = false;
  652. for (int i = 0; i < plants.countPlants(); i++) {
  653. if (selected_plants.isSet(i)) {
  654. if (plants.getKickstart()->getPinNumber(i) >= 0) {
  655. need_kickstart = true;
  656. }
  657. }
  658. }
  659. // start kickstart/valve as needed
  660. for (int i = 0; i < plants.countPlants(); i++) {
  661. if (selected_plants.isSet(i)) {
  662. plants.startPlant(i, need_kickstart);
  663. }
  664. }
  665. // for recording the flowrate
  666. watering_started_full = (wl == Plants::full);
  667. selected_time = MAX_AUTO_PLANT_RUNTIME;
  668. start_time = millis();
  669. if (need_kickstart) {
  670. switch_to(fullauto_kickstart_run);
  671. } else {
  672. switch_to(fullauto_plant_run);
  673. }
  674. } else if (wl == Plants::invalid) {
  675. error_condition = F("Invalid sensor state");
  676. state = auto_mode_a;
  677. switch_to(error);
  678. }
  679. }
  680. } else {
  681. selected_id = number_input();
  682. if ((selected_id <= 0) || (selected_id > plants.countPlants())) {
  683. error_condition = F("Invalid plant ID!");
  684. switch_to(error);
  685. } else {
  686. selected_plants.set(selected_id - 1);
  687. menu_entered_digits = "";
  688. switch_to(fullauto_plant);
  689. }
  690. }
  691. } else {
  692. if (db.spaceLeft()) {
  693. db.addDigit(n);
  694. menu_entered_digits += String(n);
  695. switch_to(state);
  696. } else {
  697. backspace();
  698. }
  699. }
  700. } else if ((state == auto_done) || (state == fullauto_done)) {
  701. switch_to(auto_mode_a);
  702. } else if (state == menu_pumps) {
  703. if (n == -1) {
  704. if (db.hasDigits()) {
  705. backspace();
  706. db.removeDigit();
  707. if (menu_entered_digits.length() > 0) {
  708. menu_entered_digits.remove(menu_entered_digits.length() - 1);
  709. switch_to(state);
  710. }
  711. } else {
  712. switch_to(menu_b);
  713. }
  714. } else if (n == -2) {
  715. if (!db.hasDigits()) {
  716. return;
  717. }
  718. selected_id = number_input();
  719. if ((selected_id <= 0) || (selected_id > plants.countFertilizers())) {
  720. error_condition = F("Invalid pump ID!");
  721. switch_to(error);
  722. } else {
  723. switch_to(menu_pumps_time);
  724. }
  725. } else {
  726. if (db.spaceLeft()) {
  727. db.addDigit(n);
  728. menu_entered_digits += String(n);
  729. switch_to(state);
  730. } else {
  731. backspace();
  732. }
  733. }
  734. } else if (state == fillnwater_plant) {
  735. if (n == -1) {
  736. if (db.hasDigits()) {
  737. backspace();
  738. db.removeDigit();
  739. if (menu_entered_digits.length() > 0) {
  740. menu_entered_digits.remove(menu_entered_digits.length() - 1);
  741. switch_to(state);
  742. }
  743. } else {
  744. switch_to(auto_mode_a);
  745. }
  746. } else if (n == -2) {
  747. if (!db.hasDigits()) {
  748. if (selected_plants.countSet() <= 0) {
  749. return;
  750. }
  751. auto wl = plants.getWaterlevel();
  752. if ((wl != Plants::full) && (wl != Plants::invalid)) {
  753. // if the waterlevel is currently empty, we
  754. // set a flag to record the time to fill
  755. filling_started_empty = (wl == Plants::empty);
  756. plants.openWaterInlet();
  757. selected_id = plants.countPlants() + 1;
  758. selected_time = MAX_TANK_FILL_TIME;
  759. start_time = millis();
  760. switch_to(fillnwater_tank_run);
  761. } else if (wl == Plants::full) {
  762. // check if kickstart is required for this
  763. bool need_kickstart = false;
  764. for (int i = 0; i < plants.countPlants(); i++) {
  765. if (selected_plants.isSet(i)) {
  766. if (plants.getKickstart()->getPinNumber(i) >= 0) {
  767. need_kickstart = true;
  768. }
  769. }
  770. }
  771. // start kickstart/valve as needed
  772. for (int i = 0; i < plants.countPlants(); i++) {
  773. if (selected_plants.isSet(i)) {
  774. plants.startPlant(i, need_kickstart);
  775. }
  776. }
  777. // for recording the flowrate
  778. watering_started_full = (wl == Plants::full);
  779. selected_time = MAX_AUTO_PLANT_RUNTIME;
  780. start_time = millis();
  781. if (need_kickstart) {
  782. switch_to(fillnwater_kickstart_run);
  783. } else {
  784. switch_to(fillnwater_plant_run);
  785. }
  786. } else if (wl == Plants::invalid) {
  787. error_condition = F("Invalid sensor state");
  788. state = auto_mode_a;
  789. switch_to(error);
  790. }
  791. } else {
  792. selected_id = number_input();
  793. if ((selected_id <= 0) || (selected_id > plants.countPlants())) {
  794. error_condition = F("Invalid plant ID!");
  795. switch_to(error);
  796. } else {
  797. selected_plants.set(selected_id - 1);
  798. menu_entered_digits = "";
  799. switch_to(fillnwater_plant);
  800. }
  801. }
  802. } else {
  803. if (db.spaceLeft()) {
  804. db.addDigit(n);
  805. menu_entered_digits += String(n);
  806. switch_to(state);
  807. } else {
  808. backspace();
  809. }
  810. }
  811. } else if (state == fillnwater_tank_run) {
  812. plants.abort();
  813. stop_time = millis();
  814. auto wl = plants.getWaterlevel();
  815. if ((wl != Plants::empty) && (wl != Plants::invalid)) {
  816. // check if kickstart is required for this
  817. bool need_kickstart = false;
  818. for (int i = 0; i < plants.countPlants(); i++) {
  819. if (selected_plants.isSet(i)) {
  820. if (plants.getKickstart()->getPinNumber(i) >= 0) {
  821. need_kickstart = true;
  822. }
  823. }
  824. }
  825. // start kickstart/valve as needed
  826. for (int i = 0; i < plants.countPlants(); i++) {
  827. if (selected_plants.isSet(i)) {
  828. plants.startPlant(i, need_kickstart);
  829. }
  830. }
  831. watering_started_full = (wl == Plants::full);
  832. selected_time = MAX_AUTO_PLANT_RUNTIME;
  833. start_time = millis();
  834. if (need_kickstart) {
  835. switch_to(fillnwater_kickstart_run);
  836. } else {
  837. switch_to(fillnwater_plant_run);
  838. }
  839. } else if (wl == Plants::empty) {
  840. switch_to(auto_mode_a);
  841. } else if (wl == Plants::invalid) {
  842. error_condition = F("Invalid sensor state");
  843. state = auto_mode_a;
  844. switch_to(error);
  845. }
  846. } else if (state == fillnwater_plant_run) {
  847. plants.abort();
  848. stop_time = millis();
  849. switch_to(auto_done);
  850. } else if (state == menu_pumps_time) {
  851. if (n == -1) {
  852. if (db.hasDigits()) {
  853. backspace();
  854. db.removeDigit();
  855. if (menu_entered_digits.length() > 0) {
  856. menu_entered_digits.remove(menu_entered_digits.length() - 1);
  857. switch_to(state);
  858. }
  859. } else {
  860. switch_to(menu_pumps);
  861. }
  862. } else if (n == -2) {
  863. if (!db.hasDigits()) {
  864. return;
  865. }
  866. selected_time = number_input();
  867. if ((selected_time <= 0) || (selected_time > MAX_PUMP_RUNTIME)) {
  868. error_condition = F("Invalid time range!");
  869. switch_to(error);
  870. } else {
  871. switch_to(menu_pumps_go);
  872. }
  873. } else {
  874. if (db.spaceLeft()) {
  875. db.addDigit(n);
  876. menu_entered_digits += String(n);
  877. switch_to(state);
  878. } else {
  879. backspace();
  880. }
  881. }
  882. } else if (state == menu_pumps_go) {
  883. if (n == -2) {
  884. start_time = millis();
  885. last_animation_time = start_time;
  886. auto wl = plants.getWaterlevel();
  887. if ((wl != Plants::full) && (wl != Plants::invalid)) {
  888. plants.startFertilizer(selected_id - 1);
  889. switch_to(menu_pumps_run);
  890. } else if (wl == Plants::full) {
  891. stop_time = millis();
  892. switch_to(menu_pumps_done);
  893. } else if (wl == Plants::invalid) {
  894. error_condition = F("Invalid sensor state");
  895. state = menu_pumps;
  896. switch_to(error);
  897. }
  898. } else {
  899. switch_to(menu_pumps_time);
  900. }
  901. } else if (state == menu_pumps_run) {
  902. plants.abort();
  903. stop_time = millis();
  904. switch_to(menu_pumps_done);
  905. } else if (state == menu_pumps_done) {
  906. switch_to(menu_b);
  907. } else if (state == menu_valves) {
  908. if (n == -1) {
  909. if (db.hasDigits()) {
  910. backspace();
  911. db.removeDigit();
  912. if (menu_entered_digits.length() > 0) {
  913. menu_entered_digits.remove(menu_entered_digits.length() - 1);
  914. switch_to(state);
  915. }
  916. } else {
  917. switch_to(menu_b);
  918. }
  919. } else if (n == -2) {
  920. if (!db.hasDigits()) {
  921. return;
  922. }
  923. selected_id = number_input();
  924. if ((selected_id <= 0) || (selected_id > (plants.countPlants() + 1))) {
  925. error_condition = F("Invalid valve ID!");
  926. switch_to(error);
  927. } else {
  928. switch_to(menu_valves_time);
  929. }
  930. } else {
  931. if (db.spaceLeft()) {
  932. db.addDigit(n);
  933. menu_entered_digits += String(n);
  934. switch_to(state);
  935. } else {
  936. backspace();
  937. }
  938. }
  939. } else if (state == menu_valves_time) {
  940. if (n == -1) {
  941. if (db.hasDigits()) {
  942. backspace();
  943. db.removeDigit();
  944. if (menu_entered_digits.length() > 0) {
  945. menu_entered_digits.remove(menu_entered_digits.length() - 1);
  946. switch_to(state);
  947. }
  948. } else {
  949. switch_to(menu_valves);
  950. }
  951. } else if (n == -2) {
  952. if (!db.hasDigits()) {
  953. return;
  954. }
  955. selected_time = number_input();
  956. if ((selected_time <= 0) || (selected_time > MAX_VALVE_RUNTIME)) {
  957. error_condition = F("Invalid time range!");
  958. switch_to(error);
  959. } else {
  960. switch_to(menu_valves_go);
  961. }
  962. } else {
  963. if (db.spaceLeft()) {
  964. db.addDigit(n);
  965. menu_entered_digits += String(n);
  966. switch_to(state);
  967. } else {
  968. backspace();
  969. }
  970. }
  971. } else if (state == menu_valves_go) {
  972. if (n == -2) {
  973. start_time = millis();
  974. last_animation_time = start_time;
  975. auto wl = plants.getWaterlevel();
  976. if ((wl != Plants::full) && (wl != Plants::invalid)) {
  977. if (selected_id >= (plants.countPlants() + 1)) {
  978. plants.openWaterInlet();
  979. } else {
  980. // TODO support testing kickstart
  981. plants.startPlant(selected_id - 1, false);
  982. }
  983. switch_to(menu_valves_run);
  984. } else if (wl == Plants::full) {
  985. stop_time = millis();
  986. switch_to(menu_valves_done);
  987. } else if (wl == Plants::invalid) {
  988. error_condition = F("Invalid sensor state");
  989. state = menu_valves;
  990. switch_to(error);
  991. }
  992. } else {
  993. switch_to(menu_valves_time);
  994. }
  995. } else if (state == menu_valves_run) {
  996. plants.abort();
  997. stop_time = millis();
  998. switch_to(menu_valves_done);
  999. } else if (state == menu_valves_done) {
  1000. switch_to(menu_b);
  1001. } else if (state == menu_aux) {
  1002. if (n == -1) {
  1003. if (db.hasDigits()) {
  1004. backspace();
  1005. db.removeDigit();
  1006. if (menu_entered_digits.length() > 0) {
  1007. menu_entered_digits.remove(menu_entered_digits.length() - 1);
  1008. switch_to(state);
  1009. }
  1010. } else {
  1011. switch_to(menu_c);
  1012. }
  1013. } else if (n == -2) {
  1014. if (!db.hasDigits()) {
  1015. return;
  1016. }
  1017. selected_id = number_input();
  1018. if ((selected_id <= 0) || (selected_id > plants.countAux())) {
  1019. error_condition = F("Invalid valve ID!");
  1020. switch_to(error);
  1021. } else {
  1022. switch_to(menu_aux_time);
  1023. }
  1024. } else {
  1025. if (db.spaceLeft()) {
  1026. db.addDigit(n);
  1027. menu_entered_digits += String(n);
  1028. switch_to(state);
  1029. } else {
  1030. backspace();
  1031. }
  1032. }
  1033. } else if (state == menu_aux_time) {
  1034. if (n == -1) {
  1035. if (db.hasDigits()) {
  1036. backspace();
  1037. db.removeDigit();
  1038. if (menu_entered_digits.length() > 0) {
  1039. menu_entered_digits.remove(menu_entered_digits.length() - 1);
  1040. switch_to(state);
  1041. }
  1042. } else {
  1043. switch_to(menu_aux);
  1044. }
  1045. } else if (n == -2) {
  1046. if (!db.hasDigits()) {
  1047. return;
  1048. }
  1049. selected_time = number_input();
  1050. if ((selected_time <= 0) || (selected_time > MAX_AUX_RUNTIME)) {
  1051. error_condition = F("Invalid time range!");
  1052. switch_to(error);
  1053. } else {
  1054. switch_to(menu_aux_go);
  1055. }
  1056. } else {
  1057. if (db.spaceLeft()) {
  1058. db.addDigit(n);
  1059. menu_entered_digits += String(n);
  1060. switch_to(state);
  1061. } else {
  1062. backspace();
  1063. }
  1064. }
  1065. } else if (state == menu_aux_go) {
  1066. if (n == -2) {
  1067. start_time = millis();
  1068. last_animation_time = start_time;
  1069. plants.startAux(selected_id - 1);
  1070. switch_to(menu_aux_run);
  1071. } else {
  1072. switch_to(menu_aux_time);
  1073. }
  1074. } else if (state == menu_aux_run) {
  1075. plants.abort();
  1076. stop_time = millis();
  1077. switch_to(menu_aux_done);
  1078. } else if (state == menu_aux_done) {
  1079. switch_to(menu_c);
  1080. } else if (state == error) {
  1081. if (old_state != error) {
  1082. switch_to(old_state);
  1083. } else {
  1084. switch_to(menu_a);
  1085. }
  1086. }
  1087. }
  1088. uint32_t Statemachine::number_input(void) {
  1089. for (int i = 0; i < db.countDigits(); i++) {
  1090. backspace();
  1091. }
  1092. uint32_t n = db.getNumber();
  1093. db.clear();
  1094. debug.print("Whole number input: ");
  1095. debug.println(n);
  1096. return n;
  1097. }
  1098. void Statemachine::act(void) {
  1099. if ((state == menu_pumps_run) || (state == menu_valves_run)
  1100. || (state == menu_aux_run) || (state == fullauto_stirr_run)
  1101. || (state == auto_stirr_run)) {
  1102. unsigned long runtime = millis() - start_time;
  1103. if ((runtime / 1000UL) >= selected_time) {
  1104. // stop if timeout has been reached
  1105. plants.abort();
  1106. stop_time = millis();
  1107. if (state == menu_pumps_run) {
  1108. switch_to(menu_pumps_done);
  1109. } else if (state == menu_valves_run) {
  1110. switch_to(menu_valves_done);
  1111. } else if (state == menu_aux_run) {
  1112. switch_to(menu_aux_done);
  1113. } else if (state == fullauto_stirr_run) {
  1114. if (selected_ferts.countSet() > 0) {
  1115. selected_time = auto_pump_runtime[0];
  1116. for (int i = 0; i < plants.countFertilizers(); i++) {
  1117. if (auto_pump_runtime[i] > selected_time) {
  1118. selected_time = auto_pump_runtime[i];
  1119. }
  1120. if (selected_ferts.isSet(i)) {
  1121. plants.startFertilizer(i);
  1122. }
  1123. }
  1124. start_time = millis();
  1125. switch_to(fullauto_fert_run);
  1126. } else {
  1127. auto wl = plants.getWaterlevel();
  1128. if ((wl != Plants::full) && (wl != Plants::invalid)) {
  1129. // if the waterlevel is currently empty, we
  1130. // set a flag to record the time to fill
  1131. filling_started_empty = (wl == Plants::empty);
  1132. plants.openWaterInlet();
  1133. selected_id = plants.countPlants() + 1;
  1134. selected_time = MAX_TANK_FILL_TIME;
  1135. start_time = millis();
  1136. switch_to(fullauto_tank_run);
  1137. } else if (wl == Plants::full) {
  1138. // check if kickstart is required for this
  1139. bool need_kickstart = false;
  1140. for (int i = 0; i < plants.countPlants(); i++) {
  1141. if (selected_plants.isSet(i)) {
  1142. if (plants.getKickstart()->getPinNumber(i) >= 0) {
  1143. need_kickstart = true;
  1144. }
  1145. }
  1146. }
  1147. // start kickstart/valve as needed
  1148. for (int i = 0; i < plants.countPlants(); i++) {
  1149. if (selected_plants.isSet(i)) {
  1150. plants.startPlant(i, need_kickstart);
  1151. }
  1152. }
  1153. watering_started_full = (wl == Plants::full);
  1154. selected_time = MAX_AUTO_PLANT_RUNTIME;
  1155. start_time = millis();
  1156. if (need_kickstart) {
  1157. switch_to(fullauto_kickstart_run);
  1158. } else {
  1159. switch_to(fullauto_plant_run);
  1160. }
  1161. } else if (wl == Plants::invalid) {
  1162. plants.abort();
  1163. error_condition = F("Invalid sensor state");
  1164. state = fullauto_fert;
  1165. switch_to(error);
  1166. }
  1167. }
  1168. } else {
  1169. switch_to(auto_done);
  1170. }
  1171. } else if ((millis() - last_animation_time) >= 500) {
  1172. // update animation if needed
  1173. last_animation_time = millis();
  1174. switch_to(state);
  1175. }
  1176. }
  1177. #ifdef CHECK_SENSORS_VALVE_PUMP_MENU_FULL
  1178. if ((state == menu_pumps_run) || ((state == menu_valves_run) && (selected_id == (plants.countPlants() + 1)))) {
  1179. // check water level state
  1180. auto wl = plants.getWaterlevel();
  1181. if (wl == Plants::full) {
  1182. plants.abort();
  1183. stop_time = millis();
  1184. switch_to((state == menu_pumps_run) ? menu_pumps_done : menu_valves_done);
  1185. } else if (wl == Plants::invalid) {
  1186. plants.abort();
  1187. error_condition = F("Invalid sensor state");
  1188. state = (state == menu_pumps_run) ? menu_pumps : menu_valves;
  1189. switch_to(error);
  1190. }
  1191. }
  1192. #endif // CHECK_SENSORS_VALVE_PUMP_MENU_FULL
  1193. #ifdef CHECK_SENSORS_VALVE_PUMP_MENU_EMPTY
  1194. if ((state == menu_valves_run) && (selected_id <= plants.countPlants())) {
  1195. // check water level state
  1196. auto wl = plants.getWaterlevel();
  1197. if (wl == Plants::empty) {
  1198. plants.abort();
  1199. stop_time = millis();
  1200. switch_to(menu_valves_done);
  1201. } else if (wl == Plants::invalid) {
  1202. plants.abort();
  1203. error_condition = F("Invalid sensor state");
  1204. state = menu_valves;
  1205. switch_to(error);
  1206. }
  1207. }
  1208. #endif // CHECK_SENSORS_VALVE_PUMP_MENU_EMPTY
  1209. // kickstart states
  1210. if ((state == auto_plant_kickstart_run) || (state == fillnwater_kickstart_run)
  1211. || (state == fullauto_kickstart_run) || (state == fullauto_kickstart_purge_run)) {
  1212. unsigned long runtime = millis() - start_time;
  1213. if ((runtime / 1000UL) >= KICKSTART_RUNTIME) {
  1214. // kickstart is done, switch over to valves
  1215. plants.abort();
  1216. selected_time = MAX_AUTO_PLANT_RUNTIME;
  1217. start_time = millis();
  1218. // start required valves
  1219. for (int i = 0; i < plants.countPlants(); i++) {
  1220. if (selected_plants.isSet(i)) {
  1221. plants.startPlant(i, false);
  1222. }
  1223. }
  1224. if (state == auto_plant_kickstart_run) {
  1225. switch_to(auto_plant_run);
  1226. } else if (state == fillnwater_kickstart_run) {
  1227. switch_to(fillnwater_plant_run);
  1228. } else if (state == fullauto_kickstart_run) {
  1229. switch_to(fullauto_plant_run);
  1230. } else {
  1231. switch_to(fullauto_plant_purge_run);
  1232. }
  1233. } else if ((millis() - last_animation_time) >= 500) {
  1234. // update animation if needed
  1235. last_animation_time = millis();
  1236. switch_to(state);
  1237. }
  1238. }
  1239. if (state == fullauto_fert_run) {
  1240. unsigned long runtime = millis() - start_time;
  1241. for (int i = 0; i < plants.countFertilizers(); i++) {
  1242. if (selected_ferts.isSet(i)) {
  1243. if ((runtime / 1000UL) >= auto_pump_runtime[i]) {
  1244. plants.stopFertilizer(i);
  1245. selected_ferts.set(i, false);
  1246. }
  1247. }
  1248. }
  1249. if ((millis() - last_animation_time) >= 500) {
  1250. // update animation if needed
  1251. last_animation_time = millis();
  1252. switch_to(state);
  1253. }
  1254. if (selected_ferts.countSet() <= 0) {
  1255. plants.abort();
  1256. auto wl = plants.getWaterlevel();
  1257. if ((wl != Plants::full) && (wl != Plants::invalid)) {
  1258. // if the waterlevel is currently empty, we
  1259. // set a flag to record the time to fill
  1260. filling_started_empty = (wl == Plants::empty);
  1261. plants.openWaterInlet();
  1262. selected_id = plants.countPlants() + 1;
  1263. selected_time = MAX_TANK_FILL_TIME;
  1264. start_time = millis();
  1265. switch_to(fullauto_tank_run);
  1266. } else if (wl == Plants::full) {
  1267. // check if kickstart is required for this
  1268. bool need_kickstart = false;
  1269. for (int i = 0; i < plants.countPlants(); i++) {
  1270. if (selected_plants.isSet(i)) {
  1271. if (plants.getKickstart()->getPinNumber(i) >= 0) {
  1272. need_kickstart = true;
  1273. }
  1274. }
  1275. }
  1276. // start kickstart/valve as needed
  1277. for (int i = 0; i < plants.countPlants(); i++) {
  1278. if (selected_plants.isSet(i)) {
  1279. plants.startPlant(i, need_kickstart);
  1280. }
  1281. }
  1282. // for recording the flowrate
  1283. watering_started_full = (wl == Plants::full);
  1284. selected_time = MAX_AUTO_PLANT_RUNTIME;
  1285. start_time = millis();
  1286. if (need_kickstart) {
  1287. switch_to(fullauto_kickstart_run);
  1288. } else {
  1289. switch_to(fullauto_plant_run);
  1290. }
  1291. } else if (wl == Plants::invalid) {
  1292. error_condition = F("Invalid sensor state");
  1293. state = auto_mode_a;
  1294. switch_to(error);
  1295. }
  1296. }
  1297. }
  1298. // states that fill the tank up
  1299. if ((state == auto_fert_run) || (state == auto_tank_run)
  1300. || (state == fullauto_tank_run) || (state == fullauto_tank_purge_run)
  1301. || (state == fillnwater_tank_run)) {
  1302. unsigned long runtime = millis() - start_time;
  1303. if ((runtime / 1000UL) >= selected_time) {
  1304. // stop if timeout has been reached
  1305. plants.abort();
  1306. stop_time = millis();
  1307. if ((state == fillnwater_tank_run)
  1308. || (state == fullauto_tank_run)
  1309. || (state == fullauto_tank_purge_run)) {
  1310. auto wl = plants.getWaterlevel();
  1311. if ((wl != Plants::empty) && (wl != Plants::invalid)) {
  1312. // check if kickstart is required for this
  1313. bool need_kickstart = false;
  1314. for (int i = 0; i < plants.countPlants(); i++) {
  1315. if (selected_plants.isSet(i)) {
  1316. if (plants.getKickstart()->getPinNumber(i) >= 0) {
  1317. need_kickstart = true;
  1318. }
  1319. }
  1320. }
  1321. // start kickstart/valve as needed
  1322. for (int i = 0; i < plants.countPlants(); i++) {
  1323. if (selected_plants.isSet(i)) {
  1324. plants.startPlant(i, need_kickstart);
  1325. }
  1326. }
  1327. watering_started_full = (wl == Plants::full);
  1328. selected_time = MAX_AUTO_PLANT_RUNTIME;
  1329. start_time = millis();
  1330. if (need_kickstart) {
  1331. if (state == fillnwater_tank_run) {
  1332. switch_to(fillnwater_kickstart_run);
  1333. } else if (state == fullauto_tank_run) {
  1334. switch_to(fullauto_kickstart_run);
  1335. } else {
  1336. switch_to(fullauto_kickstart_purge_run);
  1337. }
  1338. } else {
  1339. if (state == fillnwater_tank_run) {
  1340. switch_to(fillnwater_plant_run);
  1341. } else if (state == fullauto_tank_run) {
  1342. switch_to(fullauto_plant_run);
  1343. } else {
  1344. switch_to(fullauto_plant_purge_run);
  1345. }
  1346. }
  1347. } else if (wl == Plants::empty) {
  1348. stop_time = millis();
  1349. switch_to(auto_done);
  1350. } else if (wl == Plants::invalid) {
  1351. error_condition = F("Invalid sensor state");
  1352. state = auto_mode_a;
  1353. switch_to(error);
  1354. }
  1355. } else {
  1356. switch_to(auto_done);
  1357. }
  1358. } else if ((millis() - last_animation_time) >= 500) {
  1359. // update animation if needed
  1360. last_animation_time = millis();
  1361. switch_to(state);
  1362. }
  1363. // check water level state
  1364. auto wl = plants.getWaterlevel();
  1365. if (wl == Plants::full) {
  1366. plants.abort();
  1367. stop_time = millis();
  1368. if ((state == fillnwater_tank_run)
  1369. || (state == fullauto_tank_run)
  1370. || (state == fullauto_tank_purge_run)) {
  1371. // record time to fill here, if we started with
  1372. // an empty tank at the start of filling
  1373. if (filling_started_empty) {
  1374. unsigned long time_to_fill = stop_time - start_time;
  1375. debug.print("Filling tank took ");
  1376. debug.print(String(time_to_fill));
  1377. debug.println("ms");
  1378. #if defined(PLATFORM_ESP)
  1379. wifi_write_database(time_to_fill, "calibrated_filling", -1);
  1380. #endif // PLATFORM_ESP
  1381. }
  1382. // check if kickstart is required for this
  1383. bool need_kickstart = false;
  1384. for (int i = 0; i < plants.countPlants(); i++) {
  1385. if (selected_plants.isSet(i)) {
  1386. if (plants.getKickstart()->getPinNumber(i) >= 0) {
  1387. need_kickstart = true;
  1388. }
  1389. }
  1390. }
  1391. // start kickstart/valve as needed
  1392. for (int i = 0; i < plants.countPlants(); i++) {
  1393. if (selected_plants.isSet(i)) {
  1394. plants.startPlant(i, need_kickstart);
  1395. }
  1396. }
  1397. watering_started_full = (wl == Plants::full);
  1398. selected_time = MAX_AUTO_PLANT_RUNTIME;
  1399. start_time = millis();
  1400. if (need_kickstart) {
  1401. if (state == fillnwater_tank_run) {
  1402. switch_to(fillnwater_kickstart_run);
  1403. } else if (state == fullauto_tank_run) {
  1404. switch_to(fullauto_kickstart_run);
  1405. } else {
  1406. switch_to(fullauto_kickstart_purge_run);
  1407. }
  1408. } else {
  1409. if (state == fillnwater_tank_run) {
  1410. switch_to(fillnwater_plant_run);
  1411. } else if (state == fullauto_tank_run) {
  1412. switch_to(fullauto_plant_run);
  1413. } else {
  1414. switch_to(fullauto_plant_purge_run);
  1415. }
  1416. }
  1417. } else {
  1418. switch_to(auto_done);
  1419. }
  1420. } else if (wl == Plants::invalid) {
  1421. plants.abort();
  1422. error_condition = F("Invalid sensor state");
  1423. state = auto_mode_a;
  1424. switch_to(error);
  1425. }
  1426. }
  1427. // states that empty the tank
  1428. if ((state == auto_plant_run) || (state == fillnwater_plant_run)
  1429. || (state == fullauto_plant_run) || (state == fullauto_plant_overrun)
  1430. || (state == fullauto_plant_purge_run)
  1431. || (state == fullauto_plant_purge_overrun)
  1432. || (state == fullauto_plant_purge_over_pump_run)) {
  1433. unsigned long runtime = millis() - start_time;
  1434. if ((runtime / 1000UL) >= selected_time) {
  1435. if (state == fullauto_plant_run) {
  1436. start_time = millis();
  1437. selected_time = OVERRUN_RUNTIME;
  1438. switch_to(fullauto_plant_overrun);
  1439. } else if (state == fullauto_plant_overrun) {
  1440. plants.abort();
  1441. stop_time = millis();
  1442. auto wl = plants.getWaterlevel();
  1443. if ((wl != Plants::full) && (wl != Plants::invalid)) {
  1444. // if the waterlevel is currently empty, we
  1445. // set a flag to record the time to fill
  1446. filling_started_empty = (wl == Plants::empty);
  1447. plants.openWaterInlet();
  1448. selected_id = plants.countPlants() + 1;
  1449. selected_time = PURGE_FILL_RUNTIME;
  1450. start_time = millis();
  1451. switch_to(fullauto_tank_purge_run);
  1452. } else if (wl == Plants::full) {
  1453. // check if kickstart is required for this
  1454. bool need_kickstart = false;
  1455. for (int i = 0; i < plants.countPlants(); i++) {
  1456. if (selected_plants.isSet(i)) {
  1457. if (plants.getKickstart()->getPinNumber(i) >= 0) {
  1458. need_kickstart = true;
  1459. }
  1460. }
  1461. }
  1462. // start kickstart/valve as needed
  1463. for (int i = 0; i < plants.countPlants(); i++) {
  1464. if (selected_plants.isSet(i)) {
  1465. plants.startPlant(i, need_kickstart);
  1466. }
  1467. }
  1468. // for recording the flowrate
  1469. watering_started_full = (wl == Plants::full);
  1470. selected_time = MAX_AUTO_PLANT_RUNTIME;
  1471. start_time = millis();
  1472. if (need_kickstart) {
  1473. switch_to(fullauto_kickstart_purge_run);
  1474. } else {
  1475. switch_to(fullauto_plant_purge_run);
  1476. }
  1477. } else if (wl == Plants::invalid) {
  1478. error_condition = F("Invalid sensor state");
  1479. state = auto_mode_a;
  1480. switch_to(error);
  1481. }
  1482. } else if (state == fullauto_plant_purge_run) {
  1483. start_time = millis();
  1484. selected_time = OVERRUN_RUNTIME;
  1485. switch_to(fullauto_plant_purge_overrun);
  1486. } else if (state == fullauto_plant_purge_overrun) {
  1487. plants.abort();
  1488. stop_time = millis();
  1489. bool need_kickstart = false;
  1490. for (int i = 0; i < plants.countPlants(); i++) {
  1491. if (selected_plants.isSet(i)) {
  1492. if (plants.getKickstart()->getPinNumber(i) >= 0) {
  1493. need_kickstart = true;
  1494. }
  1495. }
  1496. }
  1497. start_time = millis();
  1498. selected_time = FULLAUTO_END_PUMP_TIME;
  1499. if (need_kickstart) {
  1500. for (int i = 0; i < plants.countPlants(); i++) {
  1501. if (selected_plants.isSet(i)) {
  1502. plants.startPlant(i, true);
  1503. }
  1504. }
  1505. switch_to(fullauto_plant_purge_over_pump_run);
  1506. } else {
  1507. switch_to(fullauto_done);
  1508. }
  1509. } else if (state == fullauto_plant_purge_over_pump_run) {
  1510. plants.abort();
  1511. stop_time = millis();
  1512. switch_to(fullauto_done);
  1513. } else {
  1514. plants.abort();
  1515. stop_time = millis();
  1516. switch_to(auto_done);
  1517. }
  1518. } else if ((millis() - last_animation_time) >= 500) {
  1519. // update animation if needed
  1520. last_animation_time = millis();
  1521. switch_to(state);
  1522. }
  1523. if ((state == fullauto_plant_overrun)
  1524. || (state == fullauto_plant_purge_overrun)
  1525. || (state == fullauto_plant_purge_over_pump_run)) {
  1526. // overrun should not exit when sensor returns empty
  1527. return;
  1528. }
  1529. // check water level state
  1530. auto wl = plants.getWaterlevel();
  1531. if (wl == Plants::empty) {
  1532. if ((state == auto_plant_run) || (state == fillnwater_plant_run)) {
  1533. plants.abort();
  1534. stop_time = millis();
  1535. }
  1536. if ((state == auto_plant_run) || (state == fillnwater_plant_run)
  1537. || (state == fullauto_plant_run)) {
  1538. // if we started watering with a full tank
  1539. // and then finished watering when it was empty
  1540. // and we were only watering a single plant
  1541. // look at this as a "calibration run" and record
  1542. // the time it took to empty the tank
  1543. if (watering_started_full && (selected_plants.countSet() == 1)) {
  1544. unsigned long time_to_water = stop_time - start_time;
  1545. debug.print("Watering plant ");
  1546. debug.print(selected_plants.getFirstSet() + 1);
  1547. debug.print(" with the complete tank took ");
  1548. debug.print(String(time_to_water));
  1549. debug.println("ms");
  1550. #if defined(PLATFORM_ESP)
  1551. wifi_write_database(time_to_water, "calibrated_watering", selected_plants.getFirstSet() + 1);
  1552. #endif // PLATFORM_ESP
  1553. }
  1554. }
  1555. if ((state == auto_plant_run) || (state == fillnwater_plant_run)) {
  1556. switch_to(auto_done);
  1557. } else if (state == fullauto_plant_run) {
  1558. start_time = millis();
  1559. selected_time = OVERRUN_RUNTIME;
  1560. switch_to(fullauto_plant_overrun);
  1561. } else if (state == fullauto_plant_purge_run) {
  1562. start_time = millis();
  1563. selected_time = OVERRUN_RUNTIME;
  1564. switch_to(fullauto_plant_purge_overrun);
  1565. }
  1566. } else if (wl == Plants::invalid) {
  1567. plants.abort();
  1568. error_condition = F("Invalid sensor state");
  1569. state = auto_mode_a;
  1570. switch_to(error);
  1571. }
  1572. }
  1573. // timeout in user-input states
  1574. if ((state == menu_a) || (state == menu_b) || (state == menu_c)
  1575. || (state == automation_mode) || (state == auto_done)
  1576. || (state == auto_mode_a) || (state == auto_mode_b)
  1577. || (state == auto_fert_a) || (state == auto_fert_b)
  1578. || (state == auto_plant) || (state == fillnwater_plant)
  1579. || (state == menu_pumps) || (state == menu_pumps_time)
  1580. || (state == menu_pumps_go) || (state == menu_pumps_done)
  1581. || (state == menu_valves) || (state == menu_valves_time)
  1582. || (state == menu_valves_go) || (state == menu_valves_done)
  1583. || (state == menu_aux) || (state == menu_aux_time)
  1584. || (state == menu_aux_go) || (state == menu_aux_done)
  1585. || (state == fullauto_fert) || (state == fullauto_plant)
  1586. || (state == fullauto_done)) {
  1587. unsigned long runtime = millis() - into_state_time;
  1588. if (runtime >= BACK_TO_IDLE_TIMEOUT) {
  1589. debug.print("Idle timeout reached in state ");
  1590. debug.println(state_names[state]);
  1591. switch_to(init);
  1592. }
  1593. }
  1594. }
  1595. void Statemachine::switch_to(States s) {
  1596. old_state = state;
  1597. state = s;
  1598. into_state_time = millis();
  1599. if (old_state != state) {
  1600. // don't spam log with every animation state "change"
  1601. debug.print("switch_to ");
  1602. debug.print(state_names[old_state]);
  1603. debug.print(" --> ");
  1604. debug.println(state_names[state]);
  1605. menu_entered_digits = "";
  1606. }
  1607. if (s == init) {
  1608. String a = String(F("- Giess-o-mat V")) + FIRMWARE_VERSION + String(F(" -"));
  1609. #if (LOCK_COUNT > 0) && defined(DOOR_LOCK_PIN)
  1610. String b = String(F("PIN: ")) + menu_entered_digits;
  1611. print(a.c_str(),
  1612. "* or # to enter menu",
  1613. "Enter PIN for locks:",
  1614. b.c_str(),
  1615. 3);
  1616. #else
  1617. print(a.c_str(),
  1618. "Usage: Enter number",
  1619. "* Delete prev. digit",
  1620. "# Execute input num.",
  1621. -1);
  1622. #endif
  1623. } else if (s == door_select) {
  1624. String a = String(F("(Input 1 to ")) + String(LOCK_COUNT) + String(F(")"));
  1625. String b = String(F("Door: ")) + menu_entered_digits;
  1626. print("- Select Door Lock -",
  1627. "Leave empty if done!",
  1628. a.c_str(),
  1629. b.c_str(),
  1630. 3);
  1631. } else if (s == menu_a) {
  1632. print("----- Menu 1/3 -----",
  1633. "1: Manual Operation",
  1634. "2: Automation",
  1635. "#: Go to page 2/3...",
  1636. -1);
  1637. } else if (s == menu_b) {
  1638. print("----- Menu 2/3 -----",
  1639. "3: Fertilizer pumps",
  1640. "4: Outlet valves",
  1641. "#: Go to page 3/3...",
  1642. -1);
  1643. } else if (s == menu_c) {
  1644. print("----- Menu 3/3 -----",
  1645. "5: Aux. Outputs",
  1646. #if (LOCK_COUNT > 0) && !defined(DOOR_LOCK_PIN)
  1647. "6: Door Locks",
  1648. #else
  1649. "",
  1650. #endif
  1651. "#: Go to page 1/3...",
  1652. -1);
  1653. } else if (state == automation_mode) {
  1654. // TODO
  1655. print("---- Automation ----",
  1656. "TODO NOT IMPLEMENTED",
  1657. "TODO NOT IMPLEMENTED",
  1658. "TODO NOT IMPLEMENTED",
  1659. -1);
  1660. } else if (s == auto_mode_a) {
  1661. print("---- Manual 1/3 ----",
  1662. "1: Full-Auto Mode",
  1663. "2: Add Fertilizer",
  1664. "#: Go to page 2/3...",
  1665. -1);
  1666. } else if (s == auto_mode_b) {
  1667. print("---- Manual 2/3 ----",
  1668. "3: Fill 'n' Water",
  1669. "4: Fill Reservoir",
  1670. "#: Go to page 3/3...",
  1671. -1);
  1672. } else if (s == auto_mode_c) {
  1673. print("---- Manual 3/3 ----",
  1674. "5: Water a plant",
  1675. "",
  1676. "#: Go to page 1/3...",
  1677. -1);
  1678. } else if (s == auto_fert_a) {
  1679. print("-- Fertilizer 1/2 --",
  1680. "1: Vegetation Phase",
  1681. "2: Bloom Phase",
  1682. "#: Go to page 2/2...",
  1683. -1);
  1684. } else if (s == auto_fert_b) {
  1685. print("-- Fertilizer 2/2 --",
  1686. "3: Special Fert.",
  1687. "4: Run Stirrer",
  1688. "#: Go to page 1/2...",
  1689. -1);
  1690. } else if ((s == auto_fert_run) || (s == fullauto_fert_run)) {
  1691. unsigned long runtime = millis() - start_time;
  1692. String a = String(F("Time: ")) + String(runtime / 1000UL) + String(F("s / ")) + String(selected_time) + String('s');
  1693. unsigned long anim = runtime * 20UL / (selected_time * 1000UL);
  1694. String b;
  1695. for (unsigned long i = 0; i < anim; i++) {
  1696. b += '#';
  1697. }
  1698. print("---- Dispensing ----",
  1699. a.c_str(),
  1700. b.c_str(),
  1701. "Hit any key to stop!",
  1702. -1);
  1703. } else if ((s == auto_stirr_run) || (s == fullauto_stirr_run)) {
  1704. unsigned long runtime = millis() - start_time;
  1705. String a = String(F("Time: ")) + String(runtime / 1000UL) + String(F("s / ")) + String(selected_time) + String('s');
  1706. unsigned long anim = runtime * 20UL / (selected_time * 1000UL);
  1707. String b;
  1708. for (unsigned long i = 0; i < anim; i++) {
  1709. b += '#';
  1710. }
  1711. print("----- Stirring -----",
  1712. a.c_str(),
  1713. b.c_str(),
  1714. "Hit any key to stop!",
  1715. -1);
  1716. } else if ((s == auto_tank_run) || (s == fillnwater_tank_run) || (s == fullauto_tank_run) || (s == fullauto_tank_purge_run)) {
  1717. unsigned long runtime = millis() - start_time;
  1718. String a = String(F("Time: ")) + String(runtime / 1000UL) + String(F("s / ")) + String(selected_time) + String('s');
  1719. unsigned long anim = runtime * 20UL / (selected_time * 1000UL);
  1720. String b;
  1721. for (unsigned long i = 0; i < anim; i++) {
  1722. b += '#';
  1723. }
  1724. print("--- Filling Tank ---",
  1725. a.c_str(),
  1726. b.c_str(),
  1727. "Hit any key to stop!",
  1728. -1);
  1729. } else if ((s == auto_plant) || (s == fillnwater_plant) || (s == fullauto_plant)) {
  1730. String a = String(F("(Input 1 to ")) + String(plants.countPlants()) + String(F(")"));
  1731. String b = String(F("Plant: ")) + menu_entered_digits;
  1732. print("--- Select Plant ---",
  1733. "Leave empty if done!",
  1734. a.c_str(),
  1735. b.c_str(),
  1736. 3);
  1737. } else if (s == fullauto_fert) {
  1738. String a = String(F("(Input 1 to ")) + String(plants.countFertilizers()) + String(F(")"));
  1739. String b = String(F("Fert.: ")) + menu_entered_digits;
  1740. print("--- Select Fert. ---",
  1741. "Leave empty if done!",
  1742. a.c_str(),
  1743. b.c_str(),
  1744. 3);
  1745. } else if ((s == auto_plant_kickstart_run) || (s == fillnwater_kickstart_run) || (s == fullauto_kickstart_run) || (s == fullauto_kickstart_purge_run)) {
  1746. unsigned long runtime = millis() - start_time;
  1747. String a = String(F("Time: ")) + String(runtime / 1000UL) + String(F("s / ")) + String(KICKSTART_RUNTIME) + String('s');
  1748. unsigned long anim = runtime * 20UL / (KICKSTART_RUNTIME * 1000UL);
  1749. String b;
  1750. for (unsigned long i = 0; i < anim; i++) {
  1751. b += '#';
  1752. }
  1753. print("---- Kick-Start ----",
  1754. a.c_str(),
  1755. b.c_str(),
  1756. "Hit any key to stop!",
  1757. -1);
  1758. } else if ((s == auto_plant_run) || (s == fillnwater_plant_run) || (s == fullauto_plant_run) || (s == fullauto_plant_purge_run)) {
  1759. unsigned long runtime = millis() - start_time;
  1760. String a = String(F("Time: ")) + String(runtime / 1000UL) + String(F("s / ")) + String(selected_time) + String('s');
  1761. unsigned long anim = runtime * 20UL / (selected_time * 1000UL);
  1762. String b;
  1763. for (unsigned long i = 0; i < anim; i++) {
  1764. b += '#';
  1765. }
  1766. print("----- Watering -----",
  1767. a.c_str(),
  1768. b.c_str(),
  1769. "Hit any key to stop!",
  1770. -1);
  1771. } else if ((s == fullauto_plant_overrun)
  1772. || (s == fullauto_plant_purge_overrun)
  1773. || (s == fullauto_plant_purge_over_pump_run)) {
  1774. unsigned long runtime = millis() - start_time;
  1775. String a = String(F("Time: ")) + String(runtime / 1000UL) + String(F("s / ")) + String(selected_time) + String('s');
  1776. unsigned long anim = runtime * 20UL / (selected_time * 1000UL);
  1777. String b;
  1778. for (unsigned long i = 0; i < anim; i++) {
  1779. b += '#';
  1780. }
  1781. print("-- Emptying lines --",
  1782. a.c_str(),
  1783. b.c_str(),
  1784. "Hit any key to stop!",
  1785. -1);
  1786. } else if ((s == auto_done) || (s == fullauto_done)) {
  1787. String a = String(F("after ")) + String((stop_time - start_time) / 1000UL) + String(F("s."));
  1788. print("------- Done -------",
  1789. "Dispensing finished",
  1790. a.c_str(),
  1791. "Hit any key for menu",
  1792. -1);
  1793. #if defined(PLATFORM_ESP)
  1794. unsigned long runtime = stop_time - start_time;
  1795. if ((old_state == auto_plant_run) || (old_state == fillnwater_plant_run)) {
  1796. for (int i = 0; i < plants.countPlants(); i++) {
  1797. if (selected_plants.isSet(i)) {
  1798. wifi_write_database(runtime / 1000, "plant", i + 1);
  1799. }
  1800. }
  1801. } else if (old_state == auto_fert_run) {
  1802. wifi_write_database(runtime / 1000, "fertilizer", selected_id);
  1803. }
  1804. #endif // PLATFORM_ESP
  1805. } else if (s == menu_pumps) {
  1806. String a = String(F("(Input 1 to ")) + String(plants.countFertilizers()) + String(F(")"));
  1807. String b = String(F("Pump: ")) + menu_entered_digits;
  1808. print("------- Pump -------",
  1809. "Please select pump",
  1810. a.c_str(),
  1811. b.c_str(),
  1812. 3);
  1813. } else if (s == menu_pumps_time) {
  1814. String header = String(F("------ Pump ")) + String(selected_id) + String(F(" ------"));
  1815. String b = String(F("Runtime: ")) + menu_entered_digits;
  1816. print(header.c_str(),
  1817. "Please set runtime",
  1818. "(Input in seconds)",
  1819. b.c_str(),
  1820. 3);
  1821. } else if (s == menu_pumps_go) {
  1822. String a = String(F("Pump No. ")) + String(selected_id);
  1823. String b = String(F("Runtime ")) + String(selected_time) + String('s');
  1824. print("----- Confirm? -----",
  1825. a.c_str(),
  1826. b.c_str(),
  1827. " # Confirm",
  1828. -1);
  1829. } else if (s == menu_pumps_run) {
  1830. unsigned long runtime = millis() - start_time;
  1831. String a = String(F("Time: ")) + String(runtime / 1000UL) + String(F("s / ")) + String(selected_time) + String('s');
  1832. unsigned long anim = runtime * 20UL / (selected_time * 1000UL);
  1833. String b;
  1834. for (unsigned long i = 0; i < anim; i++) {
  1835. b += '#';
  1836. }
  1837. print("---- Dispensing ----",
  1838. a.c_str(),
  1839. b.c_str(),
  1840. "Hit any key to stop!",
  1841. -1);
  1842. } else if (s == menu_pumps_done) {
  1843. String a = String(F("after ")) + String((stop_time - start_time) / 1000UL) + String(F("s."));
  1844. print("------- Done -------",
  1845. "Dispensing finished",
  1846. a.c_str(),
  1847. "Hit any key for menu",
  1848. -1);
  1849. #if defined(PLATFORM_ESP)
  1850. unsigned long runtime = stop_time - start_time;
  1851. wifi_write_database(runtime / 1000, "fertilizer", selected_id);
  1852. #endif // PLATFORM_ESP
  1853. } else if (s == menu_valves) {
  1854. String a = String(F("(Input 1 to ")) + String(plants.countPlants() + 1) + String(F(")"));
  1855. String b = String(F("Valve: ")) + menu_entered_digits;
  1856. print("------ Valves ------",
  1857. "Please select valve",
  1858. a.c_str(),
  1859. b.c_str(),
  1860. 3);
  1861. } else if (s == menu_valves_time) {
  1862. String header = String(F("----- Valve ")) + String(selected_id) + String(F(" -----"));
  1863. String b = String(F("Runtime: ")) + menu_entered_digits;
  1864. print(header.c_str(),
  1865. "Please set runtime",
  1866. "(Input in seconds)",
  1867. b.c_str(),
  1868. 3);
  1869. } else if (s == menu_valves_go) {
  1870. String a = String(F("Valve No. ")) + String(selected_id);
  1871. String b = String(F("Runtime ")) + String(selected_time) + String('s');
  1872. print("----- Confirm? -----",
  1873. a.c_str(),
  1874. b.c_str(),
  1875. " # Confirm",
  1876. -1);
  1877. } else if (s == menu_valves_run) {
  1878. unsigned long runtime = millis() - start_time;
  1879. String a = String(F("Time: ")) + String(runtime / 1000UL) + String(F("s / ")) + String(selected_time) + String('s');
  1880. unsigned long anim = runtime * 20UL / (selected_time * 1000UL);
  1881. String b;
  1882. for (unsigned long i = 0; i <= anim; i++) {
  1883. b += '#';
  1884. }
  1885. print("---- Dispensing ----",
  1886. a.c_str(),
  1887. b.c_str(),
  1888. "Hit any key to stop!",
  1889. -1);
  1890. } else if (s == menu_valves_done) {
  1891. String a = String(F("after ")) + String((stop_time - start_time) / 1000UL) + String(F("s."));
  1892. print("------- Done -------",
  1893. "Dispensing finished",
  1894. a.c_str(),
  1895. "Hit any key for menu",
  1896. -1);
  1897. #if defined(PLATFORM_ESP)
  1898. unsigned long runtime = stop_time - start_time;
  1899. if (selected_id <= plants.countPlants()) {
  1900. wifi_write_database(runtime / 1000, "plant", selected_id);
  1901. }
  1902. #endif // PLATFORM_ESP
  1903. } else if (s == menu_aux) {
  1904. String a = String(F("(Input 1 to ")) + String(plants.countAux()) + String(F(")"));
  1905. String b = String(F("Aux.: ")) + menu_entered_digits;
  1906. print("------- Aux. -------",
  1907. "Please select aux.",
  1908. a.c_str(),
  1909. b.c_str(),
  1910. 3);
  1911. } else if (s == menu_aux_time) {
  1912. String header = String(F("------ Aux ")) + String(selected_id) + String(F(" ------"));
  1913. String b = String(F("Runtime: ")) + menu_entered_digits;
  1914. print(header.c_str(),
  1915. "Please set runtime",
  1916. "(Input in seconds)",
  1917. b.c_str(),
  1918. 3);
  1919. } else if (s == menu_aux_go) {
  1920. String a = String(F("Aux No. ")) + String(selected_id);
  1921. String b = String(F("Runtime ")) + String(selected_time) + String('s');
  1922. print("----- Confirm? -----",
  1923. a.c_str(),
  1924. b.c_str(),
  1925. " # Confirm",
  1926. -1);
  1927. } else if (s == menu_aux_run) {
  1928. unsigned long runtime = millis() - start_time;
  1929. String a = String(F("Time: ")) + String(runtime / 1000UL) + String(F("s / ")) + String(selected_time) + String('s');
  1930. unsigned long anim = runtime * 20UL / (selected_time * 1000UL);
  1931. String b;
  1932. for (unsigned long i = 0; i <= anim; i++) {
  1933. b += '#';
  1934. }
  1935. print("----- Stirring -----",
  1936. a.c_str(),
  1937. b.c_str(),
  1938. "Hit any key to stop!",
  1939. -1);
  1940. } else if (s == menu_aux_done) {
  1941. String a = String(F("after ")) + String((stop_time - start_time) / 1000UL) + String(F("s."));
  1942. print("------- Done -------",
  1943. "Aux. run finished",
  1944. a.c_str(),
  1945. "Hit any key for menu",
  1946. -1);
  1947. } else if (s == error) {
  1948. print("------ Error! ------",
  1949. "There is a problem:",
  1950. error_condition.c_str(),
  1951. " Press any key...",
  1952. -1);
  1953. } else {
  1954. debug.print("Invalid state ");
  1955. debug.println(s);
  1956. }
  1957. }
  1958. #endif