Program Selection Modification #1387
Macabre2000
started this conversation in
General
Replies: 4 comments 16 replies
-
from pybricks.hubs import PrimeHub
from pybricks.parameters import Button, Color, Direction, Port, Stop
from pybricks.tools import wait
# Let's offer these menu options. You can add as many as you like.
menu_options = ("D", "L", "F")
menu_index = 0
hub = PrimeHub()
while True:
# Normally, the center button stops the program. But we want to use the
# center button for our menu. So we can disable the stop button.
hub.system.set_stop_button(None)
while True:
hub.display.char(menu_options[menu_index])
# Wait for any button.
pressed = ()
while not pressed:
pressed = hub.buttons.pressed()
wait(10)
# Wait for the button to be released.
while hub.buttons.pressed():
wait(10)
# Now check which button was pressed.
if Button.CENTER in pressed:
# Center button, so the menu is done!
break
elif Button.LEFT in pressed:
# Left button, so decrement menu menu_index.
menu_index = (menu_index + 1) % len(menu_options)
elif Button.RIGHT in pressed:
# Right button, so increment menu menu_index.
menu_index = (menu_index - 1) % len(menu_options)
# Now we want to use the Center button as the stop button again.
hub.system.set_stop_button(Button.CENTER)
# Based on the selection, choose a program.
selected = menu_options[menu_index]
if selected == "D":
import Testtwo
elif selected == "L":
import fasdfasdf
elif selected == "F":
import rotationtest
|
Beta Was this translation helpful? Give feedback.
4 replies
-
|
this is how I did the hub menu for our FLL team. seems to work well and the stopEverything() function is: |
Beta Was this translation helpful? Give feedback.
10 replies
-
|
I know this is kind of late but you can do |
Beta Was this translation helpful? Give feedback.
1 reply
-
|
Thank you for your response. I already did this though, and forgot to
delete the old 'exec' message it after I realized that Pybricks python does
not support what I did.
…On Tue, Apr 22, 2025 at 1:11 AM Bert ***@***.***> wrote:
Hi Agni,
To run an imported program or function multiple times, you can make a file
that contains one or more functions.
Do not get confused by my longer filenames.
A small example to be imported with two functions:
1. turn motor forward
2. turn motor backward
I named it issue_1387_my_function.py
# filename issue_1387_my_functions.py
from pybricks.pupdevices import Motorfrom pybricks.tools import wait
def motor_fwd(motor):
turn_angle = 360
print("turn motor", turn_angle, "degrees")
motor.run_angle(100, turn_angle)
wait(1000)
def motor_bwd(motor):
turn_angle = -360
print("turn motor", turn_angle, "degrees")
motor.run_angle(100, turn_angle)
wait(1000)
The main program:
# program name issue_1387_main_example.py
from pybricks.pupdevices import Motorfrom pybricks.parameters import Portfrom pybricks.tools import wait
#from pybricks.hubs import PrimeHubimport issue_1387_my_functions
# hub = PrimeHub()motor_a = Motor(Port.A)
# Run the code multiple timesissue_1387_my_functions.motor_fwd(motor_a) # First runwait(500)issue_1387_my_functions.motor_bwd(motor_a) # Second run backwardwait(500)issue_1387_my_functions.motor_fwd(motor_a) # Third run
# Can be used in loops toofor i in range(5):
issue_1387_my_functions.motor_bwd(motor_a)
wait(250)
issue_1387_my_functions.motor_fwd(motor_a)
wait(250)
This way you can call the functions numerous times.
Hope this helps.
Bert
—
Reply to this email directly, view it on GitHub
<#1387 (reply in thread)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AYSQQKRVJFKFQHWW47D7VRD22X2SBAVCNFSM6AAAAABCDUHKAGVHI2DSMVQWIX3LMV43URDJONRXK43TNFXW4Q3PNVWWK3TUHMYTEOJQG4ZTSNY>
.
You are receiving this because you commented.Message ID:
***@***.***>
|
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
I am fairly new to pybricks and I am trying to get code that allows the user to select from multiple programs. I found the thread at the link here #861 which provided a nice approach to allow for this. The only problem with the example code is that it exits after a selected code block is executed. I tried modifying the code to allow the user to select a different code after the first selected code executes, but the next selected program does not seem to execute. The code lets me select a new program letter, but does not execute when I press the circle button. Are there any suggestions on how I could loop back to the program select menu after the previous selection completes?
edit:: it appears that one issue i am having is that the motors are assigned within each sub-program which causes a conflict when the next program tries to connect to the port. Is there a command i can use to delete the prior port associations so that the next program can assign them when it runs?
Beta Was this translation helpful? Give feedback.
All reactions