I've already published an example using finite state machines to implement a pedestrian traffic light for Arduino in C++.
This was the first comment on Twitter:
Oh my God this is gonna said the python people offDon't get me wrong I love everybody but everybody has their biased
I know, I know... hackster.io is mainly for hardware examples... but I couldn't resist:
Here it is - the exact same example with Python!
Long story shortState machines are a powerful design pattern to implement complex, event-driven systems. As the complexity rises, they might become more and more interesting. In this example I will show you how to use state machines, generate python code and run it connected to a simple GUI.
How it worksThe GUI contains three buttons. You can turn the state machine on and off. Turned off, it's signaled by blinking lights. After turning the state machine on, the pedestrian can request crossing the street. The car traffic light will turn from green to red, let the pedestrian cross and turns back to normal mode once again.
Honestly, there are more details you need to be aware of, e.g. timing, but this is explained more detailed in my last example.
This is how a pedestrian traffic light could be implemented as a state machine:
To show you how you can use it I've just recorded a little screen cast. All you need is an installed Python3 and YAKINDU Statechart Tools (which at least requires Java 8). Everything else comes with the IDE.
Here you can download the software:
Free for non-commercial use.
Comments