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

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