Program an Arduino With State Machines in 5 Minutes
Programming Arduinos can be a pain, especially when dealing with complex flow controls. But YAKINDU's Statechart Tools can help. See an example here.
Join the DZone community and get the full member experience.Join For Free
Did you ever program an Arduino? Have you ever been worried about complex control flows written in pure C? Maybe you have already heard of statecharts and state machines? In this blog post, I will show you how to program an Arduino in just 5 minutes in a model-driven way with the help of YAKINDU Statechart Tools (SCT).
There have been several attempts to program an Arduino with YAKINDU SCT as described by Marco Scholtyssek or René Beckmann. However, when I tried to teach how to program an Arduino with YAKINDU SCT within the Automotive Software Engineering Summer School 2016 at the University of Applied Sciences and Arts in Dortmund I found out that it’s hard to understand and implement without appropriate tooling. So I sat down and implemented Arduino support for YAKINDU SCT to generate lots of the glue code that is needed to run state machines on the Arduino.
YAKINDU Statechart Tools for Arduino is based on Eclipse, YAKINDU Statechart Tools, and the Eclipse C/C++ Development Tooling (CDT). You can download Statechart Tools at that link. It sets up an initial project containing an empty statechart that you just need to fill with your own ideas. The only part that you still need to program on your own is the connection between the statechart and the hardware, i.e. initializing the hardware and updating the state of the hardware depending on the state of the statechart and vice-versa.
Now, let's have a look at the tooling. In the screenshot depicted below, you'll find the well-known Arduino "Hello World" example — a blinking LED — programmed as a statechart. I created an Arduino SCT project with the help of a wizard. It opened an empty statechart with an empty interface declaration. I will use this statechart to model a blinking LED and generate the state machine running on an Arduino Uno board.
The LED has two states, on and off. Therefore, I declare a boolean variable
on representing on and off with its values
false in the interface. Within the statechart, I create the two states
Off. Once deployed to the Arduino, the program's execution starts by entering the statechart through the black dot - the
Entry state - depicted in the statechart. After entering the statechart, it immediately changes its state through the first transition - it's the arrow from the
Entry state to the
On state. When entering the
On state, the boolean variable
on is set to
true. After 500 milliseconds, the state changes to
Off and the variable
on is set to
false. Again, after another 500 milliseconds, it switches back to
On. This goes on and on and on .... until you pull the plug.
Once I finish the modelling I might want to simulate my model with YAKINDU SCT to find out if it works as expected. You will find more details about modeling and simulation in the YAKINDU SCT documentation. Based on this statechart, I generate a state machine in C++ code that executes the statechart on my Arduino. All that I still need to do is connecting the statechart with the hardware. I do this by editing the
runCycle() methods of the
- In the
init()method I set up the LED built into the Arduino Uno board. This method is called once when starting the program's execution - analog to the
setup()function in a common Arduino sketch.
runCycle()method is the analogon for the
loop()function of an Arduino sketch. It is called regularly, once in every cycle of the statechart's execution. Here, I set the LED's pin accordingly to the statechart's boolean variable
Now, I compile the code and upload it to my Arduino board. There it is, a blinking LED in five minutes!
Ok, you’re right, this example may be implemented in plain C and uploaded to the Arduino in less than five minutes. It’s that simple. But could you imagine the effort of developing a state machine in plain C with the complexity of a pedestrian crossing light shown in the picture below? And even this example is still a simple one. By the way, you will find this example as well as the Blink example in the YAKINDU SCT for the Arduino environment.
On my GitHub page, you will find a complete walkthrough starting from installation, through setup, modeling, simulation, code generation, and code deployment. It is also integrated into the online help of YAKINDU Statechart Tools for Arduino.
To date, YAKINDU SCT for Arduino supports various popular microprocessors from the maker and IoT scene. Amongst others, these are the ATmega328 microprocessor used by the widely adopted and well known Arduino Uno boards, the ATmega2560 from the Arduino Mega2560, or the ESP8266. Most of the AVR microprocessors provide multiple hardware timers/counters. YAKINDU SCT for Arduino let's you choose one of those. Even if your microprocessor is not in the list of supported microprocessors, you can still choose the software timer that should run on every target.
More features are still to come, so stay tuned!
Published at DZone with permission of Lothar Wendehals. See the original article here.
Opinions expressed by DZone contributors are their own.
Getting Started With the YugabyteDB Managed REST API
Cucumber Selenium Tutorial: A Comprehensive Guide With Examples and Best Practices
SRE vs. DevOps
5 Key Concepts for MQTT Broker in Sparkplug Specification