Python覆盆子pi GPIO Logi

2024-05-03 09:43:15 发布

您现在位置:Python中文网/ 问答频道 /正文

嗨,伙计们,谁能发现我的错误我只想在什么时候GPIO.input.输入(22)它将等待gpio.input.输入(11) 在调用一个方法之前,我试图比较时间,但似乎它不能离开循环。谢谢

from datetime import datetime
import time
import threading
import MySQLdb

import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BOARD)
GPIO.setup(11, GPIO.IN)
GPIO.setup(15, GPIO.OUT)
GPIO.setup(7, GPIO.OUT)
GPIO.setup(22, GPIO.IN)
GPIO.setup(18, GPIO.OUT)

db = MySQLdb.connect("localhost","root","root","test" )
cursor = db.cursor()

class Variable:
    count = 0
    people = 0
    times = 0
    decision = 0
    choice = 0

def countdown():
    GPIO.output(15, False)
    time.sleep(5)
    GPIO.output(15, True)

t = threading.Timer(5.0, countdown)   
def enterexit():
    global t  # You need this to tell Python that you're going to change the global t variable. If you don't do this, using 't = ..' will just create a local t variable.
    if int(Variable.choice == 1):
        print("Entered")
        print ("TIME: " + datetime.now().strftime('%Y-%m-%d %H:%M:%S'))
        sql = """INSERT INTO `timing`( `Time`, `Date`) VALUES (CURTIME(), CURDATE())"""
        t.start()
    else:
        if(Variable.decision == 1):            
            print("Entered")
            print ("TIME: " + datetime.now().strftime('%Y-%m-%d %H:%M:%S'))
            sql = """INSERT INTO `timing`( `Time`, `Date`) VALUES (CURTIME(), CURDATE())"""
            t.cancel()
            t.join()         # here you block the main thread until the timer is completely stopped
            t = threading.Timer(5.0, countdown)
            t.start()
        elif(Variable.decision == 2):            
            print("Exit")
            print ("TIME: " + datetime.now().strftime('%Y-%m-%d %H:%M:%S'))
            sql = """INSERT INTO `timing`( `Time`, `Date`) VALUES (CURTIME(), CURDATE())"""
            t.cancel()
            t.join()         # here you block the main thread until the timer is completely stopped
            t = threading.Timer(5.0, countdown)
            t.start()


while True:
    global sensor1
    global sensor2
    global Time1
    global Time2
    if GPIO.input(22):
        Time1 = datetime.now().strftime('%H%M%S')
        while True:
            if GPIO.input(11):
                 GPIO.output(18, False)
                 GPIO.output(18, True)
                 Time2 = datetime.now().strftime('%H%M%S')
                 if(Time1 < Time2):
                     Variable.count+=1
                     Variable.choice += 1
                     Variable.people += 1
                     Variable.decision = 1
                     enterexit()
                     break
            else:
                print("Waiting")

    elif GPIO.input(11):
        Time2 = datetime.now().strftime('%H%M%S')
        while True:
            if GPIO.input(22):
                GPIO.output(7, False)
                GPIO.output(7, True)
                Variable.count-=1
                Time1 = datetime.now().strftime('%H%M%S')
                if(Time2 < Time1):
                    Variable.people -= 1
                    print(Variable.people)
                    Variable.decision = 2
                    enterexit()
                    break
            else:
                print("Waiting")

    else:
        Variable.count = 0

db.close()

Tags: theimporttrueinputoutputdatetimegpioif
1条回答
网友
1楼 · 发布于 2024-05-03 09:43:15

几个问题:

  • 使用Time1 < Time可以比较字符串。这是你想要的行为吗?或者换句话说:为什么要使用strftime()?在
  • 为什么Time1和{}是全局的?在

相关问题 更多 >