• MATRIX Mini R4 Blocks API

    This document provides a complete API reference for MATRIX Mini R4 robot Blockly visual programming blocks.


    MATRIX Mini Basics

    Initialization Setup

    mini_setup

    Function Description: Initialize basic settings for the MATRIX Mini development board. This block should be placed at the beginning of the program. This is a HAT block. All main code except events and custom functions should be placed here, the concept is the same as Arduino's Setup and Loop.

    Note: Do not place multiple instances of this block in the program canvas, as this will cause abnormal functionality.

    Usage:


    Button Read

    mini_BTNget

    Function Description: Read the state of the onboard button.

    Return Value:


    Power Voltage Read

    mini_PWR_getVolt

    Function Description: Read the power input voltage value (unit: volts).

    Return Value:


    Digital Input Read

    mini_DIget

    Function Description: Read the input state of the specified digital pin.

    Parameters:

    Return Value:


    Digital Output Set

    mini_DOset

    Function Description: Set the output state of the specified digital pin.

    Parameters:


    Analog Input Read

    mini_AIget

    Function Description: Read the value of the specified analog pin.

    Parameters:

    Return Value:


    DC Motor Direction Set

    mini_MsetDIR

    Function Description: Set the rotation direction of the DC motor.

    Parameters:


    Motor PPR and RPM Settings

    mini_ddc_set_ppr_rpm

    Function Description: Set the encoder motor's pulses per revolution (PPR) and rated speed (RPM) parameters.

    Parameters:

    Description:


    Motor Power Control

    mini_Mset

    Function Description: Set motor power (PWM) and start operation.

    Parameters:


    Motor Speed Control

    mini_MsetSpeed

    Function Description: Set motor speed and start operation, but calculates speed in real-time through Encoder and uses PID for stable speed control.

    Parameters:


    Motor Brake

    mini_Mbrake

    Function Description: Stop the motor and apply brake.

    Parameters:


    Encoder Reset

    mini_ENC_reset

    Function Description: Reset encoder count value to zero.

    Parameters:


    Encoder Read

    mini_ENC_get

    Function Description: Read the current count value of the encoder.

    Parameters:

    Return Value:


    RC Servo Motor Direction Set

    mini_RCsetDIR

    Function Description: Set the rotation direction of the RC servo motor.

    Parameters:


    RC Servo Motor Angle Set

    mini_RCset

    Function Description: Set the angle position of the RC servo motor.

    Non-blocking function, the program will immediately continue execution, add delay manually if necessary

    Parameters:


    Movement Control

    This category provides high-level control blocks for robot movement, including synchronized control, gyroscope assistance and other functions.

    Chassis Configuration Settings

    mini_ddc_setting

    Function Description:

    If using non-MATRIX motors, please configure PPR, RPM, direction and other information through motor blocks before use.

    Configure left and right motors and their reverse settings for differential drive robots.

    Parameters:


    PID Control Parameter Settings

    mini_ddc_set_pid

    Function Description: Set PID controller parameters for movement synchronization or gyroscope control.

    Parameters:

    Adjustment Suggestions:

    Default PID Values (Kp, Ki, Kd) Reference:


    Move for Specified Distance/Angle

    mini_ddc_runFor

    Function Description: Control the robot to move a specified distance or angle.

    Parameters:


    Synchronized Move for Specified Distance/Angle

    mini_ddc_runFor_sync

    Function Description: Use PID synchronization control to keep left and right wheels moving consistently.

    Parameters:

    Features:


    Gyroscope-Assisted Movement

    mini_ddc_runFor_gyro

    Function Description: Use gyroscope sensor assistance to maintain straight-line movement.

    Parameters:

    Features:


    Continuous Movement

    mini_ddc_on

    Function Description: Start continuous movement until stop command. *Power is PWM output, no PID speed control, suitable for use with user's own line-following or other algorithms (such as PID)

    Parameters:


    Continuous Synchronized Movement

    mini_ddc_on_sync

    Function Description: Continuous movement with PID synchronization control.

    Parameters:


    Continuous Gyroscope Straight Movement

    mini_ddc_on_gyro

    Function Description: Continuous straight movement with gyroscope assistance.

    Parameters:


    Gyroscope Turn (Single Wheel)

    mini_ddc_turn

    Function Description: Control the robot to turn to a specified angle, can set turning speed. Uses gyroscope Yaw Heading for turning angle detection.

    Parameters:

    Features:


    Gyroscope Turn (Two Wheel)

    mini_ddc_turntwo

    Function Description: Control the robot to turn to a specified angle, can set turning speed. Uses gyroscope Yaw Heading for turning angle detection.

    Parameters:


    Chassis Movement Accumulated Angle

    mini_ddc_get_degs

    Function Description: Read the accumulated rotation angle value of the motor since last reset, can be used to calculate travel distance. Its calculation method is (abs(leftMotorDegs) + abs(rightMotorDegs)) / 2. Note: Does not conflict with original motor Encoder angle calculation.

    Return Value:


    Reset Chassis Accumulated Angle

    mini_ddc_reset_degs

    Function Description: Reset the chassis accumulated angle to zero, used to restart distance calculation. It is recommended to perform reset before precise distance measurement is needed. Note: Does not conflict with original motor Encoder angle calculation.


    Stop Movement

    mini_ddc_off

    Function Description: Stop robot movement. Brake method: Brake accelerates stopping through motor H-bridge driver chip, Coast releases throttle for coasting, Hold uses PID brake to ensure angle stability (not yet released)


    Appearance and Display

    RGB LED Setting

    mini_setRGB

    Function Description: Set the color of the onboard RGB LED.

    Parameters:


    Buzzer Play Frequency

    mini_Buzzer_Tone

    Function Description: Play a tone at a specified frequency and volume.

    Non-blocking function, the program will immediately continue execution, add delay manually if necessary

    Parameters:


    Buzzer Play Note

    mini_Buzzer_ToneNote

    Function Description: Play a predefined musical note.

    Non-blocking function, the program will immediately continue execution, add delay manually if necessary

    Parameters:


    Buzzer Stop

    mini_Buzzer_NoTone

    Function Description: Stop buzzer playback.


    OLED Clear Screen

    mini_OLED_clear

    Function Description: Clear all content from the OLED display.


    OLED Display Text

    mini_OLED_printEASY

    Function Description: Display text at a specified position on the OLED screen.

    Parameters:


    OLED Set Text Size

    mini_OLED_setTextSize

    Function Description: Set the text size for OLED display.

    Parameters:


    OLED Set Text Color

    mini_OLED_setTextColor

    Function Description: Set the text color for OLED display.

    Parameters:


    Serial Communication

    USB Serial Output (No Line Break)

    mini_Serial_print

    Function Description: Output text through USB serial port without automatic line break.

    Parameters:


    USB Serial Output (With Line Break)

    mini_Serial_println

    Function Description: Output text through USB serial port with automatic line break.

    Parameters:


    USB Serial Write Byte

    mini_Serial_write

    Function Description: Write raw byte data through USB serial port.

    Parameters:


    USB Serial Output Three-Axis Data

    mini_Serial_printAXIS

    Function Description: Output three-axis sensor data through USB serial port in a specific format.

    Parameters:

    Usage:


    USB Serial Check Data Available

    mini_Serial_available

    Function Description: Check if there is data available to read in the serial buffer.

    Return Value:


    USB Serial Read

    mini_Serial_read

    Function Description: Read one character from the serial port.

    Return Value:


    Sensors

    MJ2 Controller Polling

    mini_ps2_polling

    Function Description: Update MJ2(PS2) controller state data. Should be placed in the main loop for regular execution.

    MJ2 Adapter Connection


    MJ2 Button State

    mini_ps2_btn

    Function Description: Read MJ2(PS2) controller button state.

    How to connect to MJ2? Please scroll up to the MJ2 Polling section.

    Parameters:

    Return Value:


    MJ2 Joystick Value

    mini_ps2_joy

    Function Description: Read MJ2(PS2) controller joystick analog value.

    How to connect to MJ2? Please scroll up to the MJ2 Polling section.

    Parameters:

    Return Value:


    IMU Reset

    mini_motion_reset

    Function Description: Reset the onboard IMU (Inertial Measurement Unit) values.


    IMU Acceleration Read

    mini_motion_getAccel

    Function Description: Read acceleration sensor value.

    Parameters:

    Return Value:


    IMU Gyroscope Read

    mini_motion_getGyro

    Function Description: Read gyroscope angular velocity.

    Parameters:

    Return Value:


    IMU Euler Angle Read

    mini_motion_getEuler

    Function Description: Read attitude angles (Roll, Pitch, Yaw).

    Parameters:

    Return Value:


    Grayscale Sensor Read

    mini_MXGrayscale_getGrayscale

    Function Description: Read MATRIX grayscale sensor value.

    Parameters:

    Return Value:


    Potentiometer Read

    mini_MXPot_getPot

    Function Description: Read MATRIX potentiometer (knob) value.

    Parameters:

    Return Value:


    Water Level Sensor Read

    mini_MXWaterLevel_getLevel

    Function Description: Read MATRIX water level sensor value.

    Parameters:

    Return Value:


    Soil Moisture Sensor Read

    mini_MXSoilMoisture_getMoisture

    Function Description: Read MATRIX soil moisture sensor value.

    Parameters:

    Return Value:


    Miniature Switch State

    mini_MXMiniatureSwitch_getState

    Function Description: Read MATRIX miniature switch state.

    Parameters:

    Return Value:


    PIR Human Infrared Sensor

    mini_MXPIR_getState

    Function Description: Read MATRIX PIR human infrared sensor state.

    Parameters:

    Return Value:


    DHT Temperature & Humidity Sensor Polling

    mini_MXDHT_Polling

    Function Description: Update DHT temperature & humidity sensor readings.

    Parameters:


    DHT Temperature & Humidity Read

    mini_MXDHT

    Function Description: Read temperature or humidity from DHT sensor.

    Parameters:

    Return Value:


    DS18B20 Temperature Sensor

    mini_MXOnewireDT

    Function Description: Read DS18B20 temperature sensor value.

    Parameters:

    Return Value:


    MATRIX Color Sensor Initialize (V1, V2)

    mini_i2c_MXcolor_begin

    Function Description: Initialize MATRIX color sensor V2 version.

    Parameters:

    Note: V1, V2 and V3 hardware are incompatible, please pay attention to the hardware version and program used


    MATRIX Color Sensor Read (V1, V2)

    mini_i2c_MXcolor_getColor

    Function Description: Read MATRIX color sensor V2 color recognition result.

    Parameters:

    Return Value:

    Color ID Correspondence Table:

    0_ColorID


    MATRIX Color Sensor Initialize (V3)

    mini_i2c_MXcolorV3_begin

    Function Description: Initialize MATRIX color sensor V3 version.

    Parameters:

    Note: V1, V2 and V3 hardware are incompatible, please pay attention to the hardware version and program used


    MATRIX Color Sensor Read (V3)

    mini_i2c_MXcolorV3_getColor

    Function Description: Read MATRIX color sensor V3 RGB or HSV values.

    Parameters:

    Return Value:

    Color ID Correspondence Table:

    0_ColorID


    MATRIX Laser Distance Sensor Initialize (V1)

    mini_i2c_MXlaser_begin

    Function Description: Initialize MATRIX laser distance sensor V1 version.

    Parameters:

    Note: V1 and V2 hardware are incompatible, please pay attention to the hardware version and program used


    MATRIX Laser Distance Sensor Read (V1)

    mini_i2c_MXlaser_getDistance

    Function Description: Read MATRIX laser distance sensor V1 distance.

    Parameters:

    Return Value:


    MATRIX Laser Distance Sensor Initialize (V2)

    mini_i2c_MXLaserV2_begin

    Function Description: Initialize MATRIX laser distance sensor V2 version.

    Parameters:

    Note: V1 and V2 hardware are incompatible, please pay attention to the hardware version and program used


    MATRIX Laser Distance Sensor Read (V2)

    mini_i2c_MXLaserV2_getDistance

    Function Description: Read MATRIX laser distance sensor V2 distance.

    Parameters:

    Return Value:


    MATRIX Gesture Sensor Initialize

    mini_i2c_MXGesture_begin

    Function Description: Initialize MATRIX gesture sensor.

    Parameters:


    MATRIX Gesture Sensor Read

    mini_i2c_MXGesture_getGesture

    Function Description: Read MATRIX gesture sensor recognized gesture.

    Parameters:

    Return Value:


    MATRIX Gesture Sensor Determine

    mini_i2c_MXGesture_getGesture_equals

    Function Description: Determine if the gesture sensor detected a specific gesture.

    Parameters:

    Return Value:


    MVision Vision Sensor Initialize

    mini_mvision_begin

    Function Description: Initialize MVision vision sensor (via UART).


    MVision Read Data

    mini_mvision_read

    Function Description: Read MVision vision sensor recognition result.


    MVision Get Data

    mini_mvision_getdata

    Function Description: Get specific data field from MVision vision sensor.

    Parameters:

    Return Value:


    HuskyLens Initialize

    mini_huskylens_begin

    Function Description: Initialize HuskyLens AI vision sensor (via UART, Baud Rate 9600).

    Note: Do not directly use the original JST to JST cable to connect HuskyLens Camera, as pin definitions differ and jumper wires are needed!

    Note: UART Pin define on MATRIX R4 is (TX, RX, V, G)


    HuskyLens Read

    mini_huskylens_read

    Function Description: Read HuskyLens recognition result.


    HuskyLens Get Block

    mini_huskylens_getblock

    Function Description: Get HuskyLens recognized block (object) information.

    Parameters:

    Return Value:


    HuskyLens Get Arrow

    mini_huskylens_getarrow

    Function Description: Get HuskyLens recognized arrow (line) information.

    Parameters:

    Return Value:


    HuskyLens Detection Determine

    mini_huskylens_isdetect

    Function Description: Determine if HuskyLens detected an object.

    Return Value:


    MATRIX Line Tracer Sensor Initialize

    mini_i2c_mxlinetracer_begin

    Function Description: Initialize MATRIX 10-channel line tracer sensor.

    Parameters:


    MATRIX Line Tracer Sensor Calibration

    mini_i2c_mxlinetracer_calibration

    Function Description: Control line tracer sensor calibration procedure.

    Parameters:

    Instructions:

    1. Call "Start calibration"

    2. Move sensor over white line and black line

    3. Call "Stop calibration" to complete calibration


    MATRIX Line Tracer Sensor Read

    mini_i2c_mxlinetracer_getsensor

    Function Description: Read sensor value of specified channel.

    Parameters:

    Return Value:


    MATRIX Line Tracer Sensor Get Value

    mini_i2c_mxlinetracer_get_number

    Function Description: Get line tracer sensor error value or line position.

    Parameters:

    Return Value:


    MATRIX Line Tracer Sensor Get Status

    mini_i2c_mxlinetracer_get_boolean

    Function Description: Query line tracer sensor status.

    Parameters:

    Return Value:


    MATRIX Line Tracer Sensor Set Weights

    mini_i2c_mxlinetracer_setweights

    Function Description: Set weight values for sensors S6-S10 for error calculation.

    Parameters:

    Description:


    MATRIX Line Tracer Sensor Set Threshold

    mini_i2c_mxlinetracer_setthreshold

    Function Description: Set black line detection threshold for the sensor.

    Parameters:

    Description:


    Third-Party Modules

    HiTechnic Color Sensor Initialize

    mini_i2c_HTcolor_begin

    Function Description: Initialize HiTechnic color sensor.

    Parameters:


    HiTechnic Color Sensor Read

    mini_i2c_HTcolor_get

    Function Description: Read HiTechnic color sensor values.

    Parameters:

    Return Value:


    DHT11 Temperature & Humidity Read

    mini_DHT11get

    Function Description: Read DHT11 temperature & humidity sensor value.

    Parameters:

    Return Value:


    DS18B20 Temperature Read

    mini_DS18B20_get

    Function Description: Read DS18B20 waterproof temperature sensor.

    Parameters:

    Return Value:


    Grove Ultrasonic Sensor

    mini_Grove_US_Get

    Function Description: Read Grove ultrasonic distance sensor distance.

    Parameters:

    Return Value:


    Ultrasonic Sensor

    mini_USget

    Function Description: Read standard HC-SR04 ultrasonic sensor distance.

    Parameters:

    Return Value:


    Timer Reset

    mini_timer_reset

    Function Description: Reset the timer, start counting from 0.


    Timer Read

    mini_timer_read

    Function Description: Read elapsed time on the timer.

    Return Value:


    IoT Internet of Things

    WiFi Connection

    mini_wifi_connect

    Function Description: Connect to WiFi network (2.4GHz only).

    Parameters:

    Description:


    WiFi Status

    mini_wifi_status

    Function Description: Check WiFi connection status.

    Return Value:


    WiFi Local IP

    mini_wifi_localip

    Function Description: Get device's local IP address.

    Return Value:


    WiFi Configure Static IP

    mini_wifi_config

    Function Description: Configure device to use fixed IP address.

    Parameters:

    Example:


    WiFi Create Hotspot

    mini_wifi_create_ap

    Function Description: Set device as WiFi hotspot (AP mode).

    Parameters:


    BLE Initialize

    mini_ble_begin

    Function Description: Initialize Bluetooth Low Energy (BLE) functionality.


    BLE Set Name

    mini_ble_set_name

    Function Description: Set BLE device broadcast name.

    Parameters:


    BLE Enable Nordic UART

    mini_ble_uart_enable

    Function Description: Enable Nordic UART Service for BLE serial communication, closer to previous HC-05 usage.


    BLE Update

    mini_ble_poll

    Function Description: Update BLE connection status and data. Should be placed in main loop for regular execution.


    BLE Connection Status

    mini_ble_connected

    Function Description: Check if BLE has device connected.

    Return Value:


    BLE UART Send

    mini_ble_uart_send

    Function Description: Send message via BLE UART.

    Parameters:


    BLE When Data Received

    mini_ble_on_received

    Function Description: Execute internal code when BLE receives data. This is a HAT block.

    Note: Do not place multiple instances of this block in the program canvas, as this will cause abnormal functionality.

    Usage:


    BLE Received Data

    mini_ble_received_data

    Function Description: Get BLE received data content.

    Return Value:


    MQTT Connection

    mini_mqtt_connect

    Function Description: Connect to MQTT Broker.

    Parameters:

    Common Free Test Brokers:

    Common Free Test Client Website:


    MQTT Loop

    mini_mqtt_loop

    Function Description: Maintain MQTT connection and process messages. Should be placed in the main loop for regular execution.


    MQTT Subscribe to Topic

    mini_mqtt_subscribe

    Function Description: Subscribe to an MQTT topic to receive messages.

    Parameters:

    Example:


    MQTT Publish Message

    mini_mqtt_publish

    Function Description: Publish a message to an MQTT topic.

    Parameters:


    MQTT When Message Received

    mini_mqtt_on_message

    Function Description: Execute internal code when MQTT receives a message from a subscribed topic. This is a HAT block.

    Note: Do not place multiple instances of this block in the program canvas, as this will cause abnormal functionality.

    Usage:


    MQTT Received Topic

    mini_mqtt_received_topic

    Function Description: Get the source topic of the MQTT message.

    Return Value:


    MQTT Received Message

    mini_mqtt_received_message

    Function Description: Get the content of the MQTT message.

    Return Value:


    MQTT Connection Status

    mini_mqtt_is_connected

    Function Description: Check if MQTT is connected.

    Return Value:


    Control Flow

    Wait

    control_wait

    Function Description: Pause program execution for a specified time.

    Parameters:


    If...Then

    control_if

    Function Description: Conditional statement, executes internal code when condition is true.

    Usage:


    If...Then...Else

    control_if_else

    Function Description: Conditional statement, executes different code based on condition.

    Usage:


    Repeat

    control_repeat

    Function Description: Repeat internal code a specified number of times.

    Parameters:

    Usage:


    Forever

    control_forever

    Function Description: Repeat internal code indefinitely until program stops.

    Usage:


    Wait Until

    control_wait_until

    Function Description: Pause program execution until condition becomes true.

    Usage:


    Repeat Until

    control_repeat_until

    Function Description: Repeat internal code until condition becomes true.

    Usage:


    Get Execution Time

    mini_millis

    Function Description: Get milliseconds elapsed since program started execution.

    Return Value:

    Uses:


    Operators

    Map Range

    mini_map

    Function Description: Map a value from one range to another range.

    Parameters:

    Example:


    Constrain Range

    operator_constrain

    Function Description: Constrain a value within a specified range.

    Due to the underlying design of the constrain() function, please avoid using data other than variables and constants within parentheses, as this may lead to incorrect results.

    Parameters:

    Example:


    Addition

    operator_add

    Function Description: Calculate the sum of two numbers.


    Subtraction

    operator_subtract

    Function Description: Calculate the difference of two numbers.


    Multiplication

    operator_multiply

    Function Description: Calculate the product of two numbers.


    Division

    operator_divide

    Function Description: Calculate the quotient of two numbers.


    Random Number

    operator_random

    Function Description: Generate a random integer within a specified range.

    Parameters:


    Set Random Seed

    mini_randomSeed

    Function Description: Set the seed value for the random number generator.

    Parameters:

    Uses:


    Less Than

    operator_lt

    Function Description: Determine if the first value is less than the second value.

    Return Value:


    Equals

    operator_equals

    Function Description: Determine if two values are equal. (Can also be used for string comparison)

    Return Value:


    Greater Than

    operator_gt

    Function Description: Determine if the first value is greater than the second value.

    Return Value:


    Boolean Constant

    operator_const_bool

    Function Description: Provide true or false boolean constants.


    AND

    operator_and

    Function Description: Logical AND operation, returns true when both conditions are true.

    Return Value:


    OR

    operator_or

    Function Description: Logical OR operation, returns true when at least one condition is true.

    Return Value:


    NOT

    operator_not

    Function Description: Logical NOT operation, inverts boolean value.

    Return Value:


    String Join

    operator_join

    Function Description: Concatenate two strings.

    Example:


    Three String Join

    operator_join_three

    Function Description: Concatenate three strings.


    Get Character

    operator_letter_of

    Function Description: Get the character at a specified position in a string.

    Parameters:

    Return Value:


    String Length

    operator_length

    Function Description: Get the length of a string.

    Return Value:


    Convert to String

    operator_toString

    Function Description: Convert a number to a string.


    Convert to Float

    operator_toFloat

    Function Description: Convert a string to a floating-point number.


    Modulo

    operator_mod

    Function Description: Calculate the remainder of division.

    Example:


    Round

    operator_round

    Function Description: Round a number to the nearest integer.


    Math Operation

    operator_mathop

    Function Description: Perform advanced mathematical operations.

    Options:


    Variables

    The variables category provides functionality for creating and managing variables.

    Create Variable

    Click the "Create Variable" button to create a new variable.

    Set Variable

    Store a value in a variable.

    Read Variable

    Get the value stored in a variable.

    Change Variable

    Increase the variable's value by a specified amount.


    Custom Blocks

    The custom blocks category allows you to create reusable program blocks.

    Create Custom Block

    1. Click the "Create Block" button

    2. Enter block name

    3. Choose whether to add parameters

    4. Define the block's program content

    Benefits

    Use Custom Block

    After creation, custom blocks will appear in this category and can be dragged and used like any other block.

    Custom Block Variable - Number

    A local numeric variable for use only within the custom block

    Custom Block Variable - String

    A local string variable for use only within the custom block

    Custom Block Variable - Boolean

    A local boolean variable for use only within the custom block


    Version Information: