[英]How can I use an Arduino Ultrasonic Sensor connected to an Arduino UNO to measure distance using Pyfirmata or Python Generally?
[英]How to use an Arduino joystick button with Pyfirmata
我在 Arduino 中制作了一個使用 PyFirmata 和操縱桿的程序。 除按鈕外,一切正常。 我的理解是,我知道我需要先寫一個電壓,但之后當我讀取該值時,無論如何它都保持在 1。 為了確保我的主板或操縱桿或其他東西沒有故障,我使用 c++ 默認 Arduino 語言運行它並且它工作。 請注意,我是 Arduino 的新手,所以請記住這一點。 這是我的代碼:
from pyfirmata import Arduino
from pyfirmata.util import Iterator
import time
# Setup
try:
board = Arduino('COM6')
iterator = Iterator(board)
iterator.start()
print("Successfully Connected to Arduino Board")
except:
print("ERROR: Could Not Connect to Arduino Board")
board = None
exit()
joystick_x = board.get_pin("a:0:i")
joystick_y = board.get_pin("a:1:i")
joystick_switch = board.digital[3]
dt = 0.1
val_x, val_y, val_s = .5, .5, 0
joystick_switch.write(1)
while True:
time.sleep(dt)
val_x = joystick_x.read()
val_y = joystick_y.read()
val_s = joystick_switch.read()
print(val_s)
我想出了答案。 使用 PyFirmata 編寫時,您無法再次正確讀取它。 雖然在 c++ 你可以。 所以為了解決這個問題,我編輯了我的電路,以便我寫入另一個引腳,然后在它們之間切換操縱桿,最后進行模擬讀取。 我必須進行模擬讀取,因為一些電流會通過。 為了解決這個問題,我使用了數學庫中的地板 function 來獲得正確的值。 感謝所有試圖提供幫助的人。 這是任何想要查看它的人的代碼:
from pyfirmata import Arduino, PWM, INPUT, OUTPUT
from pyfirmata.util import Iterator
import time
from math import floor
# Setup
try:
board = Arduino('COM6')
print("here")
iterator = Iterator(board)
iterator.start()
print("Successfully Connected to Arduino Board")
except:
print("ERROR: Could Not Connect to Arduino Board")
board = None
exit()
joystick_x = board.get_pin("a:0:i")
joystick_y = board.get_pin("a:1:i")
joystick_x.enable_reporting()
joystick_y.enable_reporting()
joystick_switch = board.get_pin("a:3:i")
joystick_switch2 = board.digital[2]
joystick_switch.enable_reporting()
dt = 0.1
val_x, val_y, val_s = .5, .5, 0
joystick_switch2.write(1)
while True:
time.sleep(dt)
val_x = joystick_x.read()
val_y = joystick_y.read()
val_s = floor(joystick_switch.read())
# if val_x is None or val_y is None or val_s:
# val_x, val_y, val_s = .5, .5, 0
print(val_s)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.