Al-
Al-

Reputation: 27

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.

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.

The problem is the servo seems stuck on function lock() at the beginning and unlock() and not continue to complete the program.

What can I do about this situation? Thank you

door-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

Upvotes: 1

Views: 357

Answers (2)

Daniel
Daniel

Reputation: 576

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

Upvotes: 2

SLDem
SLDem

Reputation: 2182

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() 

Upvotes: 0

Related Questions