[{"id":"425ee0b.7ea6ba","type":"function","z":"18961b84.bc8ec4","name":"Index the State Counter","func":"// In this particular situation the state_counter increments by unity on each clock pulse\n// so you could code it as below\n\n\nvar fsm_state = flow.get(\"state_counter\") || 0;\n\nif (fsm_state > 2)\n {fsm_state = 0;}\nelse\n {fsm_state = fsm_state + 1;}\n\nflow.set(\"state_counter\", fsm_state);\nmsg.payload = fsm_state;\nnode.status({text:\"State counter = \" + fsm_state});\n\nreturn msg;","outputs":"1","noerr":0,"x":430,"y":440,"wires":[[]]},{"id":"c3b14f5b.7113b8","type":"function","z":"18961b84.bc8ec4","name":"Decode RED light","func":"var fsm_state = flow.get(\"state_counter\");\n\nif (fsm_state === 0 || fsm_state == 1)\n {msg.payload = 1;\n node.status({fill:\"red\",shape:\"dot\",text:\"Red ON\"});\n }\n \nelse\n {msg.payload = 0;\n node.status({});\n }\n\nreturn msg;","outputs":1,"noerr":0,"x":710,"y":220,"wires":[["8d90b811.f0f118"]]},{"id":"c385a9ef.be9378","type":"function","z":"18961b84.bc8ec4","name":"Decode YELLOW light","func":"var fsm_state = flow.get(\"state_counter\");\n\nif (fsm_state == 1 || fsm_state == 3)\n {msg.payload = 1;\n node.status({fill:\"yellow\",shape:\"dot\",text:\"Yellow ON\"});\n }\nelse\n {msg.payload = 0;\n node.status({});}\n\nreturn msg;","outputs":1,"noerr":0,"x":720,"y":280,"wires":[["e835800e.edbc78"]]},{"id":"5a6a9553.df0cb4","type":"function","z":"18961b84.bc8ec4","name":"Decode GREEN light","func":"var fsm_state = flow.get(\"state_counter\");\n\nif (fsm_state == 2)\n {msg.payload = 1;\n node.status({fill:\"green\",shape:\"dot\",text:\"Green ON\"});\n }\nelse\n {msg.payload = 0;\n node.status({});\n }\n\nreturn msg;","outputs":1,"noerr":0,"x":720,"y":340,"wires":[["2c6640cf.74028"]]},{"id":"8d90b811.f0f118","type":"mqtt out","z":"18961b84.bc8ec4","name":"","topic":"/node45/gpio/14","qos":"","retain":"","broker":"e3b52ac5.f48f28","x":990,"y":220,"wires":[]},{"id":"e835800e.edbc78","type":"mqtt out","z":"18961b84.bc8ec4","name":"","topic":"/node45/gpio/12","qos":"","retain":"","broker":"e3b52ac5.f48f28","x":990,"y":280,"wires":[]},{"id":"2c6640cf.74028","type":"mqtt out","z":"18961b84.bc8ec4","name":"","topic":"/node45/gpio/13","qos":"","retain":"","broker":"e3b52ac5.f48f28","x":990,"y":340,"wires":[]},{"id":"aaac1f2.80f33e","type":"inject","z":"18961b84.bc8ec4","name":"Trigger every 3 seconds","topic":"","payload":"1","payloadType":"num","repeat":"3","crontab":"","once":true,"x":170,"y":280,"wires":[["cd2a1c3e.320b8"]]},{"id":"cd2a1c3e.320b8","type":"function","z":"18961b84.bc8ec4","name":"Index the State Counter","func":"// Here is the classical way of coding a state machine.\n// It uses a 'case construct' to check the current state and then set the next state.\n\nvar fsm_state = flow.get(\"state_counter\") || 0;\n\nswitch (fsm_state)\n {\n case 0:\n fsm_state = 1;\n break;\n \n case 1:\n fsm_state = 2;\n break;\n \n case 2:\n fsm_state = 3;\n break;\n \n case 3:\n fsm_state = 0;\n break;\n }\n \nflow.set(\"state_counter\", fsm_state);\nmsg.payload = fsm_state;\nnode.status({text:\"State counter = \" + fsm_state});\n\nreturn msg;","outputs":"1","noerr":0,"x":430,"y":280,"wires":[["c3b14f5b.7113b8","c385a9ef.be9378","5a6a9553.df0cb4"]]},{"id":"efa4c220.ba809","type":"comment","z":"18961b84.bc8ec4","name":"Simple way to code the state machine","info":"","x":430,"y":400,"wires":[]},{"id":"e3b52ac5.f48f28","type":"mqtt-broker","z":"","broker":"192.168.1.138","port":"1883","clientid":"","usetls":false,"compatmode":true,"keepalive":"60","cleansession":true,"birthTopic":"","birthQos":"0","birthPayload":"","willTopic":"","willQos":"0","willPayload":""}]