释放python中的按钮时输出更改

时间:2019-06-21 13:48:05

标签: python arduino

我正在开发一种算法,该信号将使用python发送到Firebase。流程是当我按下按钮时,它会被检测到并在释放时算作1次。

我尝试的python代码就是这样

AS = serial.Serial('COM8',9600)
url = 'https://xxx.firebaseio.com/'
AoS = 40
FB = firebase.FirebaseApplication(url, None)

result = FB.patch('/ParkingSeat/',{'Amount of the seats': str(AoS) + '/40'})
while (1==1):
    if(AS.inWaiting()>0):
        msg = AS.readline().strip().decode()
        if (msg == '1'):
            print ("Waiting for release")
            print (msg)
            if (AoS>0) and (msg == '0'):
                AoS-=1
                print ("The Signal now is " + str(AoS))
                result = FB.patch('/ParkingSeat/',{'Amount of the seats': str(AoS) + '/40'})
        if (msg == '2'):
            print ("Waiting for release")
            print (msg)
            if (AoS<40) and (msg == '0'):
                AoS+=1
                print ("The Signal now is " + str(AoS))
                result = FB.patch('/ParkingSeat/',{'Amount of the seats': str(AoS) + '/40'})
        print(msg)

我尝试的arduino代码就是这样

const int BUTTON1 = 6;
const int BUTTON2 = 7;
String i, j, x;

int ButtonState = 0;

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  pinMode(BUTTON1, INPUT_PULLUP);
  pinMode(BUTTON2, INPUT_PULLUP);
}

void loop() {
  if (digitalRead(BUTTON1) == LOW) {
    delay(500);
    i = "1";
    Serial.println(i);
  } 
  else if (digitalRead(BUTTON2) == LOW) {
    delay(500);
    j = "2";
    Serial.println(j);
  } 
  else {
    delay(500);
    x = "0";
    Serial.println(x);
  }
}

我也在将python与arduino连接起来。并且arduino一直以0的形式向python发送信号。按下button1或button2时,它将发送1或2。

因此,我正在尝试的代码是用户按下button1或button2给出的输出为1 1 1 1 1,结果将在用户立即释​​放按钮后发送。

但是现在我的代码只能在按下button1或button2的同时接收信号。当我持续按下按钮时,它将继续发送信号,但是我不想要。一次松开按钮后如何发送类似信号的信号?

0 个答案:

没有答案