"I need help using the maestro script language

The 18-channel Mini Maestro and program example,that I reverse engineered, Works fine with 1 sensor and 1 servo.
But I know the program is not optimized.
For the last five days I’ve read a lot of info, On the script language
And on forth programing, so I fill I understand how the stack part goes.
Right now I’m pulling the program apart to see what makes it work, with out all the extra fluff . but would like some help on How to make it work with 4 digital ir sensors and 4 servos the servos, well be on lines 0 1 2 3 and sensors well be on inputs 17 16 15 14, there is 2 sec delay on servos when there at their max positions.
Thanks
Gary

# Set the servo to 4000, 6000, or 8000 depending on an digital input.
  begin
 17 get_position # get the value of the sensor, 0-255.75
  dup 300 less_than
 if
 4000 # go to 4000 for values 0-299
  else
 dup 600 less_than
 if
 6000 # go to 6000 for values 300-599
  else
 8000 # go to 8000 for values 600-1023
  endif
 endif
0 servo
 2000 delay
 drop # remove the original copy of the sensor value
  repeat 	

Hello, RoboCookie.

To help me understand your script, I made made this copy of it with more readable indentation:

# Set the servo to 4000, 6000, or 8000 depending on an digital input.
begin
  17 get_position # get the value of the sensor, 0-255.75
  dup 300 less_than
  if
    4000 # go to 4000 for values 0-299
  else
    dup 600 less_than
    if
      6000 # go to 6000 for values 300-599
    else
      8000 # go to 8000 for values 600-1023
    endif
  endif
  0 servo
  2000 delay
  drop # remove the original copy of the sensor value
repeat

It looks pretty good. You are reading the value of an IR sensor on channel 17, using if/else/endif to compute a servo position from it, writing the servo position to servo 0, delaying for two seconds, and then repeating.

To extend this to handle another servo and sensor, just copy the relevant code and change the channel numbers from 0/17 to 1/16:

begin
  17 get_position # get the value of the sensor
  dup 300 less_than
  if
    4000 # go to 4000 for values 0-299
  else
    dup 600 less_than
    if
      6000 # go to 6000 for values 300-599
    else
      8000 # go to 8000 for values 600-1023
    endif
  endif
  0 servo
  drop

  16 get_position # get the value of the sensor
  dup 300 less_than
  if
    4000 # go to 4000 for values 0-299
  else
    dup 600 less_than
    if
      6000 # go to 6000 for values 300-599
    else
      8000 # go to 8000 for values 600-1023
    endif
  endif
  1 servo
  drop

  2000 delay
repeat

You could extract some of the duplicated code into subroutines if you want, to make your code shorter. For example:

begin
  17 get_position compute_target 0 servo
  16 get_position compute_target 1 servo
  2000 delay
repeat

sub compute_target
  dup 300 less_than
  if
    drop
    4000 # go to 4000 for values 0-299
  else
    600 less_than
    if
      6000 # go to 6000 for values 300-599
    else
      8000 # go to 8000 for values 600-1023
    endif
  endif
  return

I haven’t tested that code above so there might be a few bugs.

I’m not sure why you have that delay or what you want to do with it.

–David

Thanks David
That did the trick, the last bit of code works great.
I needed to see the code in an optimized format to understand
How the subroutines worked in my example. I like the whole ideal
Of Forth programing, I can relate to it because I’m dyslexic so
Numbers before commands are all right by me. The 2 sec delay is
For an object to pass under a gate and then returning the servo
To neutral thus closing the gate.
Thanks
Gary