Help with coding a button into my script


I’m trying to make a script in which the following has to happen:
A main sequence of servo movements runs and repeats itself forever, but when a button is pressed (once)
it runs another sequence and then the script stops. My question is, do I implement a regular if-function that constantly checks wether the button is pressed (and when it is, just start the second sequence and end it with a QUIT?) or do I implement some sort of waiting-for-button-to-be-pressed kind of thing?
I’m not very good at coding, any help would be appreciated.

I’m not sure on how to format this into the post but this is the script with nothing but examples of the two sequences in it.

### Sequence subroutines: ###

# main seq
sub main_seq
  500 7584 0 0 0 0 0 
  0 0 0 0 0 0 
  0 0 0 0 0 0 
  0 0 0 0 0 0 frame_0..23 # Frame 0
  500 8912 frame_0 # Frame 1
  500 7374 frame_0 # Frame 2
# power off seq
sub power_off_seq
  500 7374 0 0 0 0 0 
  0 0 0 0 0 0 
  0 0 0 0 0 0 
  0 0 0 0 0 0 frame_0..23 # Frame 0
  500 8050 frame_0 # Frame 1
  500 6628 frame_0 # Frame 2

sub frame_0..23
  23 servo
  22 servo
  21 servo
  20 servo
  19 servo
  18 servo
  17 servo
  16 servo
  15 servo
  14 servo
  13 servo
  12 servo
  11 servo
  10 servo
  9 servo
  8 servo
  7 servo
  6 servo
  5 servo
  4 servo
  3 servo
  2 servo
  1 servo
  0 servo

sub frame_0
  0 servo

I’m assuming the lack of answers (which is usually pretty fast) can mean either one of two things. Either I have to post a script demonstrating my progress (there isn’t one) or it simply cannot be done. That is, if I can’t make the script listen to button presses whilst running I have to use another maestro to send the commands? It would be nice if this can be done with only one maestro!


Sorry for the delay. It is possible to do something like that on one Maestro, but since the Maestro does not have support for interrupts on input pins, it will be a little more complicated. For the button press to be registered, your get_position command would have to be called while the button was pressed. Note that since the delay command is blocking, this means that the button press could not be registered while the Maestro is delaying. It is possible to write a special subroutine to do non-breaking delays, which is what the script I wrote in this post does, so that might be what you are looking for. Essentially, if you use that script as a stating point, you can have your main sequence running in the BEGIN/REPEAT loop, and your shut down sequence in the sequence_a subroutine (instead of the example I have there).


1 Like

Thanks for the answer, no need to apologize, I just meant that I was probably missing something.

I think I understand how to use your script, although I’m worried that I can’t use the sequencer to make the movement sequences? I’m probably missing something, but can I program the movements using the sequencer, use the function to automatically export them to a script, save that script and then manually implement the non-breaking delay function into those? The main sequence is very long and hard to make without using the sequencer in the control center.

You should be able to integrate the script generated by the sequencer in with my script with some modifications made. For example, you will need to change the DELAY commands generated with the nb_delay subroutine. Also, since the button checking is done inside of the nb_delay, you will probably want to make sure the shutdown sequence doesn’t use the nb_delay subroutine, or it will call itself recursively if the button is still pressed. Since the sequencer will reuse subroutines where it can, this might involve making the shutdown sequence use a special subroutine.

If you try putting the two scripts together and have problems, you can post your entire script here and I’d be glad to take a look.


1 Like

Allright man, I tried merging the two scripts and it’s working exactly as intended. The nb_delay sub works like a charm. Thanks a bunch for your help man, you’re a wizard.

1 Like