Hi there, is there a way to use button to call another program in Pico?
The story is, I want to call a program when I press the button and stop the program when I release the button.
Not sure if there is a sample code to help me to do that.
Thank you @Pixmusix !
Yes a program is just another simply main.py file stored in Pico.
Now I can run main.py by pressing the button, but I cannot stop the program running by double pressing the button.
Oh sorry I thought the idea was to run the program while the button was held down?
Is that still your preferred behavior?
Have a look at this page of the docs where it talks about external interrupts.
Notice that you can interrupt on the rising edge (button down) or the falling edge (button up).
You might choose the connect the same button to two different GPIOs to get the effect.
Can I clarify, is there anything stopping you from checking the buttonâs state in your picoâs loop?
# Backflips will be plentiful while the button is held down
while true:
while(buttonGPIO.value()):
# do a backflip
Would you prefer a double click to turn off the program? Itâs possible but requires more code.
Sorry I didnt explain my question clearly, and I am truly a newbie to Pico and Python . Please allow me to explain in more detail.
The hardware I have: Pi Pico, PiicoDev expansion board and PiicoDev Button. So all the hardware has been connected via PiicoDev connector.
The Python code I have stored in Pico is: main.py and test.py
What I am hoping to achieve is to autorun the main.py when Pico powered up, and call test.py when I press the button, and end test.py when I double press the button, then call test.py again when I single press it.
What I am asking might be a very silly question and can be done easily, apologise for my lack of understanding in Python.
Hope the above makes a bit more sense, really looking forward to hearing from you
Is the problem that you cannot detect a button double press? If thatâs the problem then show the code you are using, describe what you expected it to do, and describe what it actually does.
Or is the problem that you need to do something else when the program ends? Usually, ending a program is done by putting it in an endless do-nothing loop. If you are going to call test.py again at some later stage then it seems you need to run main.py again. That would just be a copy of the code you already have to to call test.py, with a change of the program name.
Ahhhhh! Thatâs an i2c device
The good news is that you wonât need any hardware interupt logic with an i2c button like the piicodev button.
Edit : This was in the title the entire time I just missed it oops
Thatâs super clear
I second Jeff where we should establish we can read the button and get the correct behavior.
There are lotâs of ways to do this but I think the path you will find easiest is to create a variable called âshouldMyProgramRunâ.
shouldMyProgramRun = False
if button.was_pressed:
shouldMyProgramRun = True
if button.was_double_pressed:
shouldMyProgramRun = False
print("should my program run?: ",shouldMyProgramRun)
The next thing I want to know is what is test.py?
What does it do? Is it a long program? Should it run once or should it run in a loop?
What does test.py do is very simple. I have got a PiicoDev Buzzer connected as well. So in main.py, the buzzer starts playing âmusicâ.
What does test.py do is, when I single press the button, run main.py, and when I double press the button, end main.py.
My problem is I dont know how to end main.py in test.py and I have no idea how to get main.py running again after it has been ended.
Maybe it will be easier if I just add a power switch to turn Pico on/off?
Ahhh.
If you double press when the music is only half way through do you expect it to stop playing immediately or would you be happy to let the audio play out until the end and then not repeat?
What happens if you just run main.py when the button is pressed in test.py, exactly like you are doing to run test.py when you press the button in main.py?
Ok I see your problem.
When you press the button the pico starts working on the playing the music.
But the pico has to âstay focusedâ on keeping the music playing.
When you double press to stop, the pico isnât listening because itâs too busy running test.py
Finally, when the music stops, the pico is free to focus back on the button and only now notices it was supposed to have stopped ages ago.
What you need is multithreading/multiprocessing.
One thread runs test.py and the other thread checks the button.
Iâm sorry but Iâve never done non-blocking code in micropython but maybe someone else can assist.
No worries Jonny, really appreciate for your time and help. Good thing is now I know what is the problem
I will keep searching see if there is any possible solution for it
Possibly, but thatâs nothing to do with playing music. Itâs simply to do with how (or if) the button is being interrogated while the music is playing. Threading could be used to play the music, but it isnât required unless, of course, OP doesnât have access to the test.py code. Thatâs possible given that we havenât seen any example of the code that is being used. But if itâs not required for a reason such as that then there will be much simpler ways of detecting the button press.
As @Pixmusix said, multithreading could work, though there are also a couple of other options as @Jeff105671 mentioned.
I expect your PiicoDev Buzzer and PiicoDev Button code is very similar to that seen in our guides on both devices?
If you are able to send the code youâre using for both main.py and test.py, we can likely suggest some methods. This is the best path forward to allow us to give you accurate suggestions!
@Puzheng278952 Suggest you have a look at using Asyncio in microython on the Pi Pico. It allows two separate processes to run at the same time or seem like they are running at the same time.
In a project I have, one process continually monitors a button and sets a global variable when the button is pressed. This process waits for 100ms at the end of the loop.
The other process does other stuff and also checks the variable and does something if it is set.
Happy to put some code together if you need.
Regards
Jim
PS the multithreading @Pixmusix suggested would be used in a more advanced system like a Pi 4 or 5 where the operating system handles the multithreading tasks.