Script switch issue

Thanks in advance for any help received.

In the code below I would like servo 1(ch1) and servo 2(ch2) to stop after their movements and then if the switch(ch0) is switched they will move to 6000 and then script stops. Hope that made sense? I’ve tried multiple ways and just can’t see it to get it to work. It keeps looping after if.

begin 0 get_position 512 less_than if 4000 1 servo 500 delay 8000 1 servo 500 delay 4000 2 servo 500 delay 8000 2 servo 500 delay else 6000 1 servo 6000 2 servo endif repeat

I’m using the Maestro 18 Channel servo controller.

So I messed with and changed the code and got it working, but it’s doing some things I didn’t expect or I’m just not savvy enough to figure out why. :slight_smile:

Code below–

[code]1

begin
1000 delay
switch if start else shutdown endif
3000 delay
repeat

sub switch
0 get_position 500 less_than
return

sub start
begin
dup
while
1000 1 servo 7000 delay
5400 6 servo 2000 delay
6000 4 servo 4000 delay
9000 3 servo 2000 delay
1000 2 servo 1700 delay
15000 2 servo 1700 delay
1000 2 servo 1700 delay
15000 2 servo 1700 delay
5000 2 servo 2500 delay
3000 5 servo 1200 delay
8000 5 servo 1200 delay
3000 5 servo 1200 delay
8000 5 servo 1200 delay
6100 5 servo 1500 delay
4000 6 servo 1200 delay
8000 6 servo 1200 delay
5400 6 servo
1 minus
repeat
return

sub shutdown
6000 5 servo 2000 delay
6000 6 servo 2000 delay
4000 4 servo 4000 delay
5000 2 servo 1000 delay
4100 3 servo 1700 delay
15000 1 servo
return[/code]

1: An issue I’m having is when the switch is turned ON it takes (or the delay is) to long. I would like it to delay for 2 sec. when switch is turned ON and delay for 3 sec when switch is turned OFF. That would give me enough time to remove the key from the switch. It’s a keyed switch as you guessed. Not sure if I need a debounce code for this?

2: Also servo 4 struggles a bit with it’s load and will hum some when moved to position and stationary. If I code the Maestro to stop sending a signal it does go silent so I’m hoping to figure out a way keep it silent for initial start and shutdown subs and somehow try to keep the speed function? I have read the FAQ’s section and understand about the speed and acceleration loss on power up. Just hoping to try and fool it somewhat. Hope that made sense?

3: Reading through the Maestro’s guide I can’t seem to figure out how to properly calculate the actual servo position to number form for code. I just guessed and played quite a while to get it right. For instance let’s say my Min and Max position is 1020-1630. How do I calculate the precise number for the code to work in between those positions only?

4: Forgot to add that when I hit the Apply Settings tab after making adjustments on the Channel Settings page it automatically starts the script. Can I do something to get that to not happen? When I’m testing servos and changing settings I would like it to not start the script.

Thanks!!

Hello.

  1. From your code, it looks like the shutdown subroutine is continuously looping followed by a three second delay, which would explain why it takes longer than you are expecting for the program check the status of your switch. You might try using a state variable to keep track of the switch and determine when it is switched “ON” or “OFF”. You can see an example of a state variable used here.

  2. One way you could have speed and acceleration limits still apply to the movements of a servo once it has been turned off is to first send the servo to the last known position it was in when the Maestro turned it off. The speed and acceleration limits should be applied to subsequent movements.

  3. The units are displayed as microseconds under the “Status” tab of the Maestro Control Center, whereas the units are displayed as quarter-microseconds in the Maestro scripting language. To convert from microseconds to quarter-microseconds, you would need to multiply the microsecond value by 4 (e.g. 1020 * 4 = 4080).

  4. It sounds like you have the “Run script on startup” option enabled. To prevent the script from running whenever you make a change in the Maestro Control Center and clicking on the “Apply Settings” button, you can uncheck the “Run script on startup” box under the “Script” tab of the Maestro Control Center. Remember to enable that option and apply those changes if you are going to use the Maestro as a standalone device (not connected to your computer via USB).

- Amanda

Thank you Amanda! I will play with this all this weekend and let you know how it goes. I appreciate the quick response!

Amanda-

I have everything working and am so excited! I will post pics and a video once I have everything mounted and ready. I do have one other question though. I am still struggling to get my key switch to function just right. This key switch I would like to leave in the ON position once turned. The way the code is below I have to turn ON and then OFF real quick to get my “sub start” to go just once. Otherwise left ON it keeps looping the “sub start”. I would like to be able to leave the switch in the ON position. (I then can remove key so no tampering is done) I’m just so confused on how to get it working right? Probably the most simple thing and I just can’t see it. Thanks for your help!


1

begin

0 get_position 500 less_than

if
dup

if start
drop 0
else shutdown
drop 1
endif

endif

repeat

sub start

 begin
  dup
 while
                2000  delay
  8000 14 servo 500   delay
  4000 14 servo 500   delay
                1000  delay
  8000 15 servo 500   delay
  4000 15 servo 500   delay
  1000  1 servo 10000 delay
  5400  6 servo 2000  delay
  6000  4 servo 5000  delay
  4100  3 servo 500   delay
  9000  3 servo 2000  delay
  6800 10 servo 5000  delay
  1984  2 servo 1700  delay
  9040  2 servo 1700  delay
  1984  2 servo 1700  delay
  9040  2 servo 1700  delay
  5000  2 servo 2500  delay
  3000  5 servo 1200  delay
  8000  5 servo 1200  delay
  3000  5 servo 1200  delay
  8000  5 servo 1200  delay
  6100  5 servo 1500  delay
  4000  6 servo 1200  delay
  8000  6 servo 1200  delay
  4000  6 servo 1200  delay
  8000  6 servo 1200  delay
  5400  6 servo 1500  delay 
  8000 11 servo 500   delay
  0     3 servo 500   delay
  1 minus
 repeat 

return

sub shutdown

                 2000 delay
   8000 16 servo 500  delay
   4000 16 servo 500  delay
                 1000 delay
   5000  2 servo 1000 delay
   8000 17 servo 500  delay
   4000 17 servo 500  delay
   4000 11 servo 1000 delay
   6000  5 servo 2000 delay
   6000  6 servo 2000 delay
   4000  4 servo 4000 delay 
   4800 10 servo 1000 delay
   9000  3 servo 500  delay
   4100  3 servo 1700 delay
   9024  1 servo 500  delay
   0     3 servo 500  delay

return 

I looked at your code and noticed that your script doesn’t check for the variable state before calling your start and shutdown subroutines. You would need to add a couple of IF statements to check the status of your state variable before executing the appropriate subroutine. For example, you might try the following code for the BEGIN/REPEAT block:

1		           #state variable status
begin
  dup		       #duplicate the state variable value
  0 get_position 500 less_than
  if		        #check switch state: ON
    if start		#if state variable is 1, start subroutine hasn't been executed
      drop 0
    endif
  else		      #switch state: OFF
    0 equals	   #check if state variable is 0.
    if shutdown	#if state variable is 0, shutdown subroutine hasn't been executed
      drop 1
    endif
  endif
repeat

Also, I noticed that you have a BEGIN/REPEAT block and a WHILE command in the start subroutine, which seems unnecessary since the subroutine should only execute once whenever called. It would be easier and probably less confusing if you control the number of times a behavior-specific subroutine is called from the main loop instead of in the subroutine.

- Amanda

Amanda-

Worked like a charm. Thanks!  I really appreciate all the help!