While Loop not shutting down

Hi All,

Hoping someone will take a look, I'm trying to understand why my "while loop" nodes are acting...odd:

[{"id":"fc2942fb.cd18","type":"group","z":"c199b9b6.017a2","name":"Text Labels","style":{"stroke":"#92d04f","label":true,"color":"#000000"},"nodes":["d5ef05f8.79b11","e1031575.90e7b"],"x":864,"y":279,"w":342,"h":82},{"id":"d5ef05f8.79b11","type":"ui_text","z":"c199b9b6.017a2","g":"fc2942fb.cd18","group":"4eddf1f8.72684","order":2,"width":1,"height":1,"name":"open","label":"Open","format":"{{msg.payload}}","layout":"row-right","x":940,"y":320,"wires":[]},{"id":"e1031575.90e7b","type":"ui_text","z":"c199b9b6.017a2","g":"fc2942fb.cd18","group":"4eddf1f8.72684","order":4,"width":1,"height":1,"name":"close","label":"Closed","format":"{{msg.payload}}","layout":"row-right","x":1130,"y":320,"wires":[]},{"id":"4eddf1f8.72684","type":"ui_group","z":"","name":"text labels","tab":"81831aab.f2f568","order":2,"disp":false,"width":6,"collapse":false},{"id":"81831aab.f2f568","type":"ui_tab","z":"","name":"GTO-500 Gate Opener","icon":"dashboard","disabled":false,"hidden":false}]

Run individually, these two sets work fine to control one label. When you click both "Opening" and "Closing" inject nodes before stopping either set, the loop(s) go into runaway. There's really no chance of getting these multiple inputs because it'll be hardware eventually controlling them. I'm just trying to understand why the loops aren't stopping. I've had a debug node on every output and they all do what they're supposed to. Tried !=0 inside the whiles, tried boolean, tried numbers (0/1). Could it be a glitch in the startBlink functions?

EDIT: Also happens if I hit the same start button more than once. It's like the loops never see a 0...

BTW, I don't know who to thank for those function nodes, think I glommed them off here though...

Russ

Hi @RASelkirk, you have only posted 2 UI nodes

image

Regarding loops - it generally not a good idea (as you are seeing). Perhaps there is another solution.

Could you explain what you are doing (and post your flow)?

Hmmm, let try that again:

[{"id":"fa369d6f.dde448","type":"ui_text","z":"c199b9b6.017a2","group":"fbf8a8dd.af27e8","order":2,"width":4,"height":1,"name":"blinking label","label":"","format":"{{msg.topic}}","layout":"col-center","x":820,"y":180,"wires":[]},{"id":"47557639.3635c8","type":"inject","z":"c199b9b6.017a2","name":"initialize","props":[{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":true,"onceDelay":0.1,"topic":"STOPPED","x":480,"y":180,"wires":[["fa369d6f.dde448"]]},{"id":"37454ed8.bb979a","type":"group","z":"c199b9b6.017a2","name":"Blink Close","style":{"label":true,"stroke":"#92d04f","color":"#000000","fill":"#92d04f","fill-opacity":"0.25"},"nodes":["101283e3.5a2cec","2ed90451.d4cc34","49f519a.fee9c68"],"x":214,"y":209,"w":322,"h":132,"info":"\"Closing...\" node injects 1 into \"lock-out\" node (prevents multiple injects from wrecking the label), \"lock-out\" node then starts \"startBlink\" function node which sends msg.topic CLOSING to label at user-defined flash rate.\n\n\"Stopped\" node injects 0 into \"lock-out\" node for a reset and into \"stopBlink\" node which sends msg.topic STOPPED to label"},{"id":"101283e3.5a2cec","type":"function","z":"c199b9b6.017a2","g":"37454ed8.bb979a","name":"startBlink","func":"var BLINKDELAY = 800;\n\nvar light = true;\n\n\nvar blinker = setInterval(blink, BLINKDELAY);\n\nglobal.set(\"blinker\", blinker);\n\nfunction blink () {\n    \n    if (light) {\n        msg.payload = false;\n        msg.topic = \"\";\n        light = false;\n    }\n    \n    else {\n        msg.payload = true;\n        msg.topic = \"CLOSING\";\n        light = true;\n    }\n    \n    node.send(msg);\n}\n\n\nreturn;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":450,"y":300,"wires":[["fa369d6f.dde448"]]},{"id":"2ed90451.d4cc34","type":"function","z":"c199b9b6.017a2","g":"37454ed8.bb979a","name":"stopBlink","func":"clearInterval(global.get(\"blinker\"));\n\nmsg.payload = false;\nmsg.topic = \"STOPPED\";\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":450,"y":250,"wires":[["fa369d6f.dde448"]]},{"id":"49f519a.fee9c68","type":"while-loop","z":"c199b9b6.017a2","g":"37454ed8.bb979a","name":"while loop","condi":"msg.payload ==1","x":300,"y":270,"wires":[["2ed90451.d4cc34"],["101283e3.5a2cec"]]},{"id":"3f10c30.2b244be","type":"group","z":"c199b9b6.017a2","name":"GPIO-out Gate Opening","style":{"label":true,"stroke":"#3f93cf","fill":"#3f93cf","fill-opacity":"0.25","color":"#000000"},"nodes":["32a0c914.a72d9e","38ada0a2.3d71"],"x":4,"y":19,"w":192,"h":132},{"id":"32a0c914.a72d9e","type":"inject","z":"c199b9b6.017a2","g":"3f10c30.2b244be","name":"Stopped","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":"","topic":"STOPPED","payload":"0","payloadType":"num","x":110,"y":110,"wires":[["2c6747c3.3855f8"]]},{"id":"38ada0a2.3d71","type":"inject","z":"c199b9b6.017a2","g":"3f10c30.2b244be","name":"Opening","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":"0.5","topic":"","payload":"1","payloadType":"num","x":110,"y":60,"wires":[["2c6747c3.3855f8"]],"info":"Will get input from pi pin containing \"motor running open\" value."},{"id":"639167db.43074","type":"group","z":"c199b9b6.017a2","name":"GPIO-out Gate Closing","style":{"stroke":"#92d04f","fill":"#92d04f","fill-opacity":"0.25","label":true,"color":"#000000"},"nodes":["4d3f319a.4174f8","7114c333.9fbbec"],"x":4,"y":209,"w":192,"h":132},{"id":"4d3f319a.4174f8","type":"inject","z":"c199b9b6.017a2","g":"639167db.43074","name":"Closing","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":"","topic":"","payload":"1","payloadType":"num","x":120,"y":250,"wires":[["49f519a.fee9c68"]]},{"id":"7114c333.9fbbec","type":"inject","z":"c199b9b6.017a2","g":"639167db.43074","name":"Stopped","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":"","topic":"","payload":"0","payloadType":"num","x":110,"y":300,"wires":[["49f519a.fee9c68"]]},{"id":"9fb89002.5b5b","type":"group","z":"c199b9b6.017a2","name":"Blink Open","style":{"label":true,"stroke":"#3f93cf","color":"#000000","fill":"#3f93cf","fill-opacity":"0.25"},"nodes":["3b1e7a35.b3fd86","58e5261d.011b5","2c6747c3.3855f8"],"x":214,"y":19,"w":322,"h":132},{"id":"3b1e7a35.b3fd86","type":"function","z":"c199b9b6.017a2","g":"9fb89002.5b5b","name":"startBlink","func":"var BLINKDELAY = 800;\n\nvar light = true;\n\n\nvar blinker = setInterval(blink, BLINKDELAY);\n\nglobal.set(\"blinker\", blinker);\n\nfunction blink () {\n    \n    if (light) {\n        msg.payload = false;\n        msg.topic = \"\";\n        light = false;\n    }\n    \n    else {\n        msg.payload = true;\n        msg.topic = \"OPENING\";\n        light = true;\n    }\n    \n    node.send(msg);\n}\n\n\nreturn;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":450,"y":110,"wires":[["fa369d6f.dde448"]]},{"id":"58e5261d.011b5","type":"function","z":"c199b9b6.017a2","g":"9fb89002.5b5b","name":"stopBlink","func":"clearInterval(global.get(\"blinker\"));\n\nmsg.payload = false;\nmsg.topic = \"STOPPED\";\n\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":450,"y":60,"wires":[["fa369d6f.dde448"]]},{"id":"2c6747c3.3855f8","type":"while-loop","z":"c199b9b6.017a2","g":"9fb89002.5b5b","name":"while loop","condi":"msg.payload ==1","x":300,"y":80,"wires":[["58e5261d.011b5"],["3b1e7a35.b3fd86"]]},{"id":"fbf8a8dd.af27e8","type":"ui_group","z":"","name":"Moving Label","tab":"81831aab.f2f568","order":3,"disp":false,"width":6,"collapse":false},{"id":"81831aab.f2f568","type":"ui_tab","z":"","name":"GTO-500 Gate Opener","icon":"dashboard","disabled":false,"hidden":false}]

I will eventually have two pi inputs feeding a single label through separate "blink" functions that blink either "OPENING" or CLOSING" depending on which pi pin is active, for that duration the pin is active. If neither pin is active, the label reads "STOPPED". In the end, each pair of two inject nodes will be replaced by a single pi pin's digital input from a hardware interface so this loop "problem" could not happen on the finished product. I'm just puzzled over it's endless looping after multiple injects of "1". No amount of "0" injects will stop it...

Russ

After further investigation, the startBlink function continues on unchecked if "true" is injected thru the loop more than once. I'm not a good coder and have no idea why this is happening, but it won't affect my project...

Russ