Edit on GitHub

Traffic Light (C++) for Arduino

Based on the standard traffic light example, this project shows how to use a statechart to develop software for the Arduino platform.

The statechart

More information about the statechart itself can be found in the standard traffic light example, which should be considered first. In summary, we have 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.

Running the example on the Hardware

This video shows how the state machine runs on the example:

Building the Hardware

The circuit needed for the project

The correct pins can be found in the arduinoPins.h file.

Get your parts ready! You will need:

  • 2 push buttons
  • 6 LEDs, ideally:
    • 2x red
    • 2x green
    • 1x yellow
    • 1x white
  • 8 resistors:
    • 2x 10kR or similar
    • 6x 220R or similar

Of course, you can change the pins, but since this represents the pin numbers in the software, don't forget to change them as well.

Addons for compiling and flashing

For compiling and flashing the Eclipse C++ IDE for Arduino plugin has been used. This can be found at 'https://marketplace.eclipse.org/content/eclipse-c-ide-arduino'. Please ensure that you have installed the Arduino toolchain. This can easily be done by installing the Arduino IDE, which can be found here: 'https://www.arduino.cc/en/Main/Software'. It is not possible to compile code with the Eclipse C++ IDE for Arduino plugin without an installed toolchain!

How to install the Eclipse C++ IDE for Arduino plugin

The easiest way is to use the example wizard. Required dependencies will be recoginized automatically and can be installed by clicking the "Install Dependencies.." button on the top right.

After the installation of the plugin: Ensure that you have downloaded the 'Arduino AVR Boards'. You need to add them via 'Help' -> 'Arduino Downloads Manager' -> 'Platforms' -> 'Add'

Using the Eclipse C++ IDE for Arduino plugin

After the installation you should notice a new toolbar, which allows you to compile and flash your Arduino.
  1. First of all, your project should contain no errors any more.
  2. To get started you need to configure the desired platform. Choose your personal settings and board. Most of the Arduinos (Uno, Mega, etc.) should be supported. Plug in your Arduino and click on 'New Launch Target..'.
  3. Set up your device. This depends on your system and configuration.
  4. Build project.
  5. Upload the example.
  6. The trafficlight should start blinking now!
Download examples

Drag to install

Drag to your running YAKINDU Statechart Tools (min. version 2.8.0) workspace.

Back to overview