# Introducing Customized Blockly Blocks

## **Define Master Block**

This block enables the configuration of a USB gateway and its initialization with a user-selected baud rate. The baud rate can be conveniently chosen from a drop-down menu, ensuring flexibility and ease of use. The USB gateway serves as a critical communication bridge between the computer and the SMD Red system, facilitating seamless data exchange.

<figure><img src="/files/iitstVb3HhsKbXK20GXp" alt=""><figcaption></figcaption></figure>

## **Smd Red Block**

This block establishes a connection to an SMD Red device by utilizing a specified Red ID parameter. The user is required to provide a valid Red ID to ensure accurate identification and communication with the target device.

<figure><img src="/files/8M9B8inog2hCOAhPFflw" alt=""><figcaption></figcaption></figure>

Above is the block structure for connecting to the smd red device with id value 0 and baudrate value 115200.

## **Set CPR Block**

This block is used to configure the **CPR (Counts Per Revolution)** value for a motor identified by a specified **Red ID**. The user provides the desired CPR value, which determines the resolution of the motor's encoder, ensuring precise control and measurement.

<figure><img src="/files/Bb9zgAwPewMazMw7rsLw" alt=""><figcaption></figcaption></figure>

## **Set RPM Block**

This block is used to configure the **RPM (Revolutions Per Minute)** value for a motor identified by a specified **Red ID**. The user provides the desired RPM value, which controls the motor's rotational speed to meet application-specific requirements.

<figure><img src="/files/8YpoNLrLoe4MVvZfDDqU" alt=""><figcaption></figcaption></figure>

## **Define Motor Block**

This block is used to initialize and start a motor using its default parameters. To activate the motor, the user must provide the **Red ID**, which identifies the specific motor to be started.

<figure><img src="/files/N9mH3vRudb8VjKe5YbXl" alt=""><figcaption></figcaption></figure>

## Set Control Parameters Block

This block allows you to configure the PID parameters dynamically for the specific mode you choose and the Red ID you input. By specifying the appropriate `mode` (Velocity, Position, Torque) and a unique Red ID, you can assign tailored values for the proportional (Kp), integral (Ki), and derivative (Kd) gains. The functionality ensures flexibility and precision for different control scenarios.

<figure><img src="/files/a05V7OVPbaGiZ6PwVx5i" alt=""><figcaption></figcaption></figure>

## Enable Torque Bloc

This block is designed to initiate the torque of an motor dynamically using a specified Red ID. By entering the appropriate Red ID, the system ensures precise activation of the torque mechanism, tailored to the corresponding motor.

<figure><img src="/files/RrFZE29eNBLLAjWAzDC8" alt=""><figcaption></figcaption></figure>

## **Set Operation Mode Block**&#x20;

This block allows the user to set the operating mode for a motor identified by a specified **Red ID**. The user can select from one of the following modes: **PWM**, **Position**, **Speed**, or **Torque**, depending on the desired control strategy for the motor.

<figure><img src="/files/i4p8rbRC5gVHFncFxmMl" alt=""><figcaption></figcaption></figure>

## Pwm Mode Blocks

These two blocks perform the same function, with the distinction that one accepts only **numerical values**, while the other accepts a **joystick variable block** as input. This flexibility allows users to choose the appropriate input method based on their specific application requirements.

<figure><img src="/files/6nx15x1ODa5SzsPXMm5Y" alt=""><figcaption></figcaption></figure>

## Position Mode Blocks

These two blocks perform the same function, but with different methods of control: one rotates the motor directly to the specified position (in degrees), while the other uses a joystick input block to control the rotation.

<figure><img src="/files/zjVoTpvQp1MdGz8MmQ9g" alt=""><figcaption></figcaption></figure>

## Velocity Mode Blocks

These two blocks perform the same function, but with different methods of speed control: one rotates the motor at a constant speed based on a given value, while the other adjusts the speed dynamically using a joystick input.

<figure><img src="/files/Ts6mlN3hJDeIJp24Vj3w" alt=""><figcaption></figcaption></figure>

## Torque Mode Blocks

These two blocks perform the same function, but with different methods for controlling the motor's current: one rotates the motor based on a fixed **milliampere** value, while the other adjusts the milliampere value dynamically using a joystick input.

<figure><img src="/files/3MaIrRZWArfvePeJVz0G" alt=""><figcaption></figcaption></figure>

## Get [Joystick](/electronics/add-on-modules/joystick-module.md) Block

This block receives **x**, **y**, and **button** information from the [joystick module](/electronics/add-on-modules/joystick-module.md) and synchronizes it with the corresponding variable blocks placed on the values within the block. This allows real-time control and interaction based on joystick input.

<figure><img src="/files/bKaOzkrvVmlgteV8WmNN" alt=""><figcaption></figcaption></figure>

```python
Red_id = None
Module_id = None
X = None
Y = None
B = None


from smd.red import *
import math
import os

from serial.tools.list_ports import comports
from platform import system
def USB_Port():
    if system() == 'Windows':
        ports = list(comports())
        if ports:
            for port, desc, hwid in sorted(ports):
                if 'USB Serial Port' in desc:
                    SMD_Port = port
                    return SMD_Port

    elif system() == 'Linux':
        ports = list(serial.tools.list_ports.comports())
        if ports:
            for port, desc, hwid in sorted(ports):
                if '/dev/ttyUSB' in port:
                    SMD_Port = port
                    return SMD_Port

    elif system() == 'Darwin':  # macOS
        ports = list(serial.tools.list_ports.comports())
        if ports:
            for port, desc, hwid in sorted(ports):
                if '/dev/tty.usbserial' in port or '/dev/tty.usbmodem' in port:
                    SMD_Port = port
                    return SMD_Port

    else:
        SMD_Port = None
        return SMD_Port

port=USB_Port()
m = Master(port,baudrate=115200)
Red_id = 0
m.attach(Red(Red_id))

Module_id = 1
while True:
  X, Y, B = m.get_joystick(Red_id, Module_id)
```

## [RGB Led](/electronics/add-on-modules/rgb-led-module.md) Block

This block controls the [**RGB LED module**](/electronics/add-on-modules/rgb-led-module.md) by setting its color according to the **RGB values** entered. Users can specify the desired red, green, and blue values to customize the LED’s color output.

<figure><img src="/files/KCQSKy5xPB9kjYyH6vfB" alt=""><figcaption></figcaption></figure>

```python
Red_id = None
Module_id = None


from smd.red import *
import math
import os

from serial.tools.list_ports import comports
from platform import system
def USB_Port():
    if system() == 'Windows':
        ports = list(comports())
        if ports:
            for port, desc, hwid in sorted(ports):
                if 'USB Serial Port' in desc:
                    SMD_Port = port
                    return SMD_Port

    elif system() == 'Linux':
        ports = list(serial.tools.list_ports.comports())
        if ports:
            for port, desc, hwid in sorted(ports):
                if '/dev/ttyUSB' in port:
                    SMD_Port = port
                    return SMD_Port

    elif system() == 'Darwin':  # macOS
        ports = list(serial.tools.list_ports.comports())
        if ports:
            for port, desc, hwid in sorted(ports):
                if '/dev/tty.usbserial' in port or '/dev/tty.usbmodem' in port:
                    SMD_Port = port
                    return SMD_Port

    else:
        SMD_Port = None
        return SMD_Port

port=USB_Port()
m = Master(port,baudrate=115200)
Red_id = 0
m.attach(Red(Red_id))

Module_id = 1
m.set_rgb(Red_id, Module_id, 255, 0, 0)
```

## [Button ](/electronics/add-on-modules/button-module.md)Block

This block retrieves and returns the current **value** from the [**button module**](/electronics/add-on-modules/button-module.md), allowing the system to react based on the button's state (e.g., pressed or released).

<figure><img src="/files/pCWW9AzVVbAPfc9rsDDU" alt=""><figcaption></figcaption></figure>

```python
Red_id = None
Module_id = None
B = None


from smd.red import *
import math
import os

from serial.tools.list_ports import comports
from platform import system
def USB_Port():
    if system() == 'Windows':
        ports = list(comports())
        if ports:
            for port, desc, hwid in sorted(ports):
                if 'USB Serial Port' in desc:
                    SMD_Port = port
                    return SMD_Port

    elif system() == 'Linux':
        ports = list(serial.tools.list_ports.comports())
        if ports:
            for port, desc, hwid in sorted(ports):
                if '/dev/ttyUSB' in port:
                    SMD_Port = port
                    return SMD_Port

    elif system() == 'Darwin':  # macOS
        ports = list(serial.tools.list_ports.comports())
        if ports:
            for port, desc, hwid in sorted(ports):
                if '/dev/tty.usbserial' in port or '/dev/tty.usbmodem' in port:
                    SMD_Port = port
                    return SMD_Port

    else:
        SMD_Port = None
        return SMD_Port

port=USB_Port()
m = Master(port,baudrate=115200)
Red_id = 0
m.attach(Red(Red_id))

Module_id = 1
while True:
  B = m.get_button(Red_id, Module_id)
```

## [Ambient Light Sensor](/electronics/add-on-modules/ambient-light-sensor-module.md) Block

This block retrieves and returns the current **ambient light value**, allowing the system to adjust or react based on the surrounding light conditions.

<figure><img src="/files/NPbbvPousGYdrcXSSFFB" alt=""><figcaption></figcaption></figure>

```python
Red_id = None
Module_id = None
Light = None


from smd.red import *
import math
import os

from serial.tools.list_ports import comports
from platform import system
def USB_Port():
    if system() == 'Windows':
        ports = list(comports())
        if ports:
            for port, desc, hwid in sorted(ports):
                if 'USB Serial Port' in desc:
                    SMD_Port = port
                    return SMD_Port

    elif system() == 'Linux':
        ports = list(serial.tools.list_ports.comports())
        if ports:
            for port, desc, hwid in sorted(ports):
                if '/dev/ttyUSB' in port:
                    SMD_Port = port
                    return SMD_Port

    elif system() == 'Darwin':  # macOS
        ports = list(serial.tools.list_ports.comports())
        if ports:
            for port, desc, hwid in sorted(ports):
                if '/dev/tty.usbserial' in port or '/dev/tty.usbmodem' in port:
                    SMD_Port = port
                    return SMD_Port

    else:
        SMD_Port = None
        return SMD_Port

port=USB_Port()
m = Master(port,baudrate=115200)
Red_id = 0
m.attach(Red(Red_id))

Module_id = 1
while True:
  Light = m.get_light(Red_id,Module_id)
```

## [Ultrasonic Distance Sensor](/electronics/add-on-modules/ultrasonic-distance-sensor-module.md) Block

This block retrieves and returns the **distance value** measured by the [**ultrasonic distance sensor**](/electronics/add-on-modules/ultrasonic-distance-sensor-module.md), providing accurate readings of the proximity or distance to an object..

<figure><img src="/files/KoKZVv7vZTTM9iq8X3vO" alt=""><figcaption></figcaption></figure>

{% code lineNumbers="true" %}

```python
Red_id = None
Module_id = None
Distance = None


from smd.red import *
import math
import os

from serial.tools.list_ports import comports
from platform import system
def USB_Port():
    if system() == 'Windows':
        ports = list(comports())
        if ports:
            for port, desc, hwid in sorted(ports):
                if 'USB Serial Port' in desc:
                    SMD_Port = port
                    return SMD_Port

    elif system() == 'Linux':
        ports = list(serial.tools.list_ports.comports())
        if ports:
            for port, desc, hwid in sorted(ports):
                if '/dev/ttyUSB' in port:
                    SMD_Port = port
                    return SMD_Port

    elif system() == 'Darwin':  # macOS
        ports = list(serial.tools.list_ports.comports())
        if ports:
            for port, desc, hwid in sorted(ports):
                if '/dev/tty.usbserial' in port or '/dev/tty.usbmodem' in port:
                    SMD_Port = port
                    return SMD_Port

    else:
        SMD_Port = None
        return SMD_Port

port=USB_Port()
m = Master(port,baudrate=115200)
Red_id = 0
m.attach(Red(Red_id))

Module_id = 1
while True:
  Distance = m.get_distance(Red_id,Module_id)
```

{% endcode %}

## [Buzzer ](/electronics/add-on-modules/buzzer-module.md)Block

This block activates the [**buzzer module**](/electronics/add-on-modules/buzzer-module.md) and sets it to emit sound at the **frequency** value entered by the user.

<figure><img src="/files/b3nD7ujU1NIHr9IEOC1U" alt=""><figcaption></figcaption></figure>

```python
Red_id = None
Module_id = None


from smd.red import *
import math
import os

from serial.tools.list_ports import comports
from platform import system
def USB_Port():
    if system() == 'Windows':
        ports = list(comports())
        if ports:
            for port, desc, hwid in sorted(ports):
                if 'USB Serial Port' in desc:
                    SMD_Port = port
                    return SMD_Port

    elif system() == 'Linux':
        ports = list(serial.tools.list_ports.comports())
        if ports:
            for port, desc, hwid in sorted(ports):
                if '/dev/ttyUSB' in port:
                    SMD_Port = port
                    return SMD_Port

    elif system() == 'Darwin':  # macOS
        ports = list(serial.tools.list_ports.comports())
        if ports:
            for port, desc, hwid in sorted(ports):
                if '/dev/tty.usbserial' in port or '/dev/tty.usbmodem' in port:
                    SMD_Port = port
                    return SMD_Port

    else:
        SMD_Port = None
        return SMD_Port

port=USB_Port()
m = Master(port,baudrate=115200)
Red_id = 0
m.attach(Red(Red_id))

Module_id = 1
m.set_buzzer(Red_id,Module_id,500)
```

## [IMU ](/electronics/add-on-modules/imu-module.md)Block

This block receives and processes data from the [**IMU (Inertial Measurement Unit)**](/electronics/add-on-modules/imu-module.md), which typically includes information such as orientation.

<figure><img src="/files/huGXe5vuUBuGNeQnIg7f" alt=""><figcaption></figcaption></figure>

```python
Red_id = None
Module_id = None
Pitch = None
roll = None


from smd.red import *
import math
import os

from serial.tools.list_ports import comports
from platform import system
def USB_Port():
    if system() == 'Windows':
        ports = list(comports())
        if ports:
            for port, desc, hwid in sorted(ports):
                if 'USB Serial Port' in desc:
                    SMD_Port = port
                    return SMD_Port

    elif system() == 'Linux':
        ports = list(serial.tools.list_ports.comports())
        if ports:
            for port, desc, hwid in sorted(ports):
                if '/dev/ttyUSB' in port:
                    SMD_Port = port
                    return SMD_Port

    elif system() == 'Darwin':  # macOS
        ports = list(serial.tools.list_ports.comports())
        if ports:
            for port, desc, hwid in sorted(ports):
                if '/dev/tty.usbserial' in port or '/dev/tty.usbmodem' in port:
                    SMD_Port = port
                    return SMD_Port

    else:
        SMD_Port = None
        return SMD_Port

port=USB_Port()
m = Master(port,baudrate=115200)
Red_id = 0
m.attach(Red(Red_id))

Module_id = 1
roll, Pitch = m.get_imu(Red_id, Module_id)  
```

## Potentiometer Block

This block retrieves the **ADC (Analog-to-Digital Conversion)** value from the [**potentiometer modul**](/electronics/add-on-modules/potentiometer-module.md)**e**, providing a digital representation of the analog voltage output based on the potentiometer's position.

<figure><img src="/files/ZaRudbbY0qogyVNfbVKZ" alt=""><figcaption></figcaption></figure>

```python
Red_id = None
Module_id = None
pot = None


from smd.red import *
import math
import os

from serial.tools.list_ports import comports
from platform import system
def USB_Port():
    if system() == 'Windows':
        ports = list(comports())
        if ports:
            for port, desc, hwid in sorted(ports):
                if 'USB Serial Port' in desc:
                    SMD_Port = port
                    return SMD_Port

    elif system() == 'Linux':
        ports = list(serial.tools.list_ports.comports())
        if ports:
            for port, desc, hwid in sorted(ports):
                if '/dev/ttyUSB' in port:
                    SMD_Port = port
                    return SMD_Port

    elif system() == 'Darwin':  # macOS
        ports = list(serial.tools.list_ports.comports())
        if ports:
            for port, desc, hwid in sorted(ports):
                if '/dev/tty.usbserial' in port or '/dev/tty.usbmodem' in port:
                    SMD_Port = port
                    return SMD_Port

    else:
        SMD_Port = None
        return SMD_Port

port=USB_Port()
m = Master(port,baudrate=115200)
Red_id = 0
m.attach(Red(0))

Module_id = 1
pot = m.get_potentiometer(Red_id, Module_id)
```

## [Servo ](/electronics/add-on-modules/servo-module.md)Blocks

This block is used to set the position of a **servo motor** by specifying the desired angle. The motor adjusts its position to align with the given angle value.

<figure><img src="/files/Qqp5avKTiohfXTNwMuyH" alt=""><figcaption></figcaption></figure>

```python
Red_id = None
Module_id = None


from smd.red import *
import math
import os

from serial.tools.list_ports import comports
from platform import system
def USB_Port():
    if system() == 'Windows':
        ports = list(comports())
        if ports:
            for port, desc, hwid in sorted(ports):
                if 'USB Serial Port' in desc:
                    SMD_Port = port
                    return SMD_Port

    elif system() == 'Linux':
        ports = list(serial.tools.list_ports.comports())
        if ports:
            for port, desc, hwid in sorted(ports):
                if '/dev/ttyUSB' in port:
                    SMD_Port = port
                    return SMD_Port

    elif system() == 'Darwin':  # macOS
        ports = list(serial.tools.list_ports.comports())
        if ports:
            for port, desc, hwid in sorted(ports):
                if '/dev/tty.usbserial' in port or '/dev/tty.usbmodem' in port:
                    SMD_Port = port
                    return SMD_Port

    else:
        SMD_Port = None
        return SMD_Port

port=USB_Port()
m = Master(port,baudrate=115200)
Red_id = 0
m.attach(Red(0))

Module_id = 1
m.set_servo(Red_id, Module_id, 90)
```

## [QTR ](/electronics/add-on-modules/reflectance-sensor-module.md)Block

This block retrieves the sensor values from the [**QTR** module](/electronics/add-on-modules/reflectance-sensor-module.md), typically used for detecting changes in proximity, position, or surface reflection.

<figure><img src="/files/bprS0HjPJ2pIu2muvjsG" alt=""><figcaption></figcaption></figure>

```python
Red_id = None
Module_id = None
left = None
right = None
middle = None


from smd.red import *
import math
import os

from serial.tools.list_ports import comports
from platform import system
def USB_Port():
    if system() == 'Windows':
        ports = list(comports())
        if ports:
            for port, desc, hwid in sorted(ports):
                if 'USB Serial Port' in desc:
                    SMD_Port = port
                    return SMD_Port

    elif system() == 'Linux':
        ports = list(serial.tools.list_ports.comports())
        if ports:
            for port, desc, hwid in sorted(ports):
                if '/dev/ttyUSB' in port:
                    SMD_Port = port
                    return SMD_Port

    elif system() == 'Darwin':  # macOS
        ports = list(serial.tools.list_ports.comports())
        if ports:
            for port, desc, hwid in sorted(ports):
                if '/dev/tty.usbserial' in port or '/dev/tty.usbmodem' in port:
                    SMD_Port = port
                    return SMD_Port

    else:
        SMD_Port = None
        return SMD_Port

port=USB_Port()
m = Master(port,baudrate=115200)
Red_id = 0
m.attach(Red(0))

Module_id = 1
left, middle, right = m.get_qtr(Red_id, Module_id)
```

## Get Position Block

This block retrieves the current position of the motor identified by the specified **Red ID**. It allows the system to track and monitor the motor's position in real-time.

<figure><img src="/files/Oq3nmfVaaAFj4fN2Mpey" alt=""><figcaption></figcaption></figure>

```python
Red_id = None
position = None


from smd.red import *
import math
import os

from serial.tools.list_ports import comports
from platform import system
def USB_Port():
    if system() == 'Windows':
        ports = list(comports())
        if ports:
            for port, desc, hwid in sorted(ports):
                if 'USB Serial Port' in desc:
                    SMD_Port = port
                    return SMD_Port

    elif system() == 'Linux':
        ports = list(serial.tools.list_ports.comports())
        if ports:
            for port, desc, hwid in sorted(ports):
                if '/dev/ttyUSB' in port:
                    SMD_Port = port
                    return SMD_Port

    elif system() == 'Darwin':  # macOS
        ports = list(serial.tools.list_ports.comports())
        if ports:
            for port, desc, hwid in sorted(ports):
                if '/dev/tty.usbserial' in port or '/dev/tty.usbmodem' in port:
                    SMD_Port = port
                    return SMD_Port

    else:
        SMD_Port = None
        return SMD_Port

port=USB_Port()
m = Master(port,baudrate=115200)
Red_id = 0
m.attach(Red(Red_id))

position = m.get_position(Red_id)
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.acrome.net/software/smd-blockly/introducing-customized-blockly-blocks.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
