Program Selection Modification #1387
Macabre2000
started this conversation in
General
Replies: 1 comment 4 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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
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