1

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
Al-
  • 27
  • 7
  • Where does `ChangeDutyCycle` come from? It is likely this is the blocking call. – ricekab Dec 15 '20 at 11:57
  • Can you show your output from terminal? – Daniel Dec 15 '20 at 11:58
  • Your `while` loop calls `lock` as often as possible if the entered code is wrong. Could that be a problem with `servo.ChangeDutyCycle`? – bjhend Dec 15 '20 at 12:01
  • @ricekab im using [this source code](https://www.explainingcomputers.com/sample_code/Servo_Test_AA.py) – Al- Dec 15 '20 at 12:02
  • @Daniel there is not output, it just stuck – Al- Dec 15 '20 at 12:02
  • Could you also correct your indentation in your original question? Then we can see if perhaps this is an indentation mistake. – ricekab Dec 15 '20 at 12:04
  • @bjhend what `servo.ChangeDutyCycle` do as far as I understand is that it will turn the rotor thingy using the value from 0 to 12, 0 is turning it to 0 degrees and 12 is to 180 degree – Al- Dec 15 '20 at 12:05

2 Answers2

2

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
Daniel
  • 546
  • 6
  • 17
0
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() 
SLDem
  • 2,065
  • 1
  • 8
  • 28