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

Detection of a held button event

Post by Curtis » Mon May 31, 2021 8:33 pm

Hello,
I am new to programming and trying to write a simple script that will change the state of a relay based on which of two touch sensors is touched for x seconds. I was able to achieve this with Python 3 and GPIOzero but I can seem to replicate it in micropython.

This is the script I am trying to mimic in micro:

from gpiozero import Button, OutputDevice

RELAY_PIN = 18
b1=Button(26,hold_time=3)
b2=Button(19, hold_time=4)

relay = OutputDevice(RELAY_PIN, active_high=True, initial_value=False)

while True:
if b1.is_held:
relay.on()

if b2.is_held:
relay.off()

I have tried to use uasycio's primitives without success in getting them to import.

Please help is newbie.

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 » Tue Jun 01, 2021 8:30 am

Curtis wrote:
Mon May 31, 2021 8:33 pm
...
I have tried to use uasycio's primitives without success in getting them to import...
The uasyncio primitives have not yet been implemented in official uasyncio. You need to install my unofficial versions as per these instructions.
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 » Tue Jun 01, 2021 5:50 pm

Hi Peter,

Thank you for taking the time to respond to my issue. I have a gitclone of the repository and have tried to drag the privitives folder in to the Pi Pico and have tried install it through Thonny's package manager. Drag and drop yields no perceivable result and with Thonny I get:

'Error installing '': b'403'
Installing to: /home/pi/

micropip returned with error code 1'

On Thonny I am Running MP v1.15 on 2021-04018; RPi Pico w/ RP2040

I appreciate your assistance.

User avatar
aivarannamaa
Posts: 171
Joined: Fri Sep 22, 2017 3:19 pm
Location: Estonia
Contact:

Re: Detection of a held button event

Post by aivarannamaa » Wed Jun 02, 2021 5:29 am

This 403 problem is fixed in newer Thonny versions. Unfortunately RPi hasn't updated Thonny in its apt repo yet. I'll ask again about this.
Aivar Annamaa
https://thonny.org

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

Re: Detection of a held button event

Post by Curtis » Wed Jun 02, 2021 4:49 pm

Hi,

So I may have miss interpreted your response.

I down loaded the windows version of Thonny on my PC and tried the same methods as with my Pi400.

I get a different error message.

' C:/Users/psiadmin/Downloads/micropython-async-master/micropython-async-master/v3/primitives/pushbutton.py)
Error: process returned with code 1 '

Is this an issue of compatibility with the RPI Pico?

User avatar
aivarannamaa
Posts: 171
Joined: Fri Sep 22, 2017 3:19 pm
Location: Estonia
Contact:

Re: Detection of a held button event

Post by aivarannamaa » Wed Jun 02, 2021 7:02 pm

Curtis wrote:
Wed Jun 02, 2021 4:49 pm
I get a different error message.

' C:/Users/psiadmin/Downloads/micropython-async-master/micropython-async-master/v3/primitives/pushbutton.py)
Error: process returned with code 1 '

Is this an issue of compatibility with the RPI Pico?
Do you get this message while trying to install something with Thonny? Can you give me the exact steps for reproducing this?
Aivar Annamaa
https://thonny.org

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

Re: Detection of a held button event

Post by Curtis » Thu Jun 03, 2021 4:43 pm

Well I have only tried this with 'primitive' package from Peter. But here is what I am doing.

I select the 'manage packages' option from the 'Tools' menu.

Selecting 'Install from Requirements File', selecting the target folder, changing to 'All files' to navigating to the target py. file and opening it.

A start forward 'drag and drop' to the micro-controller device does not seem to work either.
this is the same for my Pi400 and my PC.

I am currently trying to install your thonny-circuitpython module on my PC.

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 04, 2021 8:18 am

@aivarannamaa FYI the installation is a matter of copying this directory and its contents https://github.com/peterhinch/micropyth ... primitives to the root of the target's fileystem.
Peter Hinch
Index to my micropython libraries.

User avatar
aivarannamaa
Posts: 171
Joined: Fri Sep 22, 2017 3:19 pm
Location: Estonia
Contact:

Re: Detection of a held button event

Post by aivarannamaa » Mon Jun 07, 2021 4:35 pm

Curtis wrote:
Thu Jun 03, 2021 4:43 pm
Selecting 'Install from Requirements File', selecting the target folder, changing to 'All files' to navigating to the target py. file and opening it.
I see... 'Install from Requirements File' assumes you are giving a simple text file (usually named requirements.txt) containing one package name per line. It can't be used for installing py-files.

If you want to have https://github.com/peterhinch/micropyth ... primitives on your MicroPython filesystem, then with Thonny you can achieve it like this:
  • download this directory to your local disk (eg. by git clone or by downloading and extracting https://github.com/peterhinch/micropyth ... master.zip)
  • Select your MicroPython device in Thonny
  • Select "View => Files" to open the file browser (if it is not open yet).
  • In the upper pane of the file browser navigate to the location of the "primitives" folder. Note that you can double-click on folders to go into them.
  • In the lower pane navigate to the desired location of the "primitives" folder on your device's filesystem (eg. '/' or '/lib')
  • In the upper pane right-click on "primitives" and select "Upload to ...".
Aivar Annamaa
https://thonny.org

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

Re: Detection of a held button event

Post by Curtis » Tue Jun 08, 2021 3:38 am

Thank you! That is what I was missing! Now that I have access to the modules I just have to learn how to use them.
:D

Post Reply