-
Notifications
You must be signed in to change notification settings - Fork 4
Traffic lights
cflurin edited this page Jul 26, 2018
·
4 revisions
One popular example to demonstrate a finite state machine
is the traffic lights
. Although this example probably has no practical use here is an approach using dsm:
[{"id":"48c9381b.7857e8","type":"dsm","z":"cb099c1f.9ea6b","name":"traffic lights","sm_config":"{\n \"stateOutput\": \"payload\",\n \"currentState\": \"off\",\n \"states\": {\n \"off\": {\n \"tick\": \"red\"\n },\n \"red\": {\n \"tick\": \"red-yellow\"\n },\n \"red-yellow\": {\n \"tick\": \"green\"\n },\n \"green\": {\n \"tick\": \"yellow\"\n },\n \"yellow\": {\n \"tick\": \"red\"\n }\n }\n}","x":280,"y":940,"wires":[["68d15547.81d9ec","fb4e1989.08e338","91651b02.a230d8"]]},{"id":"4c587a93.60a224","type":"inject","z":"cb099c1f.9ea6b","name":"Trigger","topic":"tick","payload":"","payloadType":"date","repeat":"5","crontab":"","once":false,"onceDelay":0.1,"x":120,"y":940,"wires":[["48c9381b.7857e8"]]},{"id":"b8c51b02.a4c8d8","type":"debug","z":"cb099c1f.9ea6b","name":"red","active":true,"tosidebar":false,"console":false,"tostatus":true,"complete":"payload","x":570,"y":880,"wires":[]},{"id":"d13037a2.bbead8","type":"debug","z":"cb099c1f.9ea6b","name":"yellow","active":true,"tosidebar":false,"console":false,"tostatus":true,"complete":"payload","x":570,"y":940,"wires":[]},{"id":"cc9b216b.ee659","type":"debug","z":"cb099c1f.9ea6b","name":"green","active":true,"tosidebar":false,"console":false,"tostatus":true,"complete":"payload","x":570,"y":1000,"wires":[]},{"id":"ba34dc98.5e40c","type":"comment","z":"cb099c1f.9ea6b","name":"Traffic lights _______________________________________","info":"","x":250,"y":820,"wires":[]},{"id":"68d15547.81d9ec","type":"dsm","z":"cb099c1f.9ea6b","name":"","sm_config":"{\n \"methods\": {\n \"onTransition\": \"output=true;if(msg.payload==='red'||msg.payload==='red-yellow') {msg.payload=1;sta.fill='red';} else {msg.payload=0;sta.fill='grey';}\",\n \"status\": {\n \"shape\": \"dot\",\n \"text\": {\n \"get\": \"msg.payload\"\n }\n }\n }\n}","x":450,"y":880,"wires":[["b8c51b02.a4c8d8"]]},{"id":"fb4e1989.08e338","type":"dsm","z":"cb099c1f.9ea6b","name":"","sm_config":"{\n \"methods\": {\n \"onTransition\": \"output=true;if(msg.payload==='yellow'||msg.payload==='red-yellow') {msg.payload=1;sta.fill='yellow';} else {msg.payload=0;sta.fill='grey';}\",\n \"status\": {\n \"shape\": \"dot\",\n \"text\": {\n \"get\": \"msg.payload\"\n }\n }\n }\n}","x":450,"y":940,"wires":[["d13037a2.bbead8"]]},{"id":"91651b02.a230d8","type":"dsm","z":"cb099c1f.9ea6b","name":"","sm_config":"{\n \"methods\": {\n \"onTransition\": \"output=true;if(msg.payload==='green') {msg.payload=1;sta.fill='green';} else {msg.payload=0;sta.fill='grey';}\",\n \"status\": {\n \"shape\": \"dot\",\n \"text\": {\n \"get\": \"msg.payload\"\n }\n }\n }\n}","x":450,"y":1000,"wires":[["cc9b216b.ee659"]]}]