Home » Programacion » Facilino » Facilino Tutorial » Control flow instructions in Facilino

Control flow instructions in Facilino

In this post, we explain instructions related to the control flow of a program in Facilino.

What is the control flow?

In microcontrollers, the program flow, that how instructions are executed is usually sequential. This means that instructions are executed in order “as they appear”. In Facilino, “sequentially” is achieved by stacking blocks one after another one. Fortunately, we can modify the flow of a program by including instructions that will modify which is the next instruction to execute based on some conditions.

Arduino has two main functions that must be included in every program to properly compile. The “setup” function allows you to include code that will be executed once at the beginning, while the “loop” function will allow you to execute code that will be endlessly repeated.

Flow control can be achieved with loops that will allow us to repeat a set of instructions until a condition is met or conditional jump instructions that will determine wether a code will be executed or not.

Within the flow control instructions, we can also find instructions related to delays that will simply wait until a given condition is met or a time has passed by before the next instruction is executed.

Setup/Loop

Every Facilino program will include this instruction. The blocks included in the “Setup” container will be executed just once, while the block instructions included in the “Loop” container will be executed endlessly.

Loops

Loops instructions in Facilino implement “while”, “for” and “do… while” instructions in Arduino. This type of instructions will allow you to execute a set of instructions (whatever we put inside the “do” container) until a condition is met. The difference between “while” and “do…while” is that instructions contained in a “while” block will be executed if the condition is met, while the instructions contained in a “do…while” block will be executed first and after that, the condition will be evaluated. A “for” block will iterate over a variable a given number of times to repeat a task.

 

Loops can be interrupted with a “break” instruction. Also, we might be interested in continuing with the loop by “jumping” the remainder of the code of the iteration:

 

Conditional jumps

Conditional jump instructions in Facilino allow you to execute a code “if” a given condition is met. There are two types of blocks “if” and “switch”. The “if” block evaluates a binary expression and if it is true, then executes the code inside the container. Additional branches can be created by adding “else if” and “else” blocks in the mutator. A “switch” block instruction evaluates a number and depending on its value executes one case or another.

Delays

We can create simple delays of time (in milliseconds) or wait until a condition is met:

We can also meassure the current time from start in microseconds or milliseconds:

Example 1

In this example we generate random numbers between 1 and 20. If the number is lower or equal than 15, then we show a message indicating that we must press a button switch (connected to pin D2) and generate another random number.

Example 2

In this example we generate ficticious temperatures between 5ºC and 40ºC randomly. Based on the temperature value, we show messages indicating that the temperatures is too high, too cold or simply perfect.

Example 3

In this example we show how to blink a LED 10 times.