Simple Modification of example script

Hi,

i am new in programming Pololu-Servocontroller and need little help modifying the example script
pololu.com/docs/0J40/6.c
Using multiple buttons or switches to control servos

I would like to add a simple routine which let the user switch the servo with two additional buttons to two additional positions as long as another analog-input gets an external triggersignal.
If there is no triggersignal at this input, the script should function as in the example script and the two additional Buttons should not be regarded.

May someone help me?

Thanks and greets,
Claas

Hello, Claas.

To modify the script to allow the user to activate two additional buttons with an external trigger, I recommend adding separate subroutines like the ones already given in the example for handling servo movements and detecting pushes from the additional buttons and your external trigger. The subroutine for detecting the external trigger could be basically the same as the routine for button_a that is given in the example, and you could check for that trigger with lines similar to those in the first code block of the example.

If you try making a script with that added functionality and have trouble, you could post it, and I would be happy to take a look at it.

-Amanda

Hi Amanda,

thanks for that!
I had the same thoughts and i already made a script with just two buttons and the triggerinput also as a button.
It works for a numer of trys, than i got an error (subroutine call under/overflow.)
Maybe one can do it using

3 get_position  # get the value of input 3 as a number from 0 to 1023
512 less_than   # test whether it is less than 512 -> 1 if true, 0 if false
if
  6000 5 servo   # this part is run when input3 < 512
else
  7000 5 servo   # this part is run when input3 >= 512
endif

channel 3 is inputsensor and instead of the servopositions in the following lines the routines looking for the buttons will run , so that the first two buttons will asked if the input is low and the last two buttons are regarded if input is high.

But i am not quite sure if and how that works.

The whole thing is to control a door-opener, the first two buttons are inside the door for opening and closing and the other two buttons are outside for opening and closing and are working only if a fingerprintsensor will give the correct signal to the inputsensor.
So my daughter will not need a key to get in and i dont need to change the doorlock every month due to lost keys in the school :wink:

Greetz,
Claas

This is the skript, working a few times, than stops with over/underflowerror:

# When the script is not doing anything else,
# this loop will listen for button presses.  When a button
# is pressed it runs the corresponding sequence.
# Sequence_d looks for button_e after triggered by button(Voltage)_d.


start:
begin
  button_a if sequence_a endif
button_d if sequence_d endif

repeat
 
# button_a is assigned to channel 5
# button_d is assigned to channel 3 and is Input for Audiotrigger.
# button_e is assigned to channel 4

# Servo is channel 8
 
sub button_a
  0 get_position 500 less_than
  return
 
sub button_d
  3 get_position 135 less_than
  return

# These subroutines each perform an arbitrary sequence
# of servo movements. Subroutine_d is looking for button _e 

sub sequence_a
  4000 8 servo 
  return
   
sub sequence_d
  begin
  button_e if sequence_e endif

repeat

sub button_e
   5 get_position 200 less_than
  return
 
sub sequence_e
  8000 8 servo 
  GOTO start

Thank you for posting your code. While I was stepping through it, I noticed that the counter for the subroutine levels used (located in the upper right-hand side under the Script tab) does not return to 0 after executing the goto start statement in sequence_e. It looks like sequence_d does not return due to sequence_e jumping straight to the start label instead of returning back to sequence_d.

I suggest you modify your code to have all subroutines return and to use a different control structure in sequence_d such as a while loop. You can find more information about using while loops under the Control structures heading in the Maestro Script Language Basics section of the Maestro user’s guide.

-Amanda

Hi Amanda,

thanks for your reply!
But I think i am not able to manage it - i am far away from understanding the whole thing and programming is not the thing i am usually doing. :blush:
Ist it possible for you to give me a more concrete solution?
No matter if not, i will look for another solution which is running out of the box than.

Many thanks,
Claas

I modified the following subroutines from your script to show one way to solve the overflow issue:

sub sequence_d
  begin
    button_e logical_not
  while                
    #while true continue loop
  repeat
  
  sequence_e
  return

There is a WHILE loop in sequence_d that continually checks if button_e is high before exiting the loop and continuing on to sequence_e.

sub sequence_e
  8000 8 servo
  return

In sequence_e the GOTO start statement was replaced with return, so the process can properly return to the calling code.

-Amanda

Hi Amanda,

thanks for your supply.
This works but i dont like to have the loop in sequence_d continually checking if button_e is high - it should only check if button_e is high as long as button_d sees my audiosignal.
I tried to modify the script, but again i didnt find a solution that works.

Greetz,
Claas

Could you post your modified code? What is the issue that you are encountering with it? If the issue is not an error, could you tell me what type of behavior you want the script to have and what type of behavior you are actually getting?

-Amanda

Hi Amanda,

sorry, I have been absent for a few days…
The thing is just like I mentioned before:

I like to use the skript similar to
pololu.com/docs/0J40/6.c
Using multiple buttons or switches to control servos

but with the following extension:

(To simplify it for my question i changed it to button_a and button_d which are listened to.)
Button_a is a real button, button_d is an (audio)-input.
If button a_is pressed a corresponding sequence_d should run on servo 8.
If the audioinput (button_d) is less 120, two other buttons (in my example just button_e) are asked if they are pressed and if so, start a coressponding sequence_e for servo 8.
The solution you have posted is looking for button_e endless after button_d is less 120 respektively until button_e is pressed.
But I like to have button e_ looked for only as long as button_d is less 120.
When the audiotrigger at button_d is greater 120, button_e shouldn`t be regarded anymore and the skript should return for listening to button_a and (audio)button_d again and so on.

I hope I made the whole thing understandable, I think my english is not that good…

Thank you for taking care of my problem!
Greetz,
Claas

This is my script trying to use my own solution but after a day I got an subroutine overflow-error:

# looking for buttons:
start:
begin
 button_a if sequence_a endif
 button_b if sequence_b endif
button_c if sequence_c endif
repeat

# button_a is assigned to channel 0 for opening indoor
# button_b is assigned to channel 1 for closing indoor
# button_c is assigned to channel 4 and is Input for Audiotrigger
# button_d is assigned to channel 2 opening outdoor regarded only as long as button_c is triggered.
# button_e is assigned to channel 3 closing outdoor regarded only as long as button_c is triggered.
# channel 6 is LED Green
#channel 7 is LED RED
# Servo is channel 8

sub button_a
  0 get_position 500 less_than
  return

sub button_b 
1 get_position 500 less_than
return


sub button_c
  4 get_position 120 less_than
  return

#Opening indoor: 8Servo 7RedLED 6GreenLED
sub sequence_a   
  6200 8 servo
  7000 7 servo
  2000 6 servo
3000 delay
6160 8 servo 
return

#closing indoor: 8Servo 7RedLED 6GreenLED

sub sequence_b 
4600 8 servo 
7000 6 servo
2000 7 servo
2000 delay
4670 8 servo
return

#  Subroutine_c s looking for button _d and _e after an audioinput on button_c is less 120.
# to make sure that this is the case only as long as audiosignal is  less 120 on input_c, i made button_f looking in this sequence also for input_4
 # (audio) and if signal is greater than 120, sequence f is making everything going to start.

sub sequence_c
begin
button_f if sequence_f endif
 button_d if sequence_d endif
 button_e if sequence_e endif
 repeat
return
sub button_f
  4 get_position 120 greater_than
  return


sub button_d
  2 get_position 200 less_than
  return

sub button_e
  3 get_position 200 less_than
  return

sub sequence_d  
#opening outdoor:8Servo 7RedLED 6GreenLED

7000 8 servo
2000 delay
6200 8 servo
  7000 7 servo
  2000 6 servo
3000 delay
6160 8 servo 
return

#closing outdoor: 8Servo 7RedLED 6GreenLED
sub sequence_e 
4600 8 servo 
7000 6 servo
2000 7 servo
2000 delay
4670 8 servo
return

sub sequence_f

goto start
return

Your explanation was helpful. I do not see any obvious problems with the logic in the latest code you posted, except for the use of a GOTO command in sequence_f. Again, the overflow error is caused by improper subroutine returns. You could try to remove sequence_f and instead return from sequence_c when button_f is triggered as shown in the code below:

sub sequence_c
   begin
      button_f if return endif
      button_d if sequence_d endif
      button_e if sequence_e endif
   repeat
   return

-Amanda

Hi Amanda,

perfect - that works fine!
Many thanks for your support!!

Greetz,
Claas