DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
Securing Your Software Supply Chain with JFrog and Azure
Register Today

Trending

  • 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

Trending

  • 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
  1. DZone
  2. Data Engineering
  3. AI/ML
  4. Program an Arduino With State Machines in 5 Minutes

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.

Lothar Wendehals user avatar by
Lothar Wendehals
·
Updated Oct. 23, 19 · Tutorial
Like (7)
Save
Tweet
Share
19.68K Views

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).

Image title

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.

SCTforArduino_Blink.png

The LED has two states, on and off. Therefore, I declare a boolean variable on representing on and off with its values true and false in the interface. Within the statechart, I create the two states On and 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 init() and runCycle() methods of the BlinkConnector class:

  • 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.
  • The 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 on.

Now, I compile the code and upload it to my Arduino board. There it is, a blinking LED in five minutes!

SCTforArduino_Blink_generatedCode.png

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.

SCTforArduino_PedestrianCrossing.png

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!

arduino Machine

Published at DZone with permission of Lothar Wendehals. See the original article here.

Opinions expressed by DZone contributors are their own.

Trending

  • 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

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com

Let's be friends: