Skip to content

Latest commit

 

History

History
63 lines (32 loc) · 5.43 KB

ProgrammingArduino.md

File metadata and controls

63 lines (32 loc) · 5.43 KB

Programming an Arduino with Statecharts

Modeling the Statechart

Now we are ready to model the statechart. We will use the "Hello World" example from the Arduino world, it's a blinking light. The Arduino Uno board has an LED built in that can be used for this purpose. In the "Blink" example, we will let the LED turn on and off every 500ms.

Model your statechart in the way that is shown in the figure below. Create two states, On and Off. Then create transitions between both of them and add time triggers to the transitions. The time trigger after 500ms lets the state automatically change after 500 milliseconds. Within the states, add an entry action that sets the boolean variable on to true or respectively to false. You need to declare the boolean variable on within the interface.

Statechart Modeling

You will find a more detailed documentation of the YAKINDU Statechart Tools and the statechart syntax in the Eclipse help or online.

Source code generation

Our statechart model for the "Blink" example is now complete. Open the context menu on the "Blink.sgen" file in the Project Explorer and select "Generate Code Artifacts".

Generate Code Artifacts

You might also open the SGen Multi Page Editor via the context menu on the "Blink.sgen" file in the Project Explorer. Choose "Open With" and select the Multi Page Editor. On the form page you can click on the gear-wheels in the upper right corner to generate the code artifacts.

SGen Multi Page Editor

Your Eclipse environment should now look like depicted in the figure below. There are two new folders, "src" and "src-gen". The "src-gen" folder contains the statemachine generated from the statechart, interfaces to the statemachine, a timer implementation, and everything else that is needed to execute the statemachine on your Arduino. The "src" folder is meant to be changed by the developer. We will go into detail later.

Project after Code Generation

You don't have to care about the code that is put into the "src-gen" folder. It is completely generated and will be overwritten the next time when the statechart changes.

Simulation

This is a pretty simple statechart, you can easily check by hand whether it works as you expect. Once the statechart gets more complex, it's better to check its behavior by simulation. Switch to the SC Simulation perspective and start the simulation of the statechart by clicking on the "Run" button in the toolbar as depicted below.

Start Simulation

Now the statechart will be simulated. The current state(s) of the statechart will be highlighted in red. On the right, variables and their values as well as events are shown in the Simulation view. You might even manipulate variables or send events manually in the Simulation view.

Running Simulation

For more details about simulation refer to the YAKINDU Statechart Tools pages in the Eclipse help.

Connecting the Statechart Model with the Hardware

Once you are sure that your statechart works as expected you still need to do some programming. In the "src-gen" folder there is a file called "ArduinoMain.cpp". This file contains the setup and loop functions that have to be implemented in each Arduino sketch. These functions are already implemented and contain everything that is needed to run the statemachine on your Arduino.

Unfortunately, you still need to program the code that connects the statemachine to the Arduino hardware. In the "src" folder you will find two other files, "BlinkConnector.h" and "BlinkConnector.cpp" (the prefixes may differ, the class is named after the statechart). In the "BlinkConnector.cpp" file you will find two methods, BlinkConnector::init() and BlinkConnector::runCycle(). They correspond to the setup and loop functions in ordinary Arduino sketches.

Put everything that is needed to initialize your hardware into the BlinkConnector::init() function and everything that is needed to be executed regularly into the BlinkConnector::runCycle() method. In this example, you need to turn the built-in LED on the Arduino Uno board to on or off depending on the statechart's state. First of all you need to initialize the LED_BUILTIN pin as an OUTPUT pin. In the runCycle loop you then need to update the pin's level depending on the statechart's on variable to switch the LED on or off.

Implementing Hardware Connector

The Arduino hardware is now connected to the statemachine running on your Arduino. It's time to upload the program.

Uploading the Program

Click on the "Launch in 'Run' Mode" button to upload the code to your Arduino. Be sure to connect your Arduino to your computer and select the correct launch target in the toolbar.

Upload the Program

If the upload was successfull the output should look like this:

Uploading Finished

Now it's your turn to model more complex statecharts and run them on your Arduino. Have fun!