简体   繁体   English

如何读取多条件 Python Arduino Uno

[英]How to read multiple condition Python Arduino Uno

I have a question.我有个问题。 I am currently working on the python-Arduino Uno project.我目前正在从事 python-Arduino Uno 项目。

so here we go.所以我们开始吧。 I wanna send the value from python 1 to Arduino and get the other value from python 2. so actually I want to make the Arduino Uno that can read 2 conditions then it will do stuff...我想将值从 python 1 发送到 Arduino 并从 python 2 获取另一个值。所以实际上我想让 Arduino Uno 可以读取 2 个条件然后它会做一些事情......

code python 1代码蟒蛇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'))

code python 2代码蟒蛇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 code 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); 
  }
 }

You're only reading the value once, then comparing that same value twice.您只读取一次值,然后两次比较相同的值。 You need to read the value again using Serial.read() if you want to compare a second value.如果要比较第二个值,则需要使用Serial.read()再次读取该值。 A simple example of this:一个简单的例子:

  value1 = Serial.read();
  Serial.print(value1);

  value2 = Serial.read();
  Serial.print(value2);

  if (value1 == '1' && value2 == 'h' ) { 
    digitalWrite(pin, HIGH);
      delay(1000);
  }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM