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

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137
  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. bool success = wifi_write_database(time_to_fill, "calibrated_filling", -1);
  1371. if (!success) {
  1372. debug.print("Error writing to InfluxDB ");
  1373. debug.print(INFLUXDB_HOST);
  1374. debug.print(":");
  1375. debug.print(INFLUXDB_PORT);
  1376. debug.print("/");
  1377. debug.print(INFLUXDB_DATABASE);
  1378. debug.println("/calibrated_filling");
  1379. }
  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. unsigned long runtime = millis() - start_time;
  1433. if ((runtime / 1000UL) >= selected_time) {
  1434. if (state == fullauto_plant_run) {
  1435. start_time = millis();
  1436. selected_time = OVERRUN_RUNTIME;
  1437. switch_to(fullauto_plant_overrun);
  1438. } else if (state == fullauto_plant_overrun) {
  1439. plants.abort();
  1440. stop_time = millis();
  1441. auto wl = plants.getWaterlevel();
  1442. if ((wl != Plants::full) && (wl != Plants::invalid)) {
  1443. // if the waterlevel is currently empty, we
  1444. // set a flag to record the time to fill
  1445. filling_started_empty = (wl == Plants::empty);
  1446. plants.openWaterInlet();
  1447. selected_id = plants.countPlants() + 1;
  1448. selected_time = PURGE_FILL_RUNTIME;
  1449. start_time = millis();
  1450. switch_to(fullauto_tank_purge_run);
  1451. } else if (wl == Plants::full) {
  1452. // check if kickstart is required for this
  1453. bool need_kickstart = false;
  1454. for (int i = 0; i < plants.countPlants(); i++) {
  1455. if (selected_plants.isSet(i)) {
  1456. if (plants.getKickstart()->getPinNumber(i) >= 0) {
  1457. need_kickstart = true;
  1458. }
  1459. }
  1460. }
  1461. // start kickstart/valve as needed
  1462. for (int i = 0; i < plants.countPlants(); i++) {
  1463. if (selected_plants.isSet(i)) {
  1464. plants.startPlant(i, need_kickstart);
  1465. }
  1466. }
  1467. // for recording the flowrate
  1468. watering_started_full = (wl == Plants::full);
  1469. selected_time = MAX_AUTO_PLANT_RUNTIME;
  1470. start_time = millis();
  1471. if (need_kickstart) {
  1472. switch_to(fullauto_kickstart_purge_run);
  1473. } else {
  1474. switch_to(fullauto_plant_purge_run);
  1475. }
  1476. } else if (wl == Plants::invalid) {
  1477. error_condition = F("Invalid sensor state");
  1478. state = auto_mode_a;
  1479. switch_to(error);
  1480. }
  1481. } else if (state == fullauto_plant_purge_run) {
  1482. start_time = millis();
  1483. selected_time = OVERRUN_RUNTIME;
  1484. switch_to(fullauto_plant_purge_overrun);
  1485. } else if (state == fullauto_plant_purge_overrun) {
  1486. plants.abort();
  1487. stop_time = millis();
  1488. switch_to(fullauto_done);
  1489. } else {
  1490. plants.abort();
  1491. stop_time = millis();
  1492. switch_to(auto_done);
  1493. }
  1494. } else if ((millis() - last_animation_time) >= 500) {
  1495. // update animation if needed
  1496. last_animation_time = millis();
  1497. switch_to(state);
  1498. }
  1499. if ((state == fullauto_plant_overrun) || (state == fullauto_plant_purge_overrun)) {
  1500. // overrun should not exit when sensor returns empty
  1501. return;
  1502. }
  1503. // check water level state
  1504. auto wl = plants.getWaterlevel();
  1505. if (wl == Plants::empty) {
  1506. if ((state == auto_plant_run) || (state == fillnwater_plant_run)) {
  1507. plants.abort();
  1508. stop_time = millis();
  1509. }
  1510. if ((state == auto_plant_run) || (state == fillnwater_plant_run)
  1511. || (state == fullauto_plant_run)) {
  1512. // if we started watering with a full tank
  1513. // and then finished watering when it was empty
  1514. // and we were only watering a single plant
  1515. // look at this as a "calibration run" and record
  1516. // the time it took to empty the tank
  1517. if (watering_started_full && (selected_plants.countSet() == 1)) {
  1518. unsigned long time_to_water = stop_time - start_time;
  1519. debug.print("Watering plant ");
  1520. debug.print(selected_plants.getFirstSet() + 1);
  1521. debug.print(" with the complete tank took ");
  1522. debug.print(String(time_to_water));
  1523. debug.println("ms");
  1524. #if defined(PLATFORM_ESP)
  1525. bool success = wifi_write_database(time_to_water, "calibrated_watering", selected_plants.getFirstSet() + 1);
  1526. if (!success) {
  1527. debug.print("Error writing to InfluxDB ");
  1528. debug.print(INFLUXDB_HOST);
  1529. debug.print(":");
  1530. debug.print(INFLUXDB_PORT);
  1531. debug.print("/");
  1532. debug.print(INFLUXDB_DATABASE);
  1533. debug.println("/calibrated_watering");
  1534. }
  1535. #endif // PLATFORM_ESP
  1536. }
  1537. }
  1538. if ((state == auto_plant_run) || (state == fillnwater_plant_run)) {
  1539. switch_to(auto_done);
  1540. } else if (state == fullauto_plant_run) {
  1541. start_time = millis();
  1542. selected_time = OVERRUN_RUNTIME;
  1543. switch_to(fullauto_plant_overrun);
  1544. } else if (state == fullauto_plant_purge_run) {
  1545. start_time = millis();
  1546. selected_time = OVERRUN_RUNTIME;
  1547. switch_to(fullauto_plant_purge_overrun);
  1548. }
  1549. } else if (wl == Plants::invalid) {
  1550. plants.abort();
  1551. error_condition = F("Invalid sensor state");
  1552. state = auto_mode_a;
  1553. switch_to(error);
  1554. }
  1555. }
  1556. // timeout in user-input states
  1557. if ((state == menu_a) || (state == menu_b) || (state == menu_c)
  1558. || (state == automation_mode) || (state == auto_done)
  1559. || (state == auto_mode_a) || (state == auto_mode_b)
  1560. || (state == auto_fert_a) || (state == auto_fert_b)
  1561. || (state == auto_plant) || (state == fillnwater_plant)
  1562. || (state == menu_pumps) || (state == menu_pumps_time)
  1563. || (state == menu_pumps_go) || (state == menu_pumps_done)
  1564. || (state == menu_valves) || (state == menu_valves_time)
  1565. || (state == menu_valves_go) || (state == menu_valves_done)
  1566. || (state == menu_aux) || (state == menu_aux_time)
  1567. || (state == menu_aux_go) || (state == menu_aux_done)
  1568. || (state == fullauto_fert) || (state == fullauto_plant)
  1569. || (state == fullauto_done)) {
  1570. unsigned long runtime = millis() - into_state_time;
  1571. if (runtime >= BACK_TO_IDLE_TIMEOUT) {
  1572. debug.print("Idle timeout reached in state ");
  1573. debug.println(state_names[state]);
  1574. switch_to(init);
  1575. }
  1576. }
  1577. }
  1578. void Statemachine::switch_to(States s) {
  1579. old_state = state;
  1580. state = s;
  1581. into_state_time = millis();
  1582. if (old_state != state) {
  1583. // don't spam log with every animation state "change"
  1584. debug.print("switch_to ");
  1585. debug.print(state_names[old_state]);
  1586. debug.print(" --> ");
  1587. debug.println(state_names[state]);
  1588. menu_entered_digits = "";
  1589. }
  1590. if (s == init) {
  1591. String a = String(F("- Giess-o-mat V")) + FIRMWARE_VERSION + String(F(" -"));
  1592. #if (LOCK_COUNT > 0) && defined(DOOR_LOCK_PIN)
  1593. String b = String(F("PIN: ")) + menu_entered_digits;
  1594. print(a.c_str(),
  1595. "* or # to enter menu",
  1596. "Enter PIN for locks:",
  1597. b.c_str(),
  1598. 3);
  1599. #else
  1600. print(a.c_str(),
  1601. "Usage: Enter number",
  1602. "* Delete prev. digit",
  1603. "# Execute input num.",
  1604. -1);
  1605. #endif
  1606. } else if (s == door_select) {
  1607. String a = String(F("(Input 1 to ")) + String(LOCK_COUNT) + String(F(")"));
  1608. String b = String(F("Door: ")) + menu_entered_digits;
  1609. print("- Select Door Lock -",
  1610. "Leave empty if done!",
  1611. a.c_str(),
  1612. b.c_str(),
  1613. 3);
  1614. } else if (s == menu_a) {
  1615. print("----- Menu 1/3 -----",
  1616. "1: Manual Operation",
  1617. "2: Automation",
  1618. "#: Go to page 2/3...",
  1619. -1);
  1620. } else if (s == menu_b) {
  1621. print("----- Menu 2/3 -----",
  1622. "3: Fertilizer pumps",
  1623. "4: Outlet valves",
  1624. "#: Go to page 3/3...",
  1625. -1);
  1626. } else if (s == menu_c) {
  1627. print("----- Menu 3/3 -----",
  1628. "5: Aux. Outputs",
  1629. #if (LOCK_COUNT > 0) && !defined(DOOR_LOCK_PIN)
  1630. "6: Door Locks",
  1631. #else
  1632. "",
  1633. #endif
  1634. "#: Go to page 1/3...",
  1635. -1);
  1636. } else if (state == automation_mode) {
  1637. // TODO
  1638. print("---- Automation ----",
  1639. "TODO NOT IMPLEMENTED",
  1640. "TODO NOT IMPLEMENTED",
  1641. "TODO NOT IMPLEMENTED",
  1642. -1);
  1643. } else if (s == auto_mode_a) {
  1644. print("---- Manual 1/3 ----",
  1645. "1: Full-Auto Mode",
  1646. "2: Add Fertilizer",
  1647. "#: Go to page 2/3...",
  1648. -1);
  1649. } else if (s == auto_mode_b) {
  1650. print("---- Manual 2/3 ----",
  1651. "3: Fill 'n' Water",
  1652. "4: Fill Reservoir",
  1653. "#: Go to page 3/3...",
  1654. -1);
  1655. } else if (s == auto_mode_c) {
  1656. print("---- Manual 3/3 ----",
  1657. "5: Water a plant",
  1658. "",
  1659. "#: Go to page 1/3...",
  1660. -1);
  1661. } else if (s == auto_fert_a) {
  1662. print("-- Fertilizer 1/2 --",
  1663. "1: Vegetation Phase",
  1664. "2: Bloom Phase",
  1665. "#: Go to page 2/2...",
  1666. -1);
  1667. } else if (s == auto_fert_b) {
  1668. print("-- Fertilizer 2/2 --",
  1669. "3: Special Fert.",
  1670. "4: Run Stirrer",
  1671. "#: Go to page 1/2...",
  1672. -1);
  1673. } else if ((s == auto_fert_run) || (s == fullauto_fert_run)) {
  1674. unsigned long runtime = millis() - start_time;
  1675. String a = String(F("Time: ")) + String(runtime / 1000UL) + String(F("s / ")) + String(selected_time) + String('s');
  1676. unsigned long anim = runtime * 20UL / (selected_time * 1000UL);
  1677. String b;
  1678. for (unsigned long i = 0; i < anim; i++) {
  1679. b += '#';
  1680. }
  1681. print("---- Dispensing ----",
  1682. a.c_str(),
  1683. b.c_str(),
  1684. "Hit any key to stop!",
  1685. -1);
  1686. } else if ((s == auto_stirr_run) || (s == fullauto_stirr_run)) {
  1687. unsigned long runtime = millis() - start_time;
  1688. String a = String(F("Time: ")) + String(runtime / 1000UL) + String(F("s / ")) + String(selected_time) + String('s');
  1689. unsigned long anim = runtime * 20UL / (selected_time * 1000UL);
  1690. String b;
  1691. for (unsigned long i = 0; i < anim; i++) {
  1692. b += '#';
  1693. }
  1694. print("----- Stirring -----",
  1695. a.c_str(),
  1696. b.c_str(),
  1697. "Hit any key to stop!",
  1698. -1);
  1699. } else if ((s == auto_tank_run) || (s == fillnwater_tank_run) || (s == fullauto_tank_run) || (s == fullauto_tank_purge_run)) {
  1700. unsigned long runtime = millis() - start_time;
  1701. String a = String(F("Time: ")) + String(runtime / 1000UL) + String(F("s / ")) + String(selected_time) + String('s');
  1702. unsigned long anim = runtime * 20UL / (selected_time * 1000UL);
  1703. String b;
  1704. for (unsigned long i = 0; i < anim; i++) {
  1705. b += '#';
  1706. }
  1707. print("--- Filling Tank ---",
  1708. a.c_str(),
  1709. b.c_str(),
  1710. "Hit any key to stop!",
  1711. -1);
  1712. } else if ((s == auto_plant) || (s == fillnwater_plant) || (s == fullauto_plant)) {
  1713. String a = String(F("(Input 1 to ")) + String(plants.countPlants()) + String(F(")"));
  1714. String b = String(F("Plant: ")) + menu_entered_digits;
  1715. print("--- Select Plant ---",
  1716. "Leave empty if done!",
  1717. a.c_str(),
  1718. b.c_str(),
  1719. 3);
  1720. } else if (s == fullauto_fert) {
  1721. String a = String(F("(Input 1 to ")) + String(plants.countFertilizers()) + String(F(")"));
  1722. String b = String(F("Fert.: ")) + menu_entered_digits;
  1723. print("--- Select Fert. ---",
  1724. "Leave empty if done!",
  1725. a.c_str(),
  1726. b.c_str(),
  1727. 3);
  1728. } else if ((s == auto_plant_kickstart_run) || (s == fillnwater_kickstart_run) || (s == fullauto_kickstart_run) || (s == fullauto_kickstart_purge_run)) {
  1729. unsigned long runtime = millis() - start_time;
  1730. String a = String(F("Time: ")) + String(runtime / 1000UL) + String(F("s / ")) + String(KICKSTART_RUNTIME) + String('s');
  1731. unsigned long anim = runtime * 20UL / (KICKSTART_RUNTIME * 1000UL);
  1732. String b;
  1733. for (unsigned long i = 0; i < anim; i++) {
  1734. b += '#';
  1735. }
  1736. print("---- Kick-Start ----",
  1737. a.c_str(),
  1738. b.c_str(),
  1739. "Hit any key to stop!",
  1740. -1);
  1741. } else if ((s == auto_plant_run) || (s == fillnwater_plant_run) || (s == fullauto_plant_run) || (s == fullauto_plant_purge_run)) {
  1742. unsigned long runtime = millis() - start_time;
  1743. String a = String(F("Time: ")) + String(runtime / 1000UL) + String(F("s / ")) + String(selected_time) + String('s');
  1744. unsigned long anim = runtime * 20UL / (selected_time * 1000UL);
  1745. String b;
  1746. for (unsigned long i = 0; i < anim; i++) {
  1747. b += '#';
  1748. }
  1749. print("----- Watering -----",
  1750. a.c_str(),
  1751. b.c_str(),
  1752. "Hit any key to stop!",
  1753. -1);
  1754. } else if ((s == fullauto_plant_overrun) || (s == fullauto_plant_purge_overrun)) {
  1755. unsigned long runtime = millis() - start_time;
  1756. String a = String(F("Time: ")) + String(runtime / 1000UL) + String(F("s / ")) + String(selected_time) + String('s');
  1757. unsigned long anim = runtime * 20UL / (selected_time * 1000UL);
  1758. String b;
  1759. for (unsigned long i = 0; i < anim; i++) {
  1760. b += '#';
  1761. }
  1762. print("-- Emptying lines --",
  1763. a.c_str(),
  1764. b.c_str(),
  1765. "Hit any key to stop!",
  1766. -1);
  1767. } else if ((s == auto_done) || (s == fullauto_done)) {
  1768. String a = String(F("after ")) + String((stop_time - start_time) / 1000UL) + String(F("s."));
  1769. print("------- Done -------",
  1770. "Dispensing finished",
  1771. a.c_str(),
  1772. "Hit any key for menu",
  1773. -1);
  1774. #if defined(PLATFORM_ESP)
  1775. unsigned long runtime = stop_time - start_time;
  1776. if ((old_state == auto_plant_run) || (old_state == fillnwater_plant_run)) {
  1777. for (int i = 0; i < plants.countPlants(); i++) {
  1778. if (selected_plants.isSet(i)) {
  1779. bool success = wifi_write_database(runtime / 1000, "plant", i + 1);
  1780. if (!success) {
  1781. debug.print("Error writing to InfluxDB ");
  1782. debug.print(INFLUXDB_HOST);
  1783. debug.print(":");
  1784. debug.print(INFLUXDB_PORT);
  1785. debug.print("/");
  1786. debug.print(INFLUXDB_DATABASE);
  1787. debug.println("/plant");
  1788. }
  1789. }
  1790. }
  1791. } else if (old_state == auto_fert_run) {
  1792. bool success = wifi_write_database(runtime / 1000, "fertilizer", selected_id);
  1793. if (!success) {
  1794. debug.print("Error writing to InfluxDB ");
  1795. debug.print(INFLUXDB_HOST);
  1796. debug.print(":");
  1797. debug.print(INFLUXDB_PORT);
  1798. debug.print("/");
  1799. debug.print(INFLUXDB_DATABASE);
  1800. debug.println("/fertilizer");
  1801. }
  1802. }
  1803. #endif // PLATFORM_ESP
  1804. } else if (s == menu_pumps) {
  1805. String a = String(F("(Input 1 to ")) + String(plants.countFertilizers()) + String(F(")"));
  1806. String b = String(F("Pump: ")) + menu_entered_digits;
  1807. print("------- Pump -------",
  1808. "Please select pump",
  1809. a.c_str(),
  1810. b.c_str(),
  1811. 3);
  1812. } else if (s == menu_pumps_time) {
  1813. String header = String(F("------ Pump ")) + String(selected_id) + String(F(" ------"));
  1814. String b = String(F("Runtime: ")) + menu_entered_digits;
  1815. print(header.c_str(),
  1816. "Please set runtime",
  1817. "(Input in seconds)",
  1818. b.c_str(),
  1819. 3);
  1820. } else if (s == menu_pumps_go) {
  1821. String a = String(F("Pump No. ")) + String(selected_id);
  1822. String b = String(F("Runtime ")) + String(selected_time) + String('s');
  1823. print("----- Confirm? -----",
  1824. a.c_str(),
  1825. b.c_str(),
  1826. " # Confirm",
  1827. -1);
  1828. } else if (s == menu_pumps_run) {
  1829. unsigned long runtime = millis() - start_time;
  1830. String a = String(F("Time: ")) + String(runtime / 1000UL) + String(F("s / ")) + String(selected_time) + String('s');
  1831. unsigned long anim = runtime * 20UL / (selected_time * 1000UL);
  1832. String b;
  1833. for (unsigned long i = 0; i < anim; i++) {
  1834. b += '#';
  1835. }
  1836. print("---- Dispensing ----",
  1837. a.c_str(),
  1838. b.c_str(),
  1839. "Hit any key to stop!",
  1840. -1);
  1841. } else if (s == menu_pumps_done) {
  1842. String a = String(F("after ")) + String((stop_time - start_time) / 1000UL) + String(F("s."));
  1843. print("------- Done -------",
  1844. "Dispensing finished",
  1845. a.c_str(),
  1846. "Hit any key for menu",
  1847. -1);
  1848. #if defined(PLATFORM_ESP)
  1849. unsigned long runtime = stop_time - start_time;
  1850. bool success = wifi_write_database(runtime / 1000, "fertilizer", selected_id);
  1851. if (!success) {
  1852. debug.print("Error writing to InfluxDB ");
  1853. debug.print(INFLUXDB_HOST);
  1854. debug.print(":");
  1855. debug.print(INFLUXDB_PORT);
  1856. debug.print("/");
  1857. debug.print(INFLUXDB_DATABASE);
  1858. debug.println("/fertilizer");
  1859. }
  1860. #endif // PLATFORM_ESP
  1861. } else if (s == menu_valves) {
  1862. String a = String(F("(Input 1 to ")) + String(plants.countPlants() + 1) + String(F(")"));
  1863. String b = String(F("Valve: ")) + menu_entered_digits;
  1864. print("------ Valves ------",
  1865. "Please select valve",
  1866. a.c_str(),
  1867. b.c_str(),
  1868. 3);
  1869. } else if (s == menu_valves_time) {
  1870. String header = String(F("----- Valve ")) + String(selected_id) + String(F(" -----"));
  1871. String b = String(F("Runtime: ")) + menu_entered_digits;
  1872. print(header.c_str(),
  1873. "Please set runtime",
  1874. "(Input in seconds)",
  1875. b.c_str(),
  1876. 3);
  1877. } else if (s == menu_valves_go) {
  1878. String a = String(F("Valve No. ")) + String(selected_id);
  1879. String b = String(F("Runtime ")) + String(selected_time) + String('s');
  1880. print("----- Confirm? -----",
  1881. a.c_str(),
  1882. b.c_str(),
  1883. " # Confirm",
  1884. -1);
  1885. } else if (s == menu_valves_run) {
  1886. unsigned long runtime = millis() - start_time;
  1887. String a = String(F("Time: ")) + String(runtime / 1000UL) + String(F("s / ")) + String(selected_time) + String('s');
  1888. unsigned long anim = runtime * 20UL / (selected_time * 1000UL);
  1889. String b;
  1890. for (unsigned long i = 0; i <= anim; i++) {
  1891. b += '#';
  1892. }
  1893. print("---- Dispensing ----",
  1894. a.c_str(),
  1895. b.c_str(),
  1896. "Hit any key to stop!",
  1897. -1);
  1898. } else if (s == menu_valves_done) {
  1899. String a = String(F("after ")) + String((stop_time - start_time) / 1000UL) + String(F("s."));
  1900. print("------- Done -------",
  1901. "Dispensing finished",
  1902. a.c_str(),
  1903. "Hit any key for menu",
  1904. -1);
  1905. #if defined(PLATFORM_ESP)
  1906. unsigned long runtime = stop_time - start_time;
  1907. if (selected_id <= plants.countPlants()) {
  1908. bool success = wifi_write_database(runtime / 1000, "plant", selected_id);
  1909. if (!success) {
  1910. debug.print("Error writing to InfluxDB ");
  1911. debug.print(INFLUXDB_HOST);
  1912. debug.print(":");
  1913. debug.print(INFLUXDB_PORT);
  1914. debug.print("/");
  1915. debug.print(INFLUXDB_DATABASE);
  1916. debug.println("/plant");
  1917. }
  1918. }
  1919. #endif // PLATFORM_ESP
  1920. } else if (s == menu_aux) {
  1921. String a = String(F("(Input 1 to ")) + String(plants.countAux()) + String(F(")"));
  1922. String b = String(F("Aux.: ")) + menu_entered_digits;
  1923. print("------- Aux. -------",
  1924. "Please select aux.",
  1925. a.c_str(),
  1926. b.c_str(),
  1927. 3);
  1928. } else if (s == menu_aux_time) {
  1929. String header = String(F("------ Aux ")) + String(selected_id) + String(F(" ------"));
  1930. String b = String(F("Runtime: ")) + menu_entered_digits;
  1931. print(header.c_str(),
  1932. "Please set runtime",
  1933. "(Input in seconds)",
  1934. b.c_str(),
  1935. 3);
  1936. } else if (s == menu_aux_go) {
  1937. String a = String(F("Aux No. ")) + String(selected_id);
  1938. String b = String(F("Runtime ")) + String(selected_time) + String('s');
  1939. print("----- Confirm? -----",
  1940. a.c_str(),
  1941. b.c_str(),
  1942. " # Confirm",
  1943. -1);
  1944. } else if (s == menu_aux_run) {
  1945. unsigned long runtime = millis() - start_time;
  1946. String a = String(F("Time: ")) + String(runtime / 1000UL) + String(F("s / ")) + String(selected_time) + String('s');
  1947. unsigned long anim = runtime * 20UL / (selected_time * 1000UL);
  1948. String b;
  1949. for (unsigned long i = 0; i <= anim; i++) {
  1950. b += '#';
  1951. }
  1952. print("----- Stirring -----",
  1953. a.c_str(),
  1954. b.c_str(),
  1955. "Hit any key to stop!",
  1956. -1);
  1957. } else if (s == menu_aux_done) {
  1958. String a = String(F("after ")) + String((stop_time - start_time) / 1000UL) + String(F("s."));
  1959. print("------- Done -------",
  1960. "Aux. run finished",
  1961. a.c_str(),
  1962. "Hit any key for menu",
  1963. -1);
  1964. } else if (s == error) {
  1965. print("------ Error! ------",
  1966. "There is a problem:",
  1967. error_condition.c_str(),
  1968. " Press any key...",
  1969. -1);
  1970. } else {
  1971. debug.print("Invalid state ");
  1972. debug.println(s);
  1973. }
  1974. }
  1975. #endif