Detection of a held button event

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
Curtis
Posts: 9
Joined: Mon May 31, 2021 8:18 pm

Re: Detection of a held button event

Post by Curtis » Thu Jun 24, 2021 4:56 pm

Hello again,

I am having trouble with (I assume) either uasynco, primitives or my Pico. The following code works great but only for a limited time. Like about 30 mins.

Code: Select all

from machine import Pin
from primitives.pushbutton import Pushbutton
import uasyncio as asyncio

async def my_app():
    await asyncio.sleep(600)
ledblue =  machine.Pin(1,machine.Pin.OUT)
ledred =  machine.Pin(2,machine.Pin.OUT) 
relay = machine.Pin(18,machine.Pin.OUT)
b2=machine.Pin(26,machine.Pin.IN, machine.Pin.PULL_DOWN)
b1=machine.Pin(19,machine.Pin.IN, machine.Pin.PULL_DOWN)
relay.value(0)
            
def input1():
    relay.value(0)
    ledblue.value(1)
    ledred.value(0)
    
def input2():
    relay.value(1)
    ledred.value(1)
    ledblue.value(0)


pb1 = Pushbutton(b1, suppress=True)
pb2 = Pushbutton(b2, suppress=True)

pb1.long_func(input1)
pb2.long_func(input2)

loop = asyncio.get_event_loop()
loop.run_until_complete(my_app())


If I and a 'while True' statement it will go for about 12 hours before locking up and not changing the relay's state.
Any thoughts as to what is happening.

User avatar
pythoncoder
Posts: 5956
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: Detection of a held button event

Post by pythoncoder » Thu Jun 24, 2021 5:33 pm

I'm not clear where you're putting the 'while True' statement which makes it run for 12 hours, perhaps you could clarify. But I have no idea why the code posted fails after 30 minutes.

A couple of comments on the code. There is no need for the suppress arg as it only affects the release function, which you're not using. Secondly in uasyncio V3 there is no need for the event loop code. You can write

Code: Select all

#loop = asyncio.get_event_loop()
#loop.run_until_complete(my_app())
asyncio.run(my_app())
However none of this will fix your problem. Failures after such a long time seem unlikely to be caused by your code which is very straightforward. I would investigate any possible electrical issues.
Peter Hinch
Index to my micropython libraries.

Curtis
Posts: 9
Joined: Mon May 31, 2021 8:18 pm

Re: Detection of a held button event

Post by Curtis » Thu Jun 24, 2021 7:07 pm

Hi,

I agree with you that the code failing seems unlikely. So, I added the led indication as another way to observe function. When it's locked up I can see the activation of the Adafruit cap touch board, which is powered thought the Pico, by way of the LED's built in. So it seems that the problem is not a lack of power to the setup. The LEDs I added stay lite in step to the relay, which also has LED indication. I have used two power supplies in the setup the result in the same with both, a phone changer style and my DELL thunderbolt dock. I guess should try swapping out Pico now, I got another yesterday.

Thank you for taking the time with my little issue.

Code: Select all

from machine import Pin
from primitives.pushbutton import Pushbutton
import uasyncio as asyncio

async def my_app():
    await asyncio.sleep(600)
ledblue =  machine.Pin(1,machine.Pin.OUT)
ledred =  machine.Pin(2,machine.Pin.OUT) 
relay = machine.Pin(18,machine.Pin.OUT)
b2=machine.Pin(26,machine.Pin.IN, machine.Pin.PULL_DOWN)
b1=machine.Pin(19,machine.Pin.IN, machine.Pin.PULL_DOWN)
relay.value(0)
            
def input1():
    relay.value(0)
    ledblue.value(1)
    ledred.value(0)
    
def input2():
    relay.value(1)
    ledred.value(1)
    ledblue.value(0)

while True:
    
    pb1 = Pushbutton(b1, suppress=True)
    pb2 = Pushbutton(b2, suppress=True)

    pb1.long_func(input1)
    pb2.long_func(input2)

Curtis
Posts: 9
Joined: Mon May 31, 2021 8:18 pm

Re: Detection of a held button event

Post by Curtis » Thu Jun 24, 2021 7:54 pm

PXL_20210624_193341689.MP.jpg
PXL_20210624_193341689.MP.jpg (257.36 KiB) Viewed 2258 times
PS

I have also bypass the cap touch with tactile buttons when it's lock up and the LED's that are assocated to the relay fucntions don't change. But, the cap touch board picks it up.
Attachments
PXL_20210624_193251031.MP.jpg
PXL_20210624_193251031.MP.jpg (205.76 KiB) Viewed 2258 times
PXL_20210624_193346197.MP.jpg
PXL_20210624_193346197.MP.jpg (289.94 KiB) Viewed 2258 times
PXL_20210624_193509054.MP.jpg
PXL_20210624_193509054.MP.jpg (235.25 KiB) Viewed 2258 times

User avatar
pythoncoder
Posts: 5956
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: Detection of a held button event

Post by pythoncoder » Fri Jun 25, 2021 4:59 am

I'm even more puzzled now. That while True loop makes no sense at all and I'm rather surprised it even works. You should not need to repeat the statements in that loop. I'm still unclear whether your code has actually crashed when it fails. If you flash an LED or emit a print statement in your main loop, does it continue after a failure?

Code: Select all

count = 0
async def my_app():
    global count
    count += 1
    print(count)
    await asyncio.sleep(60)
I have done long running tests on uasyncio applications, but not yet on the Pico. On the Pyboard D my longest run was six weeks until my wife briefly unplugged it, thinking that neither it nor I would notice. So, in the right circumstances, uasyncio applications can run indefinitely.

However to achieve long running you need to be aware of electrical issues. Relays generate a back EMF when switched off. Long wires can pick up interference. Power supplies can issue brief glitches. Making any electronic equipment run for long periods needs attention to detail.
Peter Hinch
Index to my micropython libraries.

User avatar
pythoncoder
Posts: 5956
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: Detection of a held button event

Post by pythoncoder » Fri Jun 25, 2021 5:22 am

A few minutes thought has left me even more confused. You have two places where the code should run forever and they therefore can't both run. The line

Code: Select all

loop.run_until_complete(my_app())
should never terminate, since my_app runs forever. Please see my uasyncio tutorial for discussion of this.

If your while True loop is before this line, you can't be starting uasyncio. But if it's after that line, the loop should never actually run.
Peter Hinch
Index to my micropython libraries.

Curtis
Posts: 9
Joined: Mon May 31, 2021 8:18 pm

Re: Detection of a held button event

Post by Curtis » Fri Jun 25, 2021 5:13 pm

Hello,

Thank you again for giving this issue so much of your time.

When the set up fails the LED's I added wont change state with any input I have added.

I have not yet read thought the info in the link in your last message.

But, in-between our messages I built a similar simpler set up with another board that uses the RPi 2040. The Qt Py RP2040 from Adafruit is connected the same model of relay as the other setup but I'm using psychical buttons in stead of the cap touch and I am using the on board Neopixel for secondary indication.

I have matched the button color to the Neopixel color.

Video:
https://www.dropbox.com/s/pghxxn8fa86zr ... 2.mp4?dl=0

Code: Select all

from machine import Pin
from primitives.pushbutton import Pushbutton
import uasyncio as asyncio
from neopixel import Neopixel

async def my_app():
    await asyncio.sleep(600)
    
LED = Neopixel(1 , 1, 12, "RGB")
clear = LED.set_pixel(0, (0, 0, 0))
PWR = machine.Pin(11,machine.Pin.OUT)
relay = machine.Pin(29,machine.Pin.OUT)
b1=machine.Pin(28,machine.Pin.IN, machine.Pin.PULL_DOWN)
b2=machine.Pin(27,machine.Pin.IN, machine.Pin.PULL_DOWN)
relay.value(0)
PWR.value(1)
clear
LED.show()

def input1():
    relay.value(0)
    clear
    LED.show()
    LED.set_pixel(0, (0, 0, 255))
    LED.show()
    
    
def input2():
    relay.value(1)
    clear
    LED.show()
    LED.set_pixel(0, (0, 255, 0))
    LED.show()
    


pb1 = Pushbutton(b1)
pb2 = Pushbutton(b2)

pb1.long_func(input1)
pb2.long_func(input2)

loop = asyncio.get_event_loop()
loop.run_until_complete(my_app())
asyncio.run(my_app())
I have ran this code with and without the:

loop = asyncio.get_event_loop()
loop.run_until_complete(my_app())

It runs the same either way.

The button function/action [long, press ,double] is detected on both buttons and is executed. I was not expecting the button function assigned to pb1 to be detected on pb2 and visa versa . Could this be part of the 'lockup'?

Parts list:
Adafruit QT Py RP2040; PRODUCT ID: 4900
12mm Square Tactile Button Switch; PRODUCT ID: 1010
Adafruit STEMMA Non-Latching Mini Relay; PRODUCT ID: 4409

Thank you

User avatar
pythoncoder
Posts: 5956
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: Detection of a held button event

Post by pythoncoder » Sat Jun 26, 2021 5:33 am

This is not reasonable Python:

Code: Select all

clear = LED.set_pixel(0, (0, 0, 0))
clear
Try this:

Code: Select all

def foo():
    print('foo')
clear = foo()
Issuing clear does nothing. This is because the line

Code: Select all

clear = foo()
runs the foo function and assigns the result to clear. The default result from Python fuctions is None. So executing clear is the same as typing None at the REPL. It does nothing.

As always, I'm going to suggest buying a book or doing an online Python course...
Peter Hinch
Index to my micropython libraries.

Post Reply