New to coding, want to check the validity

Hi Guys and Gals,

I’m very, very new to coding and programming and robotics in general. I’ve combed over the literature and youtube videos and several posts to work on a new project I’m trying to complete. I have a Maestro Mini 12 channel microcontroller and the SparkFun MP3 trigger. It is connected to 4 buttons (which have been properly connected to ground and pull up resistors), 4 servos, and 4 pins of the Maestro are used as outputs for the MP3 trigger. I would like to activate a sound and motion at the press of each of the 4 buttons. The system would not need to run any of the actions simultaneously.

Button 1 would activate Servo 1 and Trigger MP3 1.
Button 2 would activate Servo 2 and Trigger MP3 2.
Button 3 would activate Servo 3 and Trigger MP3 3.
Button 4 would activate Servo 4 and Trigger MP3 4.

After reading through the guides for the MP3 trigger and the Maestro, I’ve come up with the following code. Would this work? Secondly, after the subroutine triggering the sequence, would the additional “sub” be necessary or could/should it be removed?

As you can see, I’ve put a lot of notes in the code, just for my reference as I am very new to this, which will be removed before the code is compiled to a script.

# Set inputs to channels 0, 1, 2, and 3
# Set outputs to channels 8, 9, 10, and 11
# Servo 1 Chan 4, Servo 2 Chan 5, Servo 3 Chan 6, Servo 4 Chan 7

begin
  button_a if sequence_a endif
  button_b if sequence_b endif
  button_c if sequence_c endif
  button_d if sequence_d endif
repeat

sub button_a
  0 get_position 500 less_than
  return

sub button_b
  1 get_position 500 less_than
  return

sub button_c
  2 get_position 500 less_than
  return

sub button_d
  3 get_position 500 less_than
  return

sub sequence_a
  sub trigger_ascii
  0x31 0x54 serial_send_byte serial_send_byte # Triggers sound 1 for sequence a
  5 4 speed # Set speed of servo 1
  4000 4 servo 1000 delay # First position of servo 1
  0 4 speed # Set speed of servo 1
  6000 4 servo 500 delay # Second position of servo 1
  return

sub sequence_b
  sub trigger_ascii
  0x32 0x54 serial_send_byte serial_send_byte # Triggers sound 2 for sequence b
  10 5 speed # Set speed of servo 2
  8000 5 servo 1200 delay # First position of servo 1
  5 5 speed # Set speed of servo 2
  5000 5 servo 100 delay # Second position of servo 1
  return

sub sequence_c
  sub trigger_ascii
  0x33 0x54 serial_send_byte serial_send_byte # Triggers sound 3 for sequence c
  5 6 speed # Set speed of servo 3
  4000 6 servo 1000 delay # First position of servo 3
  0 6 speed # Set speed of servo 3
  6000 6 servo 500 delay # Second position of servo 3
  return

sub sequence_d
  sub trigger_ascii
  0x34 0x54 serial_send_byte serial_send_byte # Triggers sound 4 for sequence d
  10 7 speed # Set speed of servo 4
  8000 7 servo 1200 delay # First position of servo 4
  5 7 speed # Set speed of servo 4
  5000 7 servo 100 delay # Second position of servo 4
  return

Here’s an example of what I mean.

The original code:

sub sequence_a
  sub trigger_ascii
  0x31 0x54 serial_send_byte serial_send_byte # Triggers sound 1 for sequence a
  5 4 speed # Set speed of servo 1
  4000 4 servo 1000 delay # First position of servo 1
  0 4 speed # Set speed of servo 1
  6000 4 servo 500 delay # Second position of servo 1
  return

Code with additional sub removed:

sub sequence_a
  trigger_ascii
  0x31 0x54 serial_send_byte serial_send_byte # Triggers sound 1 for sequence a
  5 4 speed # Set speed of servo 1
  4000 4 servo 1000 delay # First position of servo 1
  0 4 speed # Set speed of servo 1
  6000 4 servo 500 delay # Second position of servo 1
  return

Thanks! You guys are awesome!

Hello.

I briefly looked at your code and noticed there are multiple declarations of trigger_ascii. When I try to load your script onto a Maestro using the Maestro Control Center, I get an error stating that the subroutine has already been defined. To fix the error, you would need to give a unique name to each subroutine (e.g. trigger_ascii_a, trigger_ascii_b, etc). Other than that you would have to test the code yourself to see if it outputs the desired behavior in your setup.

I’m not sure if you are planning to add more to the “sequence” subroutines, but in your current code you should not need to nest subroutines within another since you are essentially giving the same subroutine multiple names, which serves no purpose. I recommend removing sub trigger_ascii in each “sequence” subroutine. (The code should still run the same without it.)

- Amanda