Python - Firmata 和 Arduino - 运行脚本的问题

问题描述 投票:0回答:1

我使用nodemcu(esp8266)和伺服电机(SG90),我尝试用python编写简单的代码,我确认我可以运行伺服电机。当我运行脚本时,我得到的只是等待,而且永远不会结束。我在 arduino 中运行标准格式脚本并将代码上传到 MCU。有人有什么建议吗?这里可能有什么问题。谢谢。

import pyfirmata
import time

# Specify the port where your Arduino is connected.
# For Windows, it may be something like "COM3." For macOS or Linux, it may be "/dev/ttyACM0" or "/dev/ttyUSB0."
port = 'COM3'  # Update with your specific port

# Create a new board object and connect to the Arduino.
board = pyfirmata.Arduino(port)

# Define the pin to which the servo is connected (e.g., digital pin 9).
servo_pin = board.get_pin('d:9:s')  # Pin 9 as a servo

try:
    while True:
        # Sweep the servo from 0 to 180 degrees
        for angle in range(0, 181, 1):
            servo_pin.write(angle)
            time.sleep(0.01)  # Adjust the delay for smoother motion

        # Sweep the servo from 180 to 0 degrees
        for angle in range(180, -1, -1):
            servo_pin.write(angle)
            time.sleep(0.01)  # Adjust the delay for smoother motion

except KeyboardInterrupt:
    board.exit()  # Cleanly exit when Ctrl+C is pressed

亲切的问候

python arduino nodemcu arduino-esp32 firmata
1个回答
0
投票

您运行的不是“标准格式脚本”,而是StandardFirmata sketch。检查草图中的线:

Firmata.begin(57600);

然后在Python模块中使用:

board = pyfirmata.Arduino(port, baudrate=57600)
print(board)
© www.soinside.com 2019 - 2024. All rights reserved.