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 75KB

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