Strange issue with script

I am trying to write a script that controls a servo based on the input from a sensor.

The servo is supposed to slowly decrease until the sensor shows a specific voltage. then it should hold that position. if the voltage dips it should continue decreasing.

what happens is that it all works. it will slowly decrease til the voltage is reached and holds the position. when the voltage drops instead of continuing to decrease until the voltage increases it suddenly reports nothing for the position of the servo and so the minus puts 100 instead of the servo position minus 100. The servo will not have moved.

stepping through the program shows that the correct position is being put on the stack then it is replaced with nothing when the minus command is passed. so the next number on the stack is 100.

restarting and going through the sequence repeats.
5 is an analog voltage sensor 0 to 5 volts
0 is the servo

100
6000 0 servo #sets the starting position
begin
5 get_position 800 less_than
	if
	  0 get_position minus 100  #take the servo position subtract 100 returns a negative value
           over
           negate #makes the servo position positive
         0 servo
       else  
          0 get_position dup
          0 servo
       endif
   rot
   drop
repeat

Hello.

I moved your post to the servo controllers and servos support category since it makes more sense here.

The quickest way to make your script work like that would probably be to move your rot command to before the else.

By the way, it seems like your script is more complicated than it needs to be, and I suspect that is because of how you are using the minus command. Right now line 6 is calculating 100 minus the servo position instead of the servo position minus 100 (hence why the result is negative) To take the servo position and subtract 100 correctly, that line should be written like this:

0 get_position 100 minus

So, if you want to try to simplify your script, that is the best place to start. Keep in mind though, you will have to change other parts of your script too since a lot of your current code is being used as a workaround.

- Patrick

well I made progress in getting my project to do what I want.

code is as such:

100 #this does nothing
top:
6000 0 servo # Move servo to middle position
begin
  button # is button on or off
    if #on proceed with program
      too_low # Check if throttle is too low
      too_high # check if throttle is too high
    else #off keep checking for on and reset the servo if it was moved.
      goto top
    endif
repeat

sub too_low 
  5 get_position 700 less_than #check the voltage is it below X
    if 0 get_position minus 100#if voltage is too low get position of servo and subtract 100
      over # not sure how this helps
      negate
      0 servo # move the servo to the new position
      100 delay # wait
    else  #if voltage is fine do nothing
      goto skip
    endif
swap # not sure why but the program won't 
drop # work without these commands
skip:
return

sub too_high
  5 get_position 900 greater_than #check the voltage is it above X
    if 0 get_position plus 100 #if voltage is too high get position of servo and add 100
      over # not sure how this helps
      0 servo #move servo to new position
      100 delay #wait
    else #if voltage is fine do nothing
      goto skip2
    endif
swap # not sure why but the program won't 
drop # work without these commands
skip2:
return

sub button
  2 get_position 500 less_than
return

basically it’s a governor for a small DC generator powered by a gasoline engine.
It’s gets the voltage through a sense and tries to keep it in between say 20 and 24v.
it works on the bench with my little mockup. Now I just need to see if it will work when connected to the real thing.