如何读取多个条件 Python Arduino Uno

How to read multiple condition Python Arduino Uno

我有一个问题。 我目前正在研究 python-Arduino Uno 项目。

我们开始吧。 我想将 python 1 中的值发送给 Arduino 并从 python 2 中获取另一个值。 所以实际上我想制作可以读取2个条件的Arduino Uno 然后它会做一些事情...

代码python1

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);
  }