I tried making some code in my driver loop that clarified what variable equaled 1 and providing text showing the driver what variable it was. I tried using “if var == 1” then print that var is enabled. The issue is that when var was switched from 0 to 1, the entire code froze. I am guessing this is due to var always equaling 1 and being thrown in a constant loop of printing something. I do not have access to a brain as I am on vacation but wanted to ask if this is the solution to my problem.
"
Controller Values
con.screen.set_cursor(1,0)
con.screen.print_(TrayPot.value(RotationUnits.DEG))
con.screen.set_cursor(2,0)
con.screen.print_(ArmPot.value(RotationUnits.DEG))
#Prototype#######################################################################################
while BlueLeft == 1:
con.screen.set_cursor(3,0)
con.screen.print_("Blue Left Auton")
while BlueRight == 1:
con.screen.set_cursor(3,0)
con.screen.print_("Blue Right Auton")
while RedLeft == 1:
con.screen.set_cursor(3,0)
con.screen.print_("Red Left Auton")
while RedRight == 1:
con.screen.set_cursor(3,0)
con.screen.print_("Red Right Auton")
#################################################################################################
"
The code you posted will, if the corresponding condition is met, enter a loop that will print the specified text to the screen over and over again until the value of the relevant variable changes, which probably isn’t what you want to do.
If you post the complete program that you were having problems with earlier (remember to wrap it in triple backtics - ```
- rather than quotation marks) we can try to diagnose things further.
2 Likes
con.screen.set_cursor(1,0)
con.screen.print_(TrayPot.value(RotationUnits.DEG))
con.screen.set_cursor(2,0)
con.screen.print_(ArmPot.value(RotationUnits.DEG))
#Prototype#######################################################################################
if BlueLeft == 1:
con.screen.set_cursor(3,0)
con.screen.print_("Blue Left")
elif BlueRight == 1:
con.screen.set_cursor(3,0)
con.screen.print_("Blue Right")
elif RedLeft == 1:
con.screen.set_cursor(3,0)
con.screen.print_("Red Left")
elif RedRight == 1:
con.screen.set_cursor(3,0)
con.screen.print_("Red Right")
else:
con.screen.set_cursor(3,0)
con.screen.print_("No Auton")
#################################################################################################
Here it is, I am not sure if this is exactly the original due to me being out of town but I remember myself doing this. Clarification would do wonders, thank you!
For printing variable values, you would want to use if
, elif
, and else
as opposed to while
loops. Your assumption:
Is correct - you used while
loops to check a variable’s status and didn’t give an option for the code to exit in that while loop. Compare this pseudocode:
# driver control code
while true:
# this loop should never end
set robot motors to desired voltage/velocity based on controller input
while var == 1:
print var
# this while loop will never end because var is never given a chance to change in this loop
to:
# driver control code
while true:
# this loop should never end
set robot motors to desired voltage/velocity based on controller input
if var == 1:
print var
# using if, elif, else here instead of while will let the driver loop repeat and update motors accordingly
In the first pseudocode piece, the robot would be stuck in the nested while
loop because var
can’t change. In the second, because if
is used, the robot will run the code in the if
statement if the condition is true and carry on with its business.
In general, be careful about creating more while loops inside of driver control and avoid doing so if necessary. Hope this helped!
2 Likes
So with this reply I can guess that my second code suggested worked? If you didn’t catch it I will place it here again:
# Controller Values
con.screen.set_cursor(1,0)
con.screen.print_(TrayPot.value(RotationUnits.DEG))
con.screen.set_cursor(2,0)
con.screen.print_(ArmPot.value(RotationUnits.DEG))
#Prototype#######################################################################################
if BlueLeft == 1:
con.screen.set_cursor(3,0)
con.screen.print_("Blue Left")
elif BlueRight == 1:
con.screen.set_cursor(3,0)
con.screen.print_("Blue Right")
elif RedLeft == 1:
con.screen.set_cursor(3,0)
con.screen.print_("Red Left")
elif RedRight == 1:
con.screen.set_cursor(3,0)
con.screen.print_("Red Right")
else:
con.screen.set_cursor(3,0)
con.screen.print_("No Auton")
#################################################################################################
Yes, since the while
loops have been replaced with an if
statement the code shouldn’t be caught in an infinite loop.
If you don’t mind me asking, is this for an autonomous selector? If so, you should be able to use one variable (such as autoIdentifier
for example) and set that variable to a number which would correspond to a specific autonomous, as opposed to using 4 variables to track it. Your code should work fine if you keep using the 4 variables (assuming the rest of your program is fine) but restructuring an autonomous selector to work with 1 variable would help to keep your code simpler.
2 Likes
Oh true lmao. I just got caught up in the world of 1’s and 0’s and totally didn’t think that one variable with multiple options of values would work. Will optimize thanks!
I tried to use auton selectors that were premade and had no prevail, Im just using multiple images showing and it “looking” like a interact-able menu