Applying Simulation Mode
Learn about the blocks and ino code requirements for simulation mode.
Twin Activate Arduino Block Library: Simulation Mode
The Twin Activate Arduino block library contains a set of blocks that can communicate with an Arduino board.
These blocks can be defined for use with the standard Arduino libraries, or they can be complex and require the use of external libraries (as noted earlier in the Required Extensions and Libraries section).
The Arduino library includes the folder _bin/Arduino which contains ino files. These files define a generic Arduino code that is used by Twin Activate during simulation to communicate with the Arduino board.
Before running a Twin Activate model containing Arduino blocks, a best practice is to ensure that the “sketch” provided in the library is uploaded to the board. To achieve this, open the arduino.ino file in the Arduino IDE, compile, and upload the result to the board.
Once this code is on the board, any Twin Activate model containing Arduino blocks can be used in simulation mode.
How Does Twin Activate Communicate with the Generic Ino Code?
- Every time the simulation function of a basic block is called, it sends an array of characters to the Arduino board through the serial port.
- The generic Arduino code, running in the loop waiting for the message, receives data on its serial port.
- The received data is then decoded, and the corresponding operation performed in Arduino.
- On the Arduino side, to read data on a digital port, the port should be defined as an input and the function digitalRead be used.
- The simulation function of the Twin Activate block, DigitalInput, sends two coded strings: one for initialization and one every time the block needs to read the value on the digital port.
- The array for initialization contains the following ascii code:
{2, 0, n, 2}
. It is sent by Twin Activate and received by Arduino. - The Arduino code, receiving the code
2
, knows that this message concerns a digital port. The following code0
is for initialization, andn
is the number of the pin coded as an ascii character. Finally, the last2
indicates that the pin should be set asINPUT_PULLUP
, so the Arduino code that is called in this case ispinMode(n,INPUT_PULLUP)
. - Then, every time the Twin Activate block is called, the simulation function sends
the following code
{2, 1, n}
where2
indicates digital, 1 indicates read andn
is the pin number (set before asINPUT_PULLUP
). The Arduino code called in then isuint8_t dgv=digitalRead(n);
. - The Arduino code sends the value of the digital pin to Twin Activate through the serial port using the code: Serial.write(dgv);
- The block simulation function waits for the value until it is available on the serial port; it then reads it and copies the value to the output of the Twin Activate block.
Twin Activate Arduino Block Library Code Generation
The code generation for models using blocks from the Arduino library is in a beta production stage and is not yet exposed as a menu.
- Place all the blocks used in the definition of the embedded code in a super block.
- Make sure the super block does not have any inputs or outputs.
- Select the super block in the diagram.
- Call the function
vssGenerateInoFile
from the OML command window; the function returns the generated ino file path. - Open the ino file in the Arduino IDE.
- Compile and upload the code to the Arduino board.
- If the model contains any of the keypad blocks (Keypad4x3 or Keypad4x4), these blocks should be inlined before generating code for the super block. The inlining operation is available through the context menu.
- The generated code contains the ino codes (even if not needed) corresponding to all Twin Activate Arduino blocks.