Page 1 of 2

Two buttons on the Pico W

Posted: Wed Aug 10, 2022 10:05 am
by mevero
I am very glad I bought a Raspberry Pico W. Only again i must feel that I am not as good as I thought. I want to make a system with two buttons an i saw a script from on YouTube form Shawn Hymel which has no deb-ounce and that was one of the items I has to solve.
He told on YouTube that i was possible to make/change the script for two buttons, only I have not the knowledge to do so. I understand that this is NOT the way, but there is now an order in pushing the buttons. I hope that one of you readers knows the solution.

import machine
from machine import Pin
import uasyncio
import time

led1 = machine.Pin(5, machine.Pin.OUT)
led2 = machine.Pin(4, machine.Pin.OUT)

btn1 = machine.Pin(2, machine.Pin.IN, machine.Pin.PULL_UP)
btn2 = machine.Pin(3, machine.Pin.IN, machine.Pin.PULL_UP)

# Coroutine only return on button press.
async def wait_button1():
btn2_prev = btn2.value()
while (btn2.value() == 1) or (btn2.value() == btn2_prev):
btn2_prev = btn2.value()
await uasyncio.sleep(0.04)

async def wait_button():
btn1_prev = btn1.value()
while (btn1.value() == 1) or (btn1.value() == btn1_prev):
btn1_prev = btn1.value()
await uasyncio.sleep(0.04)

# Coroutine: entry point for asyncio program
async def main():

# Start coroutine as a task and immediately return.
uasyncio.create_task

# Main loop BEIDE KNOPJES volgorde NIET OKE
while True:
await wait_button()
led1.value(1)
exec(open('item1.py').read())
time.sleep(1)
led1.value(0)
await wait_button1()
led2.value(1)
exec(open('item2.py').read())
time.sleep(1)
led2.value(0)

# Start event loop and run entry point coroutine
uasyncio.run(main())

Re: Two buttons on the Pico W

Posted: Wed Aug 10, 2022 3:24 pm
by MicroRichard
Create async tasks and run them in main()
Something like:

Code: Select all

task1 = asyncio.create_task(wait_button1)
task2 =asyncio.create_task(wait_button)
await asyncio.gather(task1, task2) 

Re: Two buttons on the Pico W

Posted: Thu Aug 11, 2022 7:37 am
by mevero
Thank you very much for your reply, I think for Micro Python for the Pico W they use the command uasyncio. And I have not enough knowledge to put the information on the right place in the script I get the message don't know task.

Re: Two buttons on the Pico W

Posted: Thu Aug 11, 2022 10:10 am
by fdufnews
There is an online documentation you can refer to
https://docs.micropython.org/en/latest/ ... yncio.html
You can also download a pdf of the doc that you can search in
http://docs.micropython.org/en/latest/m ... n-docs.pdf

Re: Two buttons on the Pico W

Posted: Fri Aug 12, 2022 11:22 am
by mevero
First of all Thank you for the link to the documentation. But I have not enough knowledge to "understand" the Micro-Python language.
Maybe this is not a forum for users like me !! But i am still hoping that someone update my code and I am can go further with my project.
Sorry, I understand this is not my forum but.... Greetings Wim :oops:

Re: Two buttons on the Pico W

Posted: Fri Aug 12, 2022 11:54 am
by karfas
Micropython aims to be compatible (to some extent) with CPython-3. I'm sure you can find some good online courses for that.

Re: Two buttons on the Pico W

Posted: Mon Aug 22, 2022 10:42 am
by mevero
After a lot of reading, thinking, trying and maybe some learning, I succeed in making a script ( after a lot trying and correcting )
Is here a script without "errors" only a bit sad that I can get NO result pushing a button. But no errors !!
The result after a lot of......... Hope you can give me the right solution. May THANKS .. :roll:

Code: Select all

import machine
from machine import Pin
import uasyncio
import time

#led = Pin("LED", Pin.OUT)# Is de interne led
led1 = machine.Pin(5, machine.Pin.OUT)
led2 = machine.Pin(4, machine.Pin.OUT)

btn1 = machine.Pin(2, machine.Pin.IN, machine.Pin.PULL_UP)
btn2 = machine.Pin(3, machine.Pin.IN, machine.Pin.PULL_UP)


 # Start coroutine as a task and immediately return.

# Coroutine only return  on button press.
async def main(btn1 ,btn2):
    btn1_prev = btn1.value()
    while (btn1.value() == 1) or (btn1.value() == btn1_prev):
        btn1_prev = btn1.value()                    
        await uasyncio.sleep(0.04)
        

# Coroutine: entry point for asyncio program
async def main (btn1, btn2):

    uasyncio.create_task(btn1)
    uasyncio.create_task(btn2)
    
    # Main loop
    while True:
          await btn1
          led1.value(1)
          exec(open('item1.py').read())
          time.sleep(1)
          
# Start event loop and run entry point coroutine
    uasyncio.run(btn1, btn2)
    

Re: Two buttons on the Pico W

Posted: Mon Aug 22, 2022 11:24 am
by jimmo
mevero wrote:
Mon Aug 22, 2022 10:42 am
After a lot of reading, thinking, trying and maybe some learning, I succeed in making a script ( after a lot trying and correcting )
Is here a script without "errors" only a bit sad that I can get NO result pushing a button. But no errors !!
The result after a lot of......... Hope you can give me the right solution. May THANKS ..
You probably need to learn a bit more about how asyncio works. It's probably easier to do this with "real" Python first.

The reason your script has no errors is that it doesn't actually do anything because none of the functions are called. I suspect you didn't mean to indent the "uasyncio.run(btn1, btn2)" line.

But this line should be "uasyncio.run(main())"

Also you have duplicated the main() function.

Don't mix time.sleep with asyncio. If you're using asyncio you must use asyncio.sleep.

"uasyncio.create_task(btn1)" doesn't make sense, because btn1 is not an async function (it's a button). I suspect maybe you meant to call the first function "button_task" and have it take a button. Then you'd use "uasyncio.create_task(button_task(btn1))".

You might want to take a look at https://github.com/peterhinch/micropython-async -- there's a great tutorial there for all things asyncio and MicroPython.

Re: Two buttons on the Pico W

Posted: Fri Sep 02, 2022 12:39 pm
by mevero
My problem is solved by two members of the Raspberry Pi forum may thanks for the time you are trying the support me !!!

Re: Two buttons on the Pico W

Posted: Fri Oct 28, 2022 11:54 am
by mevero
It is quite difficult for a person who is not very good in creating a script in MicroPython. More difficult when you get a script to understand a program that consists so many lines. I have had help from the “ raspberrystore.nl” in producing/making a new and shorter script. For example, the purchase of my Pico W (bought in the raspberrystore.nl ) gave a very pleasant turn. Wanted to express my gratitude by posting this script and place it here so that others might be able to do something with it. Wim

This is a simple script hope you enjoy and maybe use it!!!
# Program for working with two buttons.
# Date: october 2022
# Creat : Wim van der Leek with help from a menber of raspberrystore.nl

import machine
from machine import Pin
import time

# On which pin are the led

led1 = machine.Pin(5, machine.Pin.OUT)
led2 = machine.Pin(4, machine.Pin.OUT)

# On which pin are the buttons

btn1 = machine.Pin(2, machine.Pin.IN, machine.Pin.PULL_UP)
btn2 = machine.Pin(3, machine.Pin.IN, machine.Pin.PULL_UP)

btn1_prev = btn1.value()
btn2_prev = btn2.value()

while True:

# print("1 " + str(btn1.value()) + " " + str(btn2.value())) statement for debug.

if ((btn1.value() == 1) and (btn1.value() != btn1_prev)):
btn1_prev = btn1.value() # Reminber the last state, for if someone who pressed the butoon continius.
# print("2") statement for debug

# Calling Twilio for sending a SMS, which is not URGENT

led1.value(1)
exec(open('MessageNOR.py').read())
time.sleep(1)
led1.value(0)


if ((btn2.value() == 1) and (btn2.value() != btn2_prev)):
btn2_prev = btn2.value()
# print("3") statement for debug.

# Calling Twilio for sending a SMS, It is very urgent.

led2.value(1)
exec(open('MessageUR.py').read())
time.sleep(1)
led2.value(0)

btn1_prev = btn1.value()
btn2_prev = btn2.value()

time.sleep(0.1)