C or C++ Code Generation and Integration in Stateflow

Hi All,

How can I integrate the Arduino C++ Library files trough custom code setting and invoke the functions from the State flow states?

My questions are mainly addressed to someone who has experience with integration of Arduino into the Matlab/Simulink environment. I think explaining the entire situation would take huge volume.

The fact:
The Arduino libraries partially are integrated into the Matlab/Simulink Arduino Support Package.
Only using of digital I/Os and analog inputs.
If you use only the premade elements you can make model in Simulink, simulate, build and download to Arduino directly similar to Arduino IDE .
So principally the code making and building works well in that tool chain too.

My aim is to expand the possibilities of that feature.

I found that the custom code integration trough the Simulink/Stateflow seems a convenient way:
The procedure called C / C++ Code Generation and Integration in State flow.
There are examples in State flow help with similar names. Here are the links.


As I said it works well with premade elements, but as I attempted to extended the model with invoking Arduino function from StateFlow chart I’ve got serious obstacles always.

There are two main direction of the attempts.

  1. Making wrapper function and using C coding.
  2. Using C++ codes directly

The failures have two main streams too ( loox to be independent to compiler settings).

  1. The build process mixed the C and C++ expressions but called the C compiler. Obvious, it is unavoidable at final compiling of the executable. The process stops with tons of unresolved symbol errors
  2. The build process mixed the AVR includes with the Visual Studio default includes and stops with tons of "duplicate definition" error

Finally I couldn’t get a status to have usable C source for building downloadable executable.

Every hint would help and welcomed.

If there would be an ultimate and mystic definition what solve it
or something like that.

In exchange I could worked out a very simple method to integrate all the Arduino functions into Simulink for the Arduino world. If Gods allow it.

Thanks in advance!

The environment:
Arduino MEGA 2560
Matlab R215a With support package “Run models on ArduinoUno, Mega 2560,… ” Ver: 15.1.1
Visual Studio 2013 and/or SDK 7.1


Ok, here is a fine example of errors I've got to ilustrate what is my problem
I made a "C" wrapper for some arduino Liquid cristal functions and simle put a call into a StateFlow chart. The chart parsing gave no errors.

I got those under C source generation. Those are repeating for all declaration line in pgmspace.h file.
I think the code generation process lost or mixed the selection of AVR and Arduino toolchains and includes wrong files or in wrong order??? I have no clue how could I correct it.

d:_Store\dokumentumok\MATLAB\Arduino\Try_06\avr/pgmspace.h(211) : error C2146: syntax error : missing ';' before identifier 'attribute'
d:_Store\dokumentumok\MATLAB\Arduino\Try_06\avr/pgmspace.h(211) : error C2065: 'progmem' : undeclared identifier
d:_Store\dokumentumok\MATLAB\Arduino\Try_06\avr/pgmspace.h(211) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
d:_Store\dokumentumok\MATLAB\Arduino\Try_06\avr/pgmspace.h(212) : error C2146: syntax error : missing ';' before identifier 'attribute'
d:_Store\dokumentumok\MATLAB\Arduino\Try_06\avr/pgmspace.h(212) : error C2065: 'progmem' : undeclared identifier
d:_Store\dokumentumok\MATLAB\Arduino\Try_06\avr/pgmspace.h(212) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
d:_Store\dokumentumok\MATLAB\Arduino\Try_06\avr/pgmspace.h(212) : error C2374: 'attribute' : redefinition; multiple initialization
d:_Store\dokumentumok\MATLAB\Arduino\Try_06\avr/pgmspace.h(211) : see declaration of 'attribute'
d:_Store\dokumentumok\MATLAB\Arduino\Try_06\avr/pgmspace.h(213) : error C2146: syntax error : missing ';' before identifier 'attribute'
d:_Store\dokumentumok\MATLAB\Arduino\Try_06\avr/pgmspace.h(213) : error C2065: 'progmem' : undeclared identifier
d:_Store\dokumentumok\MATLAB\Arduino\Try_06\avr/pgmspace.h(213) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int

Perhaps I am not the only reader that does not understand ?


What compiler/toolchain are you using? The error messages do not look like GCC AVR. You need to target compile for the AVR or SAM/ARM toolchain.



I really appreciate your feedbacks.
I instantly reformed my first post to be more precise.

Additional info:
The tool chain is defined by the Matlab/Simulink

I’m using:
Matlab R215a With support package "Run models on ArduinoUno, Mega 2560,… " Ver: 15.1.1
The Support Package provided by Mathworks includes the
Ardino 1.0.5 IDE
AVR headers.
Honestly I have not mapped all of the elements of the package yet.

However the Matlab has own compiler “LCC” but it won’t work fully without Visual Studio 2013 C/C++ or SDK 7.1 C/C++ (limited compilers are supported regarding to Matlab version ) for generating source code and building executable.
So as far as I know the GNU C/C++ can’t be used.

Another problem I’ve found not too much literature for this topic.

Sorry if my intention is not clear yet. Please ask more or suggest in private how I should make this topic more precise.

Any support will be welcomed.


I instantly reformed my first post to be more precise.

Don't do that again, please.

In this case it did not matter because there were no replies. But usually it screws up the ability for people to follow the discussion. Put new stuff in later posts to maintain the chronological logic.



This question seems more appropriate for the Matlab/Simulink forum. Or the product support organization.

I had a quick look at the documentation and it seems like you are mixing "apples and pears". Please remember that you need to compile/build for the target and not the host. If you check the Arduino models and how they are used I think might get a hint on how this works.


PS: Here is an obvious thing to check Arduino Run on Target Hardware + Stateflow -


Thanks but I dont think so.
The topic you linked is about a license handling bug in R2012a relase.


I made the second succes.
I managed to integrate Arduino function calls into the Stateflow chart and built and downloaded working executable to the hardware.

Previously I succesfully integrated function calls trough the "Code generation /Coustom code" pane of the simulink.

The root cause of my problem the multi level code generation and the different toolchains what the Simulink is using on the levels.

As is learnt there is atleas four session of code making in the Simulink.

session product tool

  1. s-function building. stateflow chart -->> s-function (Probably Simulink coder)
  2. source code generation for simulation s-functions --> Simulink code (Probably VISUAL STUDIO C++)
  3. Source code generation for the target Simulink code --> C source for Arduino (Probably GCC C)
  4. Building executable for target C source for Arduino --> elf (surely GCC)

Please correct me if my list is wrong.

My previous attempts stucked at level 1 and 2

I've had a "dirty" idea and I made three files: wrapper.h; wrapper.cpp; dummy_wrapper.cpp
dummy_wrapper.cpp includes only empty functions in same name as in wrapper.cpp
I included the dummy for 1. and 2. session in "Simulation Target /Custom code" pane.
And included the real wrapper and the library file (actually LiquidCrystal.cpp ) in Code generation/Custom code pane for the 3. and 4.

The quick and dirty way always works :slight_smile:

Now I have working executable. And I can manipulate the calls freely in my model. I'm really proffesional with Simulink. I can create 5000 lines usefull working code from the zero under half a day. (Place of self advert)

The project is far from the perfection yet because, obviously, the simulation runs with empty functions.
For me its no matter yet because at the beginning I'd like to use Display and Ethernet as monitoring outputs in real runnings.
But I'm going to continue. I'll be back.

Pm for deatils!

I found this interesting post on YouTube that shows how to integrate Arduino libraries in Simulink, maybe this might be of help.