坚持功能

Stuck on function

问候,

我用的是SG90 Micro Servo模拟门锁输入密码后开门

我想要做的是让程序从伺服旋转 90 度角开始,所以它看起来像它的锁,然后如果代码是正确的,它会以 0 度旋转,大约 10 秒后它重置回 90 度。

问题是伺服似乎卡在函数 lock() 开始和 unlock() 上,无法继续完成程序。

遇到这种情况我该怎么办?谢谢

门-lock.py

import time
import RPi.GPIO as GPIO
from servo import *

code = 1234


try:
    while True:
        lock() # The rotating thingy is at 90 degree
        pass_code = input("")
            if pass_code == code:
                print("Opening the door")
                unlock() # The rotating thingy is at 0 degree
                time.sleep(10)
                print("Locking the door")
                lock() # The rotating thingy is back at 90 degree
            
except KeyboardInterrupt:
    servo.stop() # or lock(), Im not sure

servo.py

def unlock():
    servo.ChangeDutyCycle(7) # The rotating part is at 180 degree


def lock():
    servo.ChangeDutyCycle(0) # The rotating part is at 90 degree

您正在测试 input(string) 是否等于 int 1234。您可以使用 int(input())

将字符串输入转换为整数
import time
import RPi.GPIO as GPIO
from servo import *

code = 1234


try:
    while True:
        lock() # The rotating thingy is at 90 degree
        pass_code = int(input()) //casting the string to integer
            if pass_code == code:
                print("Opening the door")
                unlock() # The rotating thingy is at 0 degree
                time.sleep(10)
                print("Locking the door")
                lock() # The rotating thingy is back at 90 degree
            
except KeyboardInterrupt:
    servo.stop() # or lock(), Im not sure
try:
    while True:
        lock() 
        pass_code = input("")
        if pass_code == code: # you dont need indentation here
            print("Opening the door")
            unlock() 
            time.sleep(10)
            print("Locking the door")
            lock() 
            
except KeyboardInterrupt:
    servo.stop()