python - 如何将arduino代码翻译成python pyfirmata?
问题描述
我想使用 pyfirmata 将这个 Arduino 代码翻译成 python 代码。我怎样才能做到这一点?
int sw = 0;
void setup() {
// put your setup code here, to run once:
pinMode(2, INPUT);
pinMode(6, OUTPUT);
}
void loop() {
// put your main code here, to run repeatedly:
sw = digitalRead(2);
if (sw == LOW){
digitalWrite(6, LOW);
}
else {
digitalWrite(6, HIGH);
}
}
我试着做
from pyfirmata import Arduino, util
board = Arduino('COM3')
it = util.Iterator(board)
it.start()
button = board.get_pin('d:2:i')
led = board.get_pin('d:6:o')
while True:
sw = button.read()
print(sw)
if sw:
led.write(1)
else:
led.write(0)
但这不起作用,当我打印sw
它时返回None
然后我尝试这样做,但它只是一直返回None
。
from pyfirmata import Arduino, util, INPUT
board = Arduino('COM3')
it = util.Iterator(board)
it.start()
while True:
board.digital[2].mode = INPUT
board.digital[2].enable_reporting()
print(board.digital[2].read())
解决方案
推荐阅读
- python - 如何重用会话数据值
- javascript - 为什么“改变”变量会得到奇怪的结果
- c# - AddDbContext 在 net Core 5.0 web api 中不起作用
- python - 带有按钮的 Tkinter GUI - 想要在按钮上运行 python 脚本
- javascript - 我将如何验证用户身份 SOCKET.IO
- python - 用于 MNIST 数据集的 Python 中的 TensorFlow(“dict”对象没有属性“train”)
- symfony - 当一个属性在 Symfony 5 中的 URL 约束失败时退出类验证器
- javascript - 在 Jest 测试中处理多个日期对象
- node.js - 不和谐 JS | Loki JS - 我想为我的数据库代码实现一个处理程序
- powerbi - 需要根据另一个表中的前 N 个表过滤一个表