#πŸ”’ CircuitPython Project using timer and function call

18 messages Β· Page 1 of 1 (latest)

echo light
#

Hi everyone, Im a little lost on a school project and could use some advice. Its essentially a home security system where some push buttons allow the user to enter a passcode to arm or disarm the security protocols. The issue im encountering is when the system is Armed a countdown is started (lets say 10 seconds) when motion is detected. This countdown gives time for the user to enter the passcode to disarm the system before it activates the alarm.

im trying to code this in CircuitPython and im having some issues.

def alarm():
    global photoresistor
    attempt = 0
    photoresistor.deinit()
    led = DigitalInOut(board.A0)
    led.direction = Direction.OUTPUT
    led.value = True

    while True:
        user_pass = PES()
        if user_pass == ref_passcode:
            led.value = False
            led.deinit()
            photoresistor = analogio.AnalogIn(board.A0)
            security_sys()
        elif user_pass != ref_passcode:
            print("Incorrect")
            attempt += 1
            if attempt == 3:
                print("ALARM ACTIVATED")
                while True:
                    led.value = True
                    time.sleep(1)
                    led.value = False
                    time.sleep(1)
                    # CONTINUOUS ALARM

This is my alarm function. I want to add a timer using time.monotonic(), but I cant do that and call my password function -- that allows the user to input their passcode because it blocks the timer from running.

storm pathBOT
#

@echo light

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

echo light
#

here is the password function that is called during the alarm:

def PES():
    pes_passcode = []
    last_state =  {"0": False, "1": False, "2": False, "#": False, "*": False}
    while True:
        S0.value = False
        S1.value = False
        if not switch.value and not last_state["0"]:
            pes_passcode.append("0")
            print("0")
            last_state["0"] = True
        if switch.value:
            last_state["0"] = False
        S0.value = True
        S1.value = False
        if not switch.value and not last_state["1"]:
            pes_passcode.append("1")
            print("1")
            last_state["1"] = True
        if switch.value:
            last_state["1"] = False
        S0.value = False
        S1.value = True
        if not switch.value and not last_state["2"]:
            pes_passcode.append("2")
            print("2")
            last_state["2"] = True
        if switch.value:
            last_state["2"] = False

        S0.value = True
        S1.value = True
        if not switch.value and not last_state["#"]:
            print("#")
            last_state["#"] = True
            # Submit when "#" is pressed
            if pes_passcode:
                print("Code entered:", "".join(pes_passcode))
                return pes_passcode
                pes_passcode = []
        if switch.value:
            last_state["#"] = False

        S0.value = False
        S1.value = False
        if not switch2.value and not last_state["*"]:
            print("*")
            last_state["*"] = True
            if pes_passcode:
                print("Code entered:", "".join(pes_passcode))
                return pes_passcode
                pes_passcode = []
        if switch2.value:
            last_state["*"] = False
#

the buttons are multiplexed thats why there is a lot of redundancy

supple nymph
#

So, what kind of help you need in this? Is it not working? Any errors you are seeing?

echo light
#

sorry I guess I should have clarified more. Im trying to implement a timer into my alarm function, however when I start a timer with time.monotonic() or something similar and then call my PES() function the timer stops working. I believe its because PES() is a blocking function since it requires a user input to work. Im just wondering how I could make a timer work in the alarm

#

Im considering finding a way to redesign the PES function, but im not sure how itll effect the rest of the program

wispy ferry
#

Does that while loop just spin flat out? Or does it block waiting for something like a button press?

echo light
#

It waits for a button press. I have 5 push buttons that all have an assigned value

#

When the correct passcode is entered it’s supposed to turn arm or disarm the system

supple nymph
echo light
#

ill take a look, thanks

supple nymph
#

Here is minimal example I have tried

echo light
#

ok! ill try to implement something like this

supple nymph
#

Instead of raising generic exception, you can a raise a specific exception and try to catch that

storm pathBOT
#
Python help channel closed for inactivity

This help channel has been closed. Feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.