Skip to content

Instantly share code, notes, and snippets.

@PatchworkBoy
Created December 22, 2016 14:58
Show Gist options
  • Save PatchworkBoy/ecea0e2bedcbcd03dcfd2557412b027d to your computer and use it in GitHub Desktop.
Save PatchworkBoy/ecea0e2bedcbcd03dcfd2557412b027d to your computer and use it in GitHub Desktop.
Stepper Motor Flow
[{"id":"43898bc4.bc7674","type":"subflow","name":"Stepper Motor","info":"","in":[{"x":40,"y":120,"wires":[{"id":"a04d8ed0.5fb27"}]}],"out":[{"x":940,"y":40,"wires":[{"id":"6a04cb0f.95fb34","port":0}]},{"x":940,"y":80,"wires":[{"id":"6a04cb0f.95fb34","port":1}]},{"x":940,"y":120,"wires":[{"id":"6a04cb0f.95fb34","port":2}]},{"x":940,"y":160,"wires":[{"id":"6a04cb0f.95fb34","port":3}]},{"x":940,"y":200,"wires":[{"id":"6a04cb0f.95fb34","port":4},{"id":"1ea1a2a7.e15e5d","port":0}]}]},{"id":"1ea1a2a7.e15e5d","type":"function","z":"43898bc4.bc7674","name":"Toggle Direction","func":"var direction = flow.get('direction')||0;\n\nif (direction === -1)\n{\n direction = 1;\n}\nelse {\n direction = -1;\n}\nflow.set('direction',direction);\nmsg.payload = direction;\nreturn msg;","outputs":1,"noerr":0,"x":380,"y":140,"wires":[[]]},{"id":"a04d8ed0.5fb27","type":"switch","z":"43898bc4.bc7674","name":"Step or Direction","property":"topic","propertyType":"msg","rules":[{"t":"eq","v":"Step","vt":"str"},{"t":"eq","v":"Direction","vt":"str"}],"checkall":"true","outputs":2,"x":170,"y":120,"wires":[["ffccd35e.00333"],["1ea1a2a7.e15e5d"]]},{"id":"2cc49f4c.d33b6","type":"comment","z":"43898bc4.bc7674","name":"Flow Variables","info":"* Direction\n* Sequence\n* SequenceIndex","x":90,"y":60,"wires":[]},{"id":"6a04cb0f.95fb34","type":"function","z":"43898bc4.bc7674","name":"Sequence Generator","func":"var seqindex = context.get('seqindex')||0;\nvar sequence = [ [1,0,0,0],[1,1,0,0],[0,1,0,0],[0,1,1,0],[0,0,1,0],[0,0,1,1],[0,0,0,1],[1,0,0,1]]\nvar direction = flow.get('direction');\n\nseqindex = (seqindex % (sequence.length));\nif (seqindex == -1) {seqindex = sequence.length-1;}\n\nmsg.payload = sequence[seqindex];\nmsg.seqindex = seqindex;\nmsg.direction = direction;\n\ncontext.set('seqindex',seqindex+direction);\n\nnode.status({fill:\"green\",shape:\"dot\",text:\"Step\"});\nnode.status({});\n\nreturn GenerateOutput(seqindex);\n\nfunction GenerateOutput(index) {\n return [ NewMessage(sequence[index][0]), NewMessage(sequence[index][1]),NewMessage(sequence[index][2]),NewMessage(sequence[index][3]), msg ];\n}\n\nfunction NewMessage(val) {\n return { payload:val };\n}","outputs":"5","noerr":0,"x":720,"y":100,"wires":[[],[],[],[],[]]},{"id":"ffccd35e.00333","type":"function","z":"43898bc4.bc7674","name":"Multistep","func":"var outputMsgs = [];\nvar steps = msg.payload;\nfor (var count = 0; count < steps; count++) { \n outputMsgs.push({payload:1});\n}\nreturn [ outputMsgs ];","outputs":1,"noerr":0,"x":360,"y":100,"wires":[["7e93c84.f816c38"]]},{"id":"7e93c84.f816c38","type":"delay","z":"43898bc4.bc7674","name":"","pauseType":"rate","timeout":"5","timeoutUnits":"seconds","rate":"500","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"x":520,"y":100,"wires":[["6a04cb0f.95fb34"]]},{"id":"78f78125.87088","type":"inject","z":"a98ba17c.56746","name":"Step","topic":"Step","payload":"100","payloadType":"num","repeat":"","crontab":"","once":false,"x":90,"y":200,"wires":[["5f53a449.a0ac5c"]]},{"id":"36907358.c96f8c","type":"inject","z":"a98ba17c.56746","name":"Direction","topic":"Direction","payload":"1","payloadType":"num","repeat":"","crontab":"","once":false,"x":100,"y":240,"wires":[["5f53a449.a0ac5c"]]},{"id":"5f53a449.a0ac5c","type":"subflow:43898bc4.bc7674","z":"a98ba17c.56746","name":"","x":380,"y":200,"wires":[["fbd4ba11.042b48"],["9edec3c9.61214"],["47752a9c.b88ad4"],["f5bcfdcf.0a43"],["17d46f62.e82b91"]]},{"id":"ef987155.10679","type":"debug","z":"a98ba17c.56746","name":"1","active":false,"console":"false","complete":"payload","x":790,"y":80,"wires":[]},{"id":"ac3d72ef.53c29","type":"debug","z":"a98ba17c.56746","name":"2","active":false,"console":"false","complete":"payload","x":790,"y":140,"wires":[]},{"id":"5a14a283.a5eb5c","type":"debug","z":"a98ba17c.56746","name":"3","active":false,"console":"false","complete":"payload","x":790,"y":200,"wires":[]},{"id":"e3bcf2dd.1c431","type":"debug","z":"a98ba17c.56746","name":"4","active":false,"console":"false","complete":"payload","x":790,"y":260,"wires":[]},{"id":"17d46f62.e82b91","type":"debug","z":"a98ba17c.56746","name":"Motor Debug","active":false,"console":"false","complete":"true","x":610,"y":320,"wires":[]},{"id":"ac2d12dc.53d2f","type":"inject","z":"a98ba17c.56746","name":"Multistep","topic":"Step","payload":"10","payloadType":"num","repeat":"","crontab":"","once":false,"x":100,"y":160,"wires":[["5f53a449.a0ac5c"]]},{"id":"fbd4ba11.042b48","type":"function","z":"a98ba17c.56746","name":"Green LED","func":"if (msg.payload == 1)\n node.status({fill:\"green\",shape:\"dot\",text:\"LED On\"});\nelse\n node.status({fill:\"green\",shape:\"ring\",text:\"LED Off\"});\nreturn msg;","outputs":1,"noerr":0,"x":610,"y":80,"wires":[["ef987155.10679"]]},{"id":"9edec3c9.61214","type":"function","z":"a98ba17c.56746","name":"Green LED","func":"if (msg.payload == 1)\n node.status({fill:\"green\",shape:\"dot\",text:\"LED On\"});\nelse\n node.status({fill:\"green\",shape:\"ring\",text:\"LED Off\"});\nreturn msg;","outputs":1,"noerr":0,"x":610,"y":140,"wires":[["ac3d72ef.53c29"]]},{"id":"47752a9c.b88ad4","type":"function","z":"a98ba17c.56746","name":"Green LED","func":"if (msg.payload == 1)\n node.status({fill:\"green\",shape:\"dot\",text:\"LED On\"});\nelse\n node.status({fill:\"green\",shape:\"ring\",text:\"LED Off\"});\nreturn msg;","outputs":1,"noerr":0,"x":610,"y":200,"wires":[["5a14a283.a5eb5c"]]},{"id":"f5bcfdcf.0a43","type":"function","z":"a98ba17c.56746","name":"Green LED","func":"if (msg.payload == 1)\n node.status({fill:\"green\",shape:\"dot\",text:\"LED On\"});\nelse\n node.status({fill:\"green\",shape:\"ring\",text:\"LED Off\"});\nreturn msg;","outputs":1,"noerr":0,"x":610,"y":260,"wires":[["e3bcf2dd.1c431"]]}]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment