Multiple button control - beginners question

Hi

I’m new to this and have spent some time on the forums and google looking for an answer to my problem with no luck though I am sure it should be easy to resolve.

My setup is a micro maestro with channel 0 and 1 set as switch inputs and channel 2 connected to the servo. The project is for an automated turning targets setup for pistol shooting. The button on channel 0 runs a servo sequence that waits 3500ms, turns 90 degrees, waits 4000ms then turns back 90 degrees. This part of the code works fine.

The button on channel 1 needs to turn the servo 90 degrees so the targets face the competitors, then when the button is pressed again, to turn the targets back 90 ready for the steward to press the other button starting the shooting sequence. I just can’t figure out how to do this bit. I tried adapting the code in the samples without success.

Thanks for any assistance

# 4 second turning target control
# Wait for start button or face button
# After a 3.5 second delay move servo arm to face position
# After a 4 second delay move servo arm to edge position
# Return to the start of the sequence
# Face button available

# Servo Connected to Output 2
# Start button connected to Input 1 via pull up resistor
# Face button connected to Input 2 via pull up resistor


# 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.
begin
button_a if turner endif
button_b if face endif
repeat

# These subroutines each return 1 if the corresponding
# button is pressed, and return 0 otherwise.
# button_a is assigned to channel 0
# button_b is assigned to channel 1
sub button_a
0 get_position 500 less_than
return
sub button_b
1 get_position 500 less_than
return

sub turner
  4242 2 servo
  3500 delay
  7745  2 servo 4000 delay
  4242 2 servo
return

sub face
 # need help here!!
 7745 2 servo
 4242 2 servo
return

Hello.

It sounds like when the operator presses the button on channel 1, you want to move the servo to move from position 4242 to 7745 and wait. Then when the operator presses the button again, you want the servo to go back to position 4242.

Basically you just need to use a loop to wait for that second button press to happen. Something like this should work:

begin
  button_a if turner endif
  button_b if face endif
repeat

sub button_a
  0 get_position 500 less_than
  return
sub button_b
  1 get_position 500 less_than
  return

sub turner
  4242 2 servo
  3500 delay
  7745  2 servo 4000 delay
  4242 2 servo
  return

sub wait_for_button_b_release
  begin
    button_b while
  repeat
  end
  return
  
sub wait_for_button_b
  begin
    button_b logical_not while
  repeat
  end
  return
  
sub face
  7745 2 servo
  1000 delay   # helps debounce the button
  wait_for_button_b_release
  wait_for_button_b
  4242 2 servo
  1000 delay   # helps debounce the button
  return

I haven’t tested it so I’m not sure. Let me know how it goes!

–David

Hi David

Thanks very much. I just had to drop the 2 ‘end’ statements and it worked perfectly.

Many thanks, I’m going to spend some time looking at it so I can understand how you did it!

Regards

Graham