how run IRQ() IN IRQ()

Discussion about programs, libraries and tools that work with MicroPython. Mostly these are provided by a third party.
Target audience: All users and developers of MicroPython.
User avatar
jimmo
Posts: 2754
Joined: Tue Aug 08, 2017 1:57 am
Location: Sydney, Australia
Contact:

Re: how run IRQ() IN IRQ()

Post by jimmo » Tue Jun 04, 2019 1:28 am

mehdi_ziyaei wrote:
Tue Jun 04, 2019 1:04 am
pin_starter= Pin(5 ,Pin.IN ,handler = enter_password() ,trigger = Pin.IRQ_RISING)
You need to not have the "()" after enter_password.

Code: Select all

pin_starter= Pin(5 ,Pin.IN ,handler = enter_password ,trigger = Pin.IRQ_RISING)
(Otherwise this is the same as using the `irq` function later.
mehdi_ziyaei wrote:
Tue Jun 04, 2019 1:04 am
The second irq is always active and calls the callback function with a knock detection
Yes. That's how it works - once you register an IRQ, it is always active. You should only do this once.


I'm sorry this isn't an easy problem you're trying to solve.

Can you start with a simpler problem. For example, forget about IRQs, just write a program that counts the number of times a knock has happened. It will look something like:

Code: Select all

num_knocks = 0
knock_pin = Pin(5, Pin.IN)
while True:
  # Look at the pin value and see if it's changed.
  # If it has, add one to the counter and print it out.

mehdi_ziyaei
Posts: 16
Joined: Tue May 21, 2019 5:58 pm

Re: how run IRQ() IN IRQ()

Post by mehdi_ziyaei » Tue Jun 04, 2019 2:25 pm

like this ????

Code: Select all

def new_enter_password():
    door_password=""
    knock_count = 0
    from machine import Pin
    p5 = Pin(5, Pin.IN)
    for i in range(5):
        knock_count = 0
        print("range %i"%i)
        chk_time = utime.time() + 10
        while utime.time() <= chk_time:
            if p5.value()==1:
                knock_count += 1
                print(knock_count)
        door_password += str(knock_count)
        print("door password = " + door_password)
    print("end")


Code: Select all

def new_enter_password():
    door_password=""
    knock_count = 0
    from machine import Pin
    p5 = Pin(5, Pin.IN)
    for i in range(5):
        knock_count = 0
        print("range %i"%i)
        chk_time = utime.time() + 10
        while utime.time() <= chk_time:
            while p5.value()==1:
                knock_count += 1
                print(knock_count)
        door_password += str(knock_count)
        print("door password = " + door_password)
    print("end")



result very bad .Counters were counted most of the time
by one knock count is 4536 .

i use sleep time for fix .not work

Code: Select all

def new_enter_password():
    door_password=""
    knock_count = 0
    from machine import Pin
    p5 = Pin(5, Pin.IN)
    for i in range(5):
        knock_count = 0
        print("range %i"%i)
        chk_time = utime.time() + 10
        while utime.time() <= chk_time:
            if p5.value()==1:
                knock_count += 1
                print(knock_count)
                utime_sleep(1)
        door_password += str(knock_count)
        print("door password = " + door_password)
    print("end")


Attachments
Screenshot (57).png
Screenshot (57).png (154.74 KiB) Viewed 1857 times

mehdi_ziyaei
Posts: 16
Joined: Tue May 21, 2019 5:58 pm

Re: how run IRQ() IN IRQ()

Post by mehdi_ziyaei » Tue Jun 04, 2019 6:28 pm

Code: Select all


import network
import machine
import utime
import os 
import re

from machine import Pin
num_kenok = 0




def main():
    starter= Pin(5, Pin.IN)
    while True:
        if starter.value()==1:
            print("enter password fun")
            led_on_off(3,600)
            p5 = Pin(5, Pin.IN)
            door_password=""
            for i in range(5):
                global num_kenok
                num_kenok = 0
                chk_time = utime.time() + 12
                while utime.time() <= chk_time:
                    p5.irq(trigger=Pin.IRQ_RISING ,  handler=callback )
                door_password += str(num_kenok)
                led_on_off(1,500)
            led_on_off(5,300)
            find_password(door_password)
    return

def led_on_off(lp,sleep_time):
    led = Pin(2, Pin.OUT)
    for i in range(lp):
        led.off()
        utime.sleep_ms(sleep_time)
        led.on()   
        utime.sleep_ms(sleep_time)
    return

     
def callback(p):
    global num_kenok
    if num_kenok < 9 :
        num_kenok += 1
        led_num_kenok = Pin(4, Pin.OUT)
        led_num_kenok.on()
        utime.sleep_ms(200)     
        led_num_kenok.off()   
    return
    
    

def find_password(f_password):
        print(f_password)
      #  log_write("find_password  - start to find user and password ")
        f_username =""
        with open("user_list.text" , "r") as file:
            for line in file:
                if line.startswith("d_pass="+f_password+" d_user="):
                    line = line.strip()
                    f_username = line[20:]
                    break       
        file.close()    
        if f_username.strip():
         #   log_write("find_password  - find user and password ")
            print("ok")
        else:
         #   log_write("find_password  - not find user and password ")
            print("not ok")
        return


main and enter_passwort >> main function
is ok but Sometimes the amount of the starter is not zero and remains running

Post Reply