python - Arduino code to python
问题描述
I have arduino code for reading two inputs and them using them to make 3 or 4 different outputs bassed on input state, i need to make that in python, but im not good with it yet. Any help would be appreciated. I would use some leds to represent states for start later on when i learn something more i would like to do something more advanced.
int A = 3;
int B = 4;
int C = 5;
int D = 6;
int E = 7;
void setup(){
Serial.begin(9600);
pinMode (A, INPUT);
pinMode (B, INPUT);
pinMode (C, OUTPUT);
pinMode (D, OUTPUT);
pinMode (E, OUTPUT);
}
void loop(){
pic();
delay (100);
}
void pic(){
int a = digitalRead(A);
int b = digitalRead(B);
if(a == LOW && b == LOW){
Serial.print("something");
digitalWrite(C, HIGH)
digitalWrite(D, LOW)
digitalWrite(E, LOW)
}
if(a == LOW && b == HIGH){
Serial.print("something");
digitalWrite(C, LOW)
digitalWrite(D, HIGH)
digitalWrite(E, LOW)
}
if(a == HIGH && b == LOW{
Serial.print("something");
digitalWrite(C, LOW)
digitalWrite(D, HIGH)
digitalWrite(E, LOW)
}
if(a == HIGH && b == HIGH){
Serial.print("something");
digitalWrite(C, LOW)
digitalWrite(D, LOW)
digitalWrite(E, HIGH)
}
}
解决方案
Try this You must install RPi.GPIO if not
pip install RPi.GPIO
import RPi.GPIO as GPIO
from time import sleep
# Use on of this. (visit https://pinout.xyz/ for more details)
# GPIO.setmode(GPIO.BOARD) # If you are using number on the board (1 --> 3.3V, 2 --> 5V)
GPIO.setmode(GPIO.BCM) # If you are using the Broadcom numbering
A = 5
B = 6
C = 13
D = 19
E = 26
def pic():
a = GPIO.input(A);
b = GPIO.input(B);
if a == GPIO.LOW and b == GPIO.LOW:
print("something");
GPIO.output(C, GPIO.HIGH)
GPIO.output(D, GPIO.LOW)
GPIO.output(E, GPIO.LOW)
if a == GPIO.LOW and b == GPIO.HIGH:
print("something");
GPIO.output(C, GPIO.LOW)
GPIO.output(D, GPIO.HIGH)
GPIO.output(E, GPIO.LOW)
if a == GPIO.HIGH and b == GPIO.LOW:
print("something")
GPIO.output(C, GPIO.LOW)
GPIO.output(D, GPIO.HIGH)
GPIO.output(E, GPIO.LOW)
if a == GPIO.HIGH and b == GPIO.HIGH:
print("something")
GPIO.output(C, GPIO.LOW)
GPIO.output(D, GPIO.LOW)
GPIO.output(E, GPIO.HIGH)
GPIO.setup(A, GPIO.IN)
GPIO.setup(B, GPIO.IN)
GPIO.setup(C, GPIO.OUT)
GPIO.setup(D, GPIO.OUT)
GPIO.setup(E, GPIO.OUT)
while True:
pic()
sleep(100 / 1000)
To run execute in a terminal
python filename.py
推荐阅读
- python - Tkinter 打开两个窗口而不是 1,如果我参考根窗口会崩溃
- c# - 用字典序列化/反序列化对象
- node.js - 使用 http-proxy-middleware 处理 WebSocket 错误
- spring - Spring、JUnit 和 EasyMock:EntityNotFoundException
- ssh - 如何使用管道脚本将 zip 文件从工作区传输到我已经设置的从机(目标服务器)
- javascript - 如何将 bas64 转换为 varbinary(max)
- node.js - react-native - 设置基本项目的警告
- c - 如何读取字符串直到输入结束,读取行直到在 C 中找到 \n?
- kubernetes - Google Compute Engine 上的 Kubernetes 状态集模拟
- azure - 请求中指定的回复 url 与 Angular 中为应用程序配置的回复 url 不匹配