Skip to content

Traffic Light (Python)

Demonstrates how to to generate Python source code from a statechart.


How it works

In summary, there are two traffic lights: one for the traffic on the street, and one for pedestrians who want to cross the street. They can push a button to request their traffic light to turn green, which activates a blinking wait-light first. The street's traffic light then goes through the standard procedure, turning on the yellow light, followed by the red light, allowing the pedestrians to cross safely. After some time, the pedestrians' traffic light turns red again, and the cars are allowed to pass until the next request is made by a pedestrian.

Additionally, the traffic light can be switched off completely. This turns the yellow lights into a blinking mode, signaling that the traffic light is indeed switched off and that the street should be crossed in a careful manner.

Run the program

The traffic light example is based on Python and contains a small GUI that demonstrate the state machine behaviour modelled in /model/TrafficLightCtrl.sct. This example combines generated Python code with manual Python implementations (the GUI). To generate the state machine code, right-click on /model/TrafficLightCtrl.sgen and select 'Generate Code Artifacts'. To start the application run src/ For this, double-click on '', start it from the console or - if you already added the PyDev plugin to your IDE - just right-click on the project and select 'Run As > Python Run'.


Using the GUI:

  1. Press the 'OnOff' Button to activate the traffic light system.
  2. Press the 'Go' button to send a request.
  3. At the end press 'Exit' to exit the program. This makes sure that everything shuts down safely.

Known issues

  1. The example requires Python 3
  2. It's recommended to install the PyDev plugin (or something similar) in your IDE first (but not necessary).
  3. The example shows you a 'src' folder, because it follows the tradition of all the other examples. Please use the auto-generated 'bin' folder for your own projects if you like to write a little script showing the use of your generated state machine (this refers to Python packaging).
Download examples

Drag to install

Drag to your running itemis CREATE (min. version 2.8.0) workspace.

Back to overview