我有一个问题。 我目前正在研究 python-Arduino Uno 项目。
所以我们开始吧。 我想将值从 python 1 发送到 Arduino 并从 python 2 中获取另一个值。 所以实际上我想让Arduino Uno可以读取2个条件 然后它会做一些事情...
Python代码1
i = results[1]
i *= 100
if i >= 75:
print('Recognised as Me!')
arduino.write(str.encode('1'))
else:
print('not matches')
arduino.write(str.encode('0'))
代码Python 2
c = results[0]
c *= 100
d = results[2]
d *= 100
e = results[1]
e *= 100
for a in top_k:
if a == 0 and c >= 50:
arduino.write(str.encode('a'))
if a == 1 and e > 50:
arduino.write(str.encode('h'))
if a == 2 and d >= 50:
arduino.write(str.encode('s'))
Arduino代码
int pin = 13;
char getValue;
void setup()
{
pinMode(pin, OUTPUT);
Serial.begin(9600);
}
void loop()
{
if(Serial.available() > 0)
getValue = Serial.read();
Serial.print(getValue);
if (getValue == '1' && getValue == 'h' ) { //its not works for me.
digitalWrite(pin, HIGH);
delay(1000);
}
else if (getValue == '0'){
digitalWrite(pin, LOW);
delay(1000);
}
}
最佳答案
您只读取该值一次,然后比较同一值两次。如果您想比较第二个值,则需要使用 Serial.read()
再次读取该值。一个简单的例子:
value1 = Serial.read();
Serial.print(value1);
value2 = Serial.read();
Serial.print(value2);
if (value1 == '1' && value2 == 'h' ) {
digitalWrite(pin, HIGH);
delay(1000);
}
关于python - 如何读取Python Arduino Uno的多个条件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59121063/