Led blink w/servo

Greetings,
I am attempting to actuate 2 servos and blink 2 led’s with a proximity sensor closed and turn off the led’s and reset the servo when the sensor is open. One quick look will display my lack of programming skills. I used a button press script and added a sequence to blink the led’s. The script runs but loops without returning the servos. I believe the led blink should be in an if-else statement. :confused:
Thank you in advance for help and direction to complete this.

# Uses WAIT_FOR_BUTTON_PRESS to allow a user to step through a sequence
# of positions on servo 4,5


begin
wait_for_button_closed_10ms
5000 4 servo
500 delay
5000 5 servo
500 delay
blink
wait_for_button_open_10ms
3000 4 servo
500 delay
3000 5 servo
500 delay
repeat


# Returns 1 if the button is pressed, 0 otherwise.
sub button
0 get_position 500 less_than
return

# wait for the button to be NOT pressed for at least 10 ms
sub wait_for_button_open_10ms
get_ms # put the current time on the stack
begin
# reset the time on the stack if it is pressed
button
if
drop get_ms
else
get_ms over minus 10 greater_than
if drop return endif
endif
repeat
# wait for the button to be pressed for at least 10 ms
sub wait_for_button_closed_10ms
get_ms
begin
# reset the time on the stack if it is pressed
button
if
get_ms over minus 10 greater_than
if drop return endif
else
drop get_ms
endif
repeat

# Sequence 3
sub blink
begin
  500 0 0 0 0 3987 0 
  0 0 0 4063 0 8000 
  0 0 0 0 0 0 
  0 0 0 0 0 frame_1..23 # Frame 0
  500 3986 frame_12 # Frame 1
  500 7941 frame_10 # Frame 2
  500 3986 frame_10 # Frame 3
	Wait_for_button_closed_10ms
repeat

sub frame_1..23
  23 servo
  22 servo
  21 servo
  20 servo
  19 servo
  18 servo
  17 servo
  16 servo
  15 servo
  14 servo
  13 servo
  12 servo
  11 servo
  10 servo
  9 servo
  8 servo
  7 servo
  6 servo
	  5 servo
  4 servo
  3 servo
  2 servo
  1 servo
  delay
  return

sub frame_12
  12 servo
  delay
  return

sub frame_10
  11 servo
  delay
  return

Hello.

I noticed two odd things about your script:

You are calling “wait_for_button_closed_10ms” at the end of the blink subroutine, AND you are calling it in your main loop. Do you really need to call it twice? How about you remove the one in the blink subroutine.

Your blink subroutine looks like it was generated from a sequence using the Sequence tab. As a result, the blink subroutine will modify every servo/output channel on the Maestro, including the LEDs and the servos. This is probably not what you want to do (or is it?), so I would recommend simplifying the blink routine to just the LEDs. Also, your blink subroutine has an infinite loop in it (begin…repeat) so once your program enters, it will never exit the blink subroutine. Try changing the blink subroutine to something simple like this:

sub blink 4000 10 servo 4000 12 servo delay 500 6000 10 servo 6000 12 servo delay 500 return

If that does not help, then please tell me what channels your LEDs are on and please tell me more specifically what the expected behavior of the Maestro is and what its actual behavior is, and include a copy of your new script.

I also recommend using the Maestro’s “Stop Script” and “Step Script” to debug your program.

–David

Hi David,
Thank you for the prompt reply. I revised the code per your suggestion and made a couple changes to the blink sub. It works but not the way I want it. I am attempting to actuate the servos and make the leds continue to blink alternately while the button is closed and return the servos and turn off the leds with the button open. What we have actuates the servos and turns on each led once.

[code] # Uses WAIT_FOR_BUTTON_PRESS to allow a user to step through a sequence

of positions on servo 4,5

begin
wait_for_button_closed_10ms
5000 4 servo
500 delay
5000 5 servo
500 delay
blink
wait_for_button_open_10ms
3000 4 servo
500 delay
3000 5 servo
500 delay
repeat

Returns 1 if the button is pressed, 0 otherwise.

sub button
0 get_position 500 less_than
return

wait for the button to be NOT pressed for at least 10 ms

sub wait_for_button_open_10ms
get_ms # put the current time on the stack
begin

reset the time on the stack if it is pressed

button
if
drop get_ms
else
get_ms over minus 10 greater_than
if drop return endif
endif
repeat

wait for the button to be pressed for at least 10 ms

sub wait_for_button_closed_10ms
get_ms
begin

reset the time on the stack if it is pressed

button
if
get_ms over minus 10 greater_than
if drop return endif
else
drop get_ms
endif
repeat

sub blink
6000 11 servo
500 delay
500 11 servo
6000 12 servo
500 delay
500 12 servo
500 delay
return

[/code]

Hello. Unfortunately, it is not trivial to do what you are trying to do with the Maestro, because you will need to two things concurrently:

[ol]
[li]Blink the LEDs in a loop.[/li]
[li]Check to see if the button is open, and if so break out of the loop.[/li][/ol]

A non-ideal way to do this would be to use something like this for the blink subroutine:

sub blink
  begin
    6000 11 servo
    500 delay
    button while  # break out of the loop if the button is open
    500 11 servo
    6000 12 servo
    500 delay 
    button while  # break out of the loop if the button is open
    500 12 servo
    500 delay
    button while  # break out of the loop if the button is open
  repeat
  return

This is not ideal because it doesn’t do any button debouncing like the wait_for_button_closed_10ms subroutine does, and it only checks the state of the button every 500 ms so it will lag noticeably. However, it should be a good starting point.

The Maestro programming would be much easier if you could just drive a line high to turn on the LEDs and take care of the blinking with a separate component such as a 555 timer.

–David

Hi David,
I am away for a few days but when I return I will revise the code.
I like the idea if the 555 timer. I will do that if the code results aren’t acceptable.
Thank you for the help. :smiley:
Regards,
Harold