Hello everyone! I’m new to the programming scene and cant seem to include a push bottom command to my script. I’m looking to push a button, run the servo sequence and turn off. – The servos move an eyeball, built into a spell-book.
The “Using a button or switch to control servos” example has a subroutine called wait_for_buttton_press that might be helpful for you. You can find this example in the “Example Scripts” section of the Maestro User’s Guide. To integrate this subroutine with your script, you will need to copy lines 8-45 in the example and add them to your current script. Then, you can call wait_for_button_press just after your begin command. Please note that if your button is connected to a Maestro channel other than 0, you will need to change the line of code in the button subroutine to specify the correct channel. Also, if you have not already connected your button, you can find information about doing so in the “Attaching Servos and Peripherals” section of that same user’s guide.
If you try incorporating those subroutines into your script and have trouble, you can post what you tried so far and I would be happy to take a look.
I seem to still be having an issue with the command. Here’s my script with the push button
sub button
0 get_position 500 less_than
return
# This subroutine uses the BUTTON subroutine above to wait for a button press,
# including a small delay to eliminate noise or bounces on the input.
sub wait_for_button_press
wait_for_button_open_10ms
wait_for_button_closed_10ms
return
# Wait for the button to be NOT pressed for at least 10 ms.
sub wait_for_button_open_10ms
get_ms # put the current time on the stack
begin
# reset the time on the stack if it is pressed
button
if
drop get_ms
else
get_ms over minus 10 greater_than
if drop return endif
endif
repeat
# Wait for the button to be pressed for at least 10 ms.
sub wait_for_button_closed_10ms
get_ms
begin
# reset the time on the stack if it is not pressed
button
if
get_ms over minus 10 greater_than
if drop return endif
else
drop get_ms
endif
repeat
begin
wait_for_button_press
1600 6042 0 6000 4574 0 frame_1..5 # Frame 21
7000 4320 frame_4 # Frame 0
500 7960 6551 4672 frame_1_3_4 # Frame 1
700 5553 6492 0 0 frame_1..4 # Frame 2
500 0 6943 frame_2_3 # Frame 3
800 6610 5044 frame_1_3 # Frame 4
1900 6492 frame_3 # Frame 5
700 7119 6453 frame_1_3 # Frame 6
2200 6121 5944 frame_1_3 # Frame 7
500 5435 frame_3 # Frame 8
1000 7745 6199 frame_1_3 # Frame 9
500 5240 6571 frame_1_3 # Frame 10
1200 5827 5729 frame_1_3 # Frame 11
1000 5044 6766 frame_1_3 # Frame 12
1600 7745 6297 frame_1_3 # Frame 13
1600 5944 5846 frame_1_3 # Frame 14
1600 7021 6003 frame_1_3 # Frame 15
1600 6492 frame_3 # Frame 16
1600 5944 4887 frame_1_3 # Frame 17
1600 5338 5690 frame_1_3 # Frame 18
1600 6003 6023 frame_1_3 # Frame 19
1600 5475 6140 frame_1_3 # Frame 20
repeat
sub frame_1..5
5 servo
4 servo
3 servo
2 servo
1 servo
delay
return
sub frame_4
4 servo
delay
return
sub frame_1_3_4
4 servo
3 servo
1 servo
delay
return
sub frame_1..4
4 servo
3 servo
2 servo
1 servo
delay
return
sub frame_2_3
3 servo
2 servo
delay
return
sub frame_1_3
3 servo
1 servo
delay
return
sub frame_3
3 servo
delay
return
Thank you for posting your updated code. It looks like you added the subroutines before your main BEGIN/REPEAT loop of the script. Since the script starts at the top, this is causing it to start by entering a subroutine, which causes problems when it then reaches a return command.
There are a couple ways you could go about fixing this. One option is to move the subroutines to the end of your script (somewhere after the main BEGIN/REPEAT loop). Another option is to use a GOTO command with a label like the “Using a button or switch to control servos” example does. For example, adding goto main_loop at the beginning of your code and a main_loop: label just before your BEGIN command.
I moved the button commands to after the main loop, however, the script just loops on its own and does not wait for a button command
begin
1600 6042 0 6000 4574 0 frame_1..5 # Frame 21
7000 4320 frame_4 # Frame 0
500 7960 6551 4672 frame_1_3_4 # Frame 1
700 5553 6492 0 0 frame_1..4 # Frame 2
500 0 6943 frame_2_3 # Frame 3
800 6610 5044 frame_1_3 # Frame 4
1900 6492 frame_3 # Frame 5
700 7119 6453 frame_1_3 # Frame 6
2200 6121 5944 frame_1_3 # Frame 7
500 5435 frame_3 # Frame 8
1000 7745 6199 frame_1_3 # Frame 9
500 5240 6571 frame_1_3 # Frame 10
1200 5827 5729 frame_1_3 # Frame 11
1000 5044 6766 frame_1_3 # Frame 12
1600 7745 6297 frame_1_3 # Frame 13
1600 5944 5846 frame_1_3 # Frame 14
1600 7021 6003 frame_1_3 # Frame 15
1600 6492 frame_3 # Frame 16
1600 5944 4887 frame_1_3 # Frame 17
1600 5338 5690 frame_1_3 # Frame 18
1600 6003 6023 frame_1_3 # Frame 19
1600 5475 6140 frame_1_3 # Frame 20
repeat
sub button
0 get_position 500 less_than
return
# This subroutine uses the BUTTON subroutine above to wait for a button press,
# including a small delay to eliminate noise or bounces on the input.
sub wait_for_button_press
wait_for_button_open_10ms
wait_for_button_closed_10ms
return
# Wait for the button to be NOT pressed for at least 10 ms.
sub wait_for_button_open_10ms
get_ms # put the current time on the stack
begin
# reset the time on the stack if it is pressed
button
if
drop get_ms
else
get_ms over minus 10 greater_than
if drop return endif
endif
repeat
# Wait for the button to be pressed for at least 10 ms.
sub wait_for_button_closed_10ms
get_ms
begin
# reset the time on the stack if it is not pressed
button
if
get_ms over minus 10 greater_than
if drop return endif
else
drop get_ms
endif
repeat
sub frame_1..5
5 servo
4 servo
3 servo
2 servo
1 servo
delay
return
sub frame_4
4 servo
delay
return
sub frame_1_3_4
4 servo
3 servo
1 servo
delay
return
sub frame_1..4
4 servo
3 servo
2 servo
1 servo
delay
return
sub frame_2_3
3 servo
2 servo
delay
return
sub frame_1_3
3 servo
1 servo
delay
return
sub frame_3
3 servo
delay
return
It looks like you also removed the call to wait_for_button_press at the beginning of your BEGIN/REPEAT loop. I suspect it will work how you want if you add that back.
It works! Your words are full of wisdom, thank you! However… the servo script continues to repeat itself…instead of stopping after one cycle. How do I make it stop?
I’m glad you got it working! If you want the script to end after one cycle (instead of going back to waiting for a button press), you can call the QUIT command just before REPEAT.