我有一个问题。我目前正在研究python-Arduino Uno项目。
所以我们走了。我想将值从python 1发送到Arduino并从python 2获取另一个值。所以实际上我想制作可以读取2个条件的Arduino Uno然后它将做的事情...
代码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);
}