Using Arduino with Simulink

Hi all, I'm experimenting the Arduino programming using Simulink.

I want to build custom blocks to entirely program my boards with Simulink.
So I'm trying to build an S-Function block and I've done this with success to create a blinking led (simple example to start with).

Now I have some question because I don't find anything useful with google.

  1. How can I write blocks to use only for Serial purposes (for example Serial.print(), Serial.available(), etc..)? I'm growing mad on how to do that so I'll appreciate some help!
  2. Is there a possibility to have serial monitor (or similar) on Simulink?

In my block I've done something like this.

This under Libraries-> Includes:

# ifndef MATLAB_MEX_FILE 
# include <Arduino.h>
# endif

This under Discrete Updates (I've 1 state that update itself from 0 to 1, this is for initialization purposes):

if (xD[0] != 1) {
    
    # ifndef MATLAB_MEX_FILE
    pinMode(pin[0], OUTPUT);
    # endif
    
    xD[0] = 1;
}

This under Outputs:

if (xD[0] == 1) {
    
    # ifndef MATLAB_MEX_FILE

        digitalWrite(pin[0],in[0]);

    # endif
    
}

I know how to insert code for the voidSetup() and for the voidLoop() using those formats but I don't know how to use Serial functions.
For specific functions like Servo functions I suppose I have to just include the Servo.h library under the Libraries->Includes tab.

So I need your help. Thank you.

PS Sorry for my English. :slight_smile:

I freely admit that I know nothing about Simulink but your examples look just as complicated as writing or using the functions directly on the Arduino. What is the advantage of using Simulink ?

UKHeliBob:
I freely admit that I know nothing about Simulink but your examples look just as complicated as writing or using the functions directly on the Arduino. What is the advantage of using Simulink ?

I've to build a more complicated project which involve integrals and derivatives and many more.

We are four engineering students and everyone has to build a part of the code, doing this in Simulink is a better and easy way to do this.
Using Matlab and Simulink is the best way to do complicated mathematical operations and Simulink is more intuitive to build big algorithms. :wink:

doing this in Simulink is a better and easy way to do this.

Obviously, it isn't, or you would not be having problems.

PaulS:

doing this in Simulink is a better and easy way to do this.

Obviously, it isn't, or you would not be having problems.

Ahahah right! :sweat_smile:

Well, If there is a better way let me know!!

Throw away the difficult to use crutch and write some functions, or even a library, for the Arduino. There are literally millions more people that can give help on writing C++ for the Arduino than the number able to provide help with using Simulink.

What is it exactly that you want the program to do ?

UKHeliBob:
Throw away the difficult to use crutch and write some functions, or even a library, for the Arduino. There are literally millions more people that can give help on writing C++ for the Arduino than the number able to provide help with using Simulink.

What is it exactly that you want the program to do ?

Well, I would be able to view a serial monitor trough Simulink as I can do with the Arduino IDE and I would be able to make blocks for every part of code I want to write.
Everyone of us has to write a part of the code, using simulink block makes easier to put code parts together!

We have to build an autonomous vehicle so we have to write a differential model, and many sensors interfaces. I can't be more precise at the moment because we are still thinking on how to write the code.

have u succeeded with doing it?

im trying to transform my code into simulink block, its fairly easier to use MATLAB capabilities with arduino

any help would be useful

check this link http://www.maklabacademy.com/start-with-arduino-and-simulink/