KAG Tech
Posts: 44
Joined: Mon Jun 21, 2021 7:11 am

Combining 2 Programs

Thu Jul 22, 2021 3:04 pm

Please kindly assist on how to combine 2 programs.

Pin description:
sensor_[xxxn]_pir[n] - sensor signal (input)
standdown_pir3 - confirms a closed loop (input)
bluelock_pir6 - works as a button (input)
outputrelay - pin to trigger relay (output)
test_led - confirm functionality (output)
activate - set high to power bluelock device

objective of program 1
1. sensor_[xxxn]_pir[n] is programmed to send signal to the Pi Pico in any case of intrusion and turn on the test_led.
2. activate is set high to power the bluelock device
3. bluelock_pir6 works as button or trigger, it is programmed to send a signal to Pi Pico and simultaneously set outputrelay to high for 20 seconds and read signal from sensors as false for 40 seconds.

Code: Select all

import machine
import utime

sensor_adc1_pir = machine.Pin(27, machine.Pin.IN, machine.Pin.PULL_DOWN)
sensor_adc2_pir2 = machine.Pin(28, machine.Pin.IN, machine.Pin.PULL_DOWN)
standdown_pir3 = machine.Pin(5, machine.Pin.IN, machine.Pin.PULL_UP)
sensor_dig1_pir4 = machine.Pin(14, machine.Pin.IN, machine.Pin.PULL_DOWN)
sensor_dig2_pir5 = machine.Pin(15, machine.Pin.IN, machine.Pin.PULL_DOWN)
bluelock_pir6 = machine.Pin(6, machine.Pin.IN, machine.Pin.PULL_DOWN)
outputrelay = machine.Pin(4, machine.Pin.OUT)
test_led = machine.Pin(8, machine.Pin.OUT)
activate = machine.Pin(7, machine.Pin.OUT)

activate.on()

def pir_handler(pin):
    if pin.value():
        if pin is sensor_adc1_pir:
            print("Alarm! sensor_adc1!")
            test_led.value(1)
            utime.sleep(0.2)
            test_led.value(0)
        elif pin is sensor_adc2_pir2:
            print("Alarm! sensor_adc2!")
            test_led.value(1)
            utime.sleep(0.2)
            test_led.value(0)
        elif pin is sensor_dig1_pir4:
            print("Alarm! sensor_dig1!")
            test_led.value(1)
            utime.sleep(0.2)
            test_led.value(0)
        elif pin is sensor_dig2_pir5:
            print("Alarm! sensor_dig2!")
            test_led.value(1)
            utime.sleep(0.2)
            test_led.value(0)
        elif pin is standdown_pir3:
            print("door closed!")
            test_led.value(1)
            utime.sleep(0.2)
            test_led.value(0)
            
    elif bluelock_pir6.value() == 0:
        print("bluelock access!")
        outputrelay.value(1)
        utime.sleep(20)
        outputrelay.value(0)
        utime.sleep(0)
        sensor_adc2_pir2.value(0)
        utime.sleep(10)
        sensor_adc1_pir.value(0)
        utime.sleep(10)
        sensor_dig1_pir4.value(0)
        utime.sleep(10)
        sensor_dig2_pir5.value(0)
        utime.sleep(10)
        test_led.value(1)
        utime.sleep(0.2)
        test_led.value(0)
        
sensor_adc1_pir.irq(trigger=machine.Pin.IRQ_RISING, handler=pir_handler)
sensor_adc2_pir2.irq(trigger=machine.Pin.IRQ_RISING, handler=pir_handler)
sensor_dig1_pir4.irq(trigger=machine.Pin.IRQ_RISING, handler=pir_handler)
sensor_dig2_pir5.irq(trigger=machine.Pin.IRQ_RISING, handler=pir_handler)
standdown_pir3.irq(trigger=machine.Pin.IRQ_RISING, handler=pir_handler)
bluelock_pir6.irq(trigger=machine.Pin.IRQ_RISING, handler=pir_handler)
program 2

pins.
POT - analog input
activate - output pin (fully dependent on POT input voltage)

objective
is to set "activate" high when "POT" reads equals to(=) or greater than (>) 1.7V and set "activate low when "POT" reads (=) or (<) 1.8V.
NB this is to protect the bluelock device from resetting itself due to low voltage

Code: Select all

from machine import ADC, Pin
from time import sleep

POT = ADC(0)
active = machine.Pin(8, machine.Pin.OUT)
conversion_factor = 3.3/65536

while True:
    volt = POT.read_u16() * conversion_factor
    if volt <= 1.6: 
        active.value(0)
        
    elif volt >= 1.7:
        active.value(1)
it seem like program 2 is overlapping program 1 when combined. please assist.

hippy
Posts: 10602
Joined: Fri Sep 09, 2011 10:34 pm
Location: UK

Re: Combining 2 Programs

Fri Jul 30, 2021 10:49 am

It seems to me you would at least need to refactor you code so your PIR handlers are non-blocking, don't wait in 'time.sleep()' functions.

It looks like you are trying to create a security alarm system - perhaps with SMS alerts looking at your other threads. Is this just for fun, a coursework project, for a company, or ultimately intended as a commercial product ?

How safety critical, mission critical or business critical it needs to be will decide what the most appropriate approach would be.

KAG Tech
Posts: 44
Joined: Mon Jun 21, 2021 7:11 am

Re: Combining 2 Programs

Fri Jul 30, 2021 1:54 pm

hippy wrote:
Fri Jul 30, 2021 10:49 am
It seems to me you would at least need to refactor you code so your PIR handlers are non-blocking, don't wait in 'time.sleep()' functions.

It looks like you are trying to create a security alarm system - perhaps with SMS alerts looking at your other threads. Is this just for fun, a coursework project, for a company, or ultimately intended as a commercial product ?

How safety critical, mission critical or business critical it needs to be will decide what the most appropriate approach would be.
Correct, it is a security (locking) and alarm system that now works even better with the modified "utime.sleep()". The reason why I want to add the second code is to protect a Bluelock device (works as a button but requires an input voltage) that tends to reset itself due to low voltage.

Regarding the "SMS alert"
It is a Varsity project (for Project Design module) and we are working in groups of 3 and I am handling the alarm coding while my group mates are working on a SIM800L, at the moment they do have a program and have connected the SIM800L to the Pi Pico but unable to send and receive an SMS.

For time being, we focused on the project being "Mission Critical" because all groups are required to do a visual presentation.

hippy
Posts: 10602
Joined: Fri Sep 09, 2011 10:34 pm
Location: UK

Re: Combining 2 Programs

Fri Jul 30, 2021 6:48 pm

KAG Tech wrote:
Fri Jul 30, 2021 1:54 pm
For time being, we focused on the project being "Mission Critical" because all groups are required to do a visual presentation.
That's not a correct interpretation of "mission critical". As a Varsity project, you have to present it, show it working, but it doesn't have to work beyond what it does. It doesn't matter if it has flaws or bugs which would make it entirely unsuitable if it were a real security system product and needing to protect real valuables.

That makes it easier, because you can probably take your first code, replace all your utime.sleep() with sleep(). Then at the top you can add a sleep routine which checks the battery status.

Something like -

Code: Select all

POT = ADC(0)
active = machine.Pin(8, machine.Pin.OUT)
conversion_factor = 3.3/65536

activate.on()

def CheckVoltage():
    volt = POT.read_u16() * conversion_factor
    if volt <= 1.6: 
        active.value(0)
   elif volt >= 1.7:
        active.value(1)

def sleep(t):
    CheckVoltage()
    while t > 0.1:
        time.sleep(0.1)
        t = t - 0.1
        CheckVoltage()
    if t > 0:
        time.sleept(t)

def pir_handler(pin):
    if pin.value():
        if pin is sensor_adc1_pir:
            print("Alarm! sensor_adc1!")
            test_led.value(1)
            sleep(0.2)              # <======
            test_led.value(0)

... snip ...

bluelock_pir6.irq(trigger=machine.Pin.IRQ_RISING, handler=pir_handler)

while True:
    CheckVoltage()
    time.sleep(0.1)

KAG Tech
Posts: 44
Joined: Mon Jun 21, 2021 7:11 am

Re: Combining 2 Programs

Tue Aug 03, 2021 8:14 am

hippy wrote:
Fri Jul 30, 2021 6:48 pm
KAG Tech wrote:
Fri Jul 30, 2021 1:54 pm
For time being, we focused on the project being "Mission Critical" because all groups are required to do a visual presentation.
That's not a correct interpretation of "mission critical". As a Varsity project, you have to present it, show it working, but it doesn't have to work beyond what it does. It doesn't matter if it has flaws or bugs which would make it entirely unsuitable if it were a real security system product and needing to protect real valuables.

That makes it easier, because you can probably take your first code, replace all your utime.sleep() with sleep(). Then at the top you can add a sleep routine which checks the battery status.

Something like -

Code: Select all

POT = ADC(0)
active = machine.Pin(8, machine.Pin.OUT)
conversion_factor = 3.3/65536

activate.on()

def CheckVoltage():
    volt = POT.read_u16() * conversion_factor
    if volt <= 1.6: 
        active.value(0)
   elif volt >= 1.7:
        active.value(1)

def sleep(t):
    CheckVoltage()
    while t > 0.1:
        time.sleep(0.1)
        t = t - 0.1
        CheckVoltage()
    if t > 0:
        time.sleept(t)

def pir_handler(pin):
    if pin.value():
        if pin is sensor_adc1_pir:
            print("Alarm! sensor_adc1!")
            test_led.value(1)
            sleep(0.2)              # <======
            test_led.value(0)

... snip ...

bluelock_pir6.irq(trigger=machine.Pin.IRQ_RISING, handler=pir_handler)

while True:
    CheckVoltage()
    time.sleep(0.1)
I followed your approach and at first I kept getting "<stdin>" errors therefore I had to use "utime.sleep" on some parts of the script and it worked well. Thank you very much for your assistance, it was of great help. :D :D :D

hippy
Posts: 10602
Joined: Fri Sep 09, 2011 10:34 pm
Location: UK

Re: Combining 2 Programs

Tue Aug 03, 2021 11:11 am

KAG Tech wrote:
Tue Aug 03, 2021 8:14 am
at first I kept getting "<stdin>" errors therefore I had to use "utime.sleep" on some parts of the script and it worked well.
Very odd. 'time' and 'utime' should effectively be synonymous in this code. But I guess it doesn't matter providing what you have works.

Return to “MicroPython”