简体   繁体   English

卡在 function

[英]Stuck on function

Greetings ,问候

I'm using SG90 Micro Servo for simulating the door lock where the door is unlocked after typing in the password.我正在使用SG90 Micro Servo来模拟输入密码后门被解锁的门锁。

What I'm trying to do is so the program starts with the servo rotate at 90 degrees angle so it looks like its lock, and then if the code is right it rotates at 0 degrees and after about 10 seconds it reset back to 90 degrees.我想要做的是让程序开始时伺服器以 90 度角旋转,所以它看起来像它的锁,然后如果代码是正确的,它会旋转 0 度,大约 10 秒后它会重置回 90 度.

The problem is the servo seems stuck on function lock() at the beginning and unlock() and not continue to complete the program.问题是伺服似乎卡在 function lock()开始和unlock()并且不能继续完成程序。

What can I do about this situation?这种情况我能做些什么? Thank you谢谢

door-lock.py门锁.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伺服.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

Your are testing if input(string) is equal to int 1234. You can cast your string input to integers by using int(input())您正在测试 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() 

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

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