Loop increment and decrement

Hi All,

I create node to loop an auto increment then decrement. But my code didn't works. Here is my code:

[{"id":"f0e1fc36.bea","type":"debug","z":"d67945fe.28ff9","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":1089,"y":1024,"wires":[]},{"id":"828e0993.766d1","type":"function","z":"d67945fe.28ff9","name":"","func":"var max = 100;\nvar min = 1;\nvar x = msg.payload;\nvar step = 1;\n\nsetInterval(function() {\n  console.log(x);\n  x += step;\n  if (x === max || x === min) {\n    step = -step;\n  }\n}, 150);\nmsg.payload = x;\nreturn msg;","outputs":1,"noerr":0,"x":806.5,"y":1058,"wires":[["f0e1fc36.bea"]]},{"id":"234a16e.ee0f8ea","type":"inject","z":"d67945fe.28ff9","name":"","topic":"","payload":"1","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":606.5,"y":1064,"wires":[["828e0993.766d1"]]}]

Thank you for any advice

What are you wanting to happen?

You inject something (anything) and it starts counting 1 to 100 then 100 to 1?
Repeating....

Looking at the code, I think it is way above my skill set.

Sorry.

Hope some one else can help. I don't think I can.

So, what do you see in your console? You can change the console.log() to a node.warn() and see it in the debug. I'd also suggest changing the loop from 150 to 15 for testing.'

When you see what you get, ask yourself 'Why is this happening?" and think about what you are seeing and why you see what you see.

Yes, I want to start counting from 1 to 100 then 100 to 1.

Hi @zenofmud

Thank you in advance for your advice. I've try your advice, and let me fix it.

Here is an update for working code:

[{"id":"64ee5699.091968","type":"debug","z":"d67945fe.28ff9","name":"","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":1162,"y":941,"wires":[]},{"id":"82abd753.035aa8","type":"function","z":"d67945fe.28ff9","name":"","func":"var max = 100;\nvar min = 0;\nvar x = 1;\nvar step = 1;\n\nsetInterval(function() {\n node.warn(x);\n  x+=step;\n  \n  if (x ===max || x===min ) {\n    step=-step;\n  }\n}, 10);\n","outputs":1,"noerr":0,"x":879.5,"y":975,"wires":[["64ee5699.091968"]]},{"id":"e7f52d63.6fd2d8","type":"inject","z":"d67945fe.28ff9","name":"","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":689.5,"y":981,"wires":[["82abd753.035aa8"]]}]
1 Like

added simple start stop to the flow

[{"id":"a27c7af5.cabff8","type":"debug","z":"4af2551d.b2e1ac","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":710,"y":580,"wires":[]},{"id":"898a365c.2acfe8","type":"function","z":"4af2551d.b2e1ac","name":"","func":"var max = 100;\nvar min = 0;\nvar x = 1;\nvar step = 1;\n\nsetInterval(function() {\n    if(flow.get(\"run\") === false) {\n        return null;\n    }\n node.warn(x);\n  x += step;\n  \n  if (x ===max || x===min ) {\n    step=-step;\n  }\n}, 150);\n","outputs":1,"noerr":0,"x":510,"y":580,"wires":[["a27c7af5.cabff8"]]},{"id":"92f7f172.70581","type":"inject","z":"4af2551d.b2e1ac","name":"","topic":"","payload":"true","payloadType":"bool","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":130,"y":560,"wires":[["79de5eb3.0d7be"]]},{"id":"9984c439.32ea78","type":"inject","z":"4af2551d.b2e1ac","name":"","topic":"","payload":"false","payloadType":"bool","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":130,"y":620,"wires":[["79de5eb3.0d7be"]]},{"id":"79de5eb3.0d7be","type":"change","z":"4af2551d.b2e1ac","name":"","rules":[{"t":"set","p":"run","pt":"flow","to":"payload","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":310,"y":580,"wires":[["898a365c.2acfe8"]]}]
1 Like

Looks Better than me! Thanks @homeplayer

Hi, my stupid questions. How to get node.warn (x) value as an msg.payload?

msg.payload = x;
node.send(msg);

1 Like

thanks man!

This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.