Programming Arduino with Altair Embed
How is an Embed model executed?
When I first started working and experimenting with Altair Embed one of the things that confused me was trying to figure out was the way in which a graphical model is executed..
In Arduino programs, like other text-based programs, it is simple. Code executes from the top of your program towards the bottom, sequentially, one line of code at a time.
When there is a call to a function, the execution will branch out to that function. When the code in the function completes its execution, the execution continues from the next line after the call.
How does a model execute? How does the positioning of the blocks in an Embed model affect the order by which they execute?
This is the topic of this lecture.
Block position and order of execution
The Embed IDE gives you an area where you can place your code in the form of blocks. You can place these blocks anywhere in the model window.
What confuses people new to Embed, myself included, is how the positioning of a block affects its execution.
In a simple model like the one in Image 1, things are simple. Execution starts from the top, and continues downwards, just like in an Arduino program.
But then when you get into something a little bit more involved, like the model in Image 2, things are not as clear anymore. The model in Image 2 is that of the Elegoo car, an example that ships with Altair Embed for the Arduino. The arrow shows the contents of one of the compound blocks in the model (more about compound blocks in an upcoming article)
In such a model, does it matter if a block is a bit to the left, or a bit to the right? Is execution order affected if you move the block up or down?
This is what this article is about.
Order of execution in text-based programs (recap)
In Arduino programs, and virtually in any other text based programming language, code executes from the top of the program to the bottom.
Branching is possible, where the execution encounters calls to a function, or branching logic (like an "if" statement).
After the code in the branch (such as the called function, or the block of code in an "if" statement), is executed, the execution returns to the branched line and continue from there.
All this is very predictable.
Order of execution in Embed
Model/block execution in Embed is not materially different compared to text-based languages.
Look at the example model in Image 3. This example shows a model for an ultrasonic distance sensor program, thar runs on an Arduino Uno. The model drives an ultrasonic distance sensor that checks the distance between the sensor and an object in front of every half a second (you can consult a separate guide to learn the details of this model).
Just like you expect in any language, execution starts at the top of the model, and continues towards the bottom. The block that executes next, depends on which block is right above it, vertically.
In the example in Image 1, the first block that is executed is the Arduino configuration block since it is at the very top of the model.
It doesn't matter whether it's on the left top or right top or the middle top. All that matters is that vertically is at the top of the model and that everything else is below it.
Next, in the example is a box that contains an external definition. We use this External Definition block to import an external Arduino library and initialize the sensor object. And because that is the second vertical block in this model, it is the second thing to execute.
Then, there is the set up block. This block is called only once because it is connected to the $firstPass block. The $firstPass block emits "true" only when the program execution starts.
After that, the next block that is executed is the "Read Sensor Every Half Second".This is a compound block. This is the last block that appears vertically in this model, and I have configured its properties to be called every half a second.
In Image 4, you can see the properties window of the compound block in this example. The Local Time Step property is "0.5". The unit is "seconds". There is no need to use a blocking "delay" or equivalent block in Embed.
In case you are wondering, a compound block is a block that contains other blocks. You can learn about compound blocks in another lesson in this series.
As you learned, the path of execution of an Embed model is similar to the execution of a program in C or any other text-based language. Blocks that appears higher in the model are executed before blocks that appear lower, and it doesn't matter what the location of those blocks is horizontally.
What matters is what the location of the blocks is vertically.
Altair Embed series