Applying Simulation Mode

Learn about the blocks and ino code requirements for simulation mode.

Activate Arduino Block Library: Simulation Mode

The 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 Activate during simulation to communicate with the Arduino board.

Before running an 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 Activate model containing Arduino blocks can be used in simulation mode.

How Does Activate Communicate with the Generic Ino Code?

Each Arduino block is either a basic block, i.e., has a simulation function, or is a super block containing basic blocks. During model simulation the following occurs:
  1. 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.
  2. The generic Arduino code, running in the loop waiting for the message, receives data on its serial port.
  3. The received data is then decoded, and the corresponding operation performed in Arduino.
A simple example is used below to illustrate these steps. In this example the Arduino board is used to read a digital value.
  1. 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.
  2. The simulation function of the 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.
  3. The array for initialization contains the following ascii code: {2, 0, n, 2}. It is sent by Activate and received by Arduino.
  4. The Arduino code, receiving the code 2, knows that this message concerns a digital port. The following code 0 is for initialization, and n is the number of the pin coded as an ascii character. Finally, the last 2 indicates that the pin should be set as INPUT_PULLUP, so the Arduino code that is called in this case is pinMode(n,INPUT_PULLUP).
  5. Then, every time the Activate block is called, the simulation function sends the following code {2, 1, n} where 2 indicates digital, 1 indicates read and n is the pin number (set before as INPUT_PULLUP). The Arduino code called in then is uint8_t dgv=digitalRead(n);.
  6. The Arduino code sends the value of the digital pin to Activate through the serial port using the code: Serial.write(dgv);
  7. 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 Activate block.

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.

To generate embedded code for Arduino:
  1. Place all the blocks used in the definition of the embedded code in a super block.
  2. Make sure the super block does not have any inputs or outputs.
  3. Select the super block in the diagram.
  4. Call the function vssGenerateInoFile from the OML command window; the function returns the generated ino file path.
  5. Open the ino file in the Arduino IDE.
  6. Compile and upload the code to the Arduino board.
Note: The following are known limitations in the current release:
  • 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 Activate Arduino blocks.