Arduino - Maestro 24ch

hi guys,
as you can see, im posting a lot…
its my last days for a big project based on Maestro, and im trying to put everything together.

so…
i built a remote control based on arduino, its connected to the maestro and used for calling sub routines.
it works this way:
the arduino gets numbers from a keypad, the numbers are “banked” in the arduino code to know which sub routine it should call.
the arduino is calling the sub routine from the maestro, and printing the subroutine number on an LCD.

the problem is that i could call the first 21 sub routines, and the whole thing worked well. the 21 sub routine start runing and then the maestro shut down (all motors cut off, like when doing “apply settings”.

its long codes, but i just cant find the problem, and will admire you forever if you will help me here…

this is the maestro code:

### Sequence subroutines: ###

# 1 up left
sub 1_up_left
  500 7581 7930 6003 5964 6794 5259 
  5768 6023 0 0 0 0 frame_0..11 # up left
  500 7307 8000 7217 4814 7882 7471 frame_0_1_3..5_7 # up left 1
  500 7581 5807 7637 3968 6962 frame_0_3..5_7 # up left 2
  500 7930 7549 5964 6794 5259 6434 
  6023 frame_1..7 # up left rot right
  500 4770 4985 frame_2_6 # up left rot left
  quit
# 2 up center
sub 2_up_center
  500 7581 6190 6003 5964 6794 5259 
  5768 6023 0 0 0 0 frame_0..11 # center up
  500 7307 7217 4814 7882 7471 frame_0_3..5_7 # center up1
  500 7581 5964 8041 4946 6962 frame_0_3..5_7 # center up 2
  500 6173 7549 6794 5259 6434 6023 frame_1_2_4..7 # center up rot right
  500 4770 4985 frame_2_6 # center up rot left
  quit
# 3 up right
sub 3_up_right
  500 7581 4416 6003 5964 6794 5259 
  5768 6023 0 0 0 0 frame_0..11 # up right
  500 7307 7217 4814 7882 7471 frame_0_3..5_7 # up right 1
  500 7581 5807 7637 3968 6962 frame_0_3..5_7 # up right 2
  500 7549 5964 6794 5259 6434 6023 frame_2..7 # up right rot right
  500 4770 4985 frame_2_6 # up right rot left
  quit
# 4 left center
sub 4_left_center
  500 6391 8000 6003 5964 6831 4829 
  5768 6747 0 0 0 0 frame_0..11 # left center
  500 6571 4411 8078 7314 frame_3..5_7 # left center 1
  500 5964 8041 4320 7099 frame_3..5_7 # left center 2
  500 7549 6794 5259 6434 6023 frame_2_4..7 # left center rot right
  500 7930 4770 4985 frame_1_2_6 # left center rot left
  quit
# 5 center center
sub 5_center_center
  3000 6391 6190 6003 5964 6794 5259 
  5768 6023 0 0 0 0 frame_0..11 # center center
  3000 6208 6571 4411 8078 7314 frame_1_3..5_7 # center center 1
  3000 6190 5964 8041 4946 6962 frame_1_3..5_7 # center center 2
  3000 6173 7549 6794 5259 6434 6023 frame_1_2_4..7 # center center rot right
  3000 4770 4985 frame_2_6 # center center rot left
  quit
# 6 right center
sub 6_right_center
  500 6391 4416 6003 5964 6794 5259 
  5768 6023 0 0 0 0 frame_0..11 # right center
  500 4433 6042 4008 7412 7314 frame_1_3..5_7 # right center 1
  500 4416 5964 8041 4946 7099 frame_1_3..5_7 # right center 2
  500 7549 6794 5259 6434 6023 frame_2_4..7 # right center rot right
  500 4770 4985 frame_2_6 # right center rot left
  quit
# 7 down left
sub 7_down_left
  500 5353 8000 6003 5964 6794 5259 
  5768 6590 0 0 0 0 frame_0..11 # down left
  500 6825 3531 8548 7628 frame_3..5_7 # down left 1
  500 4966 8041 3968 4300 frame_3..5_7 # down left 2
  500 5312 7549 5964 6794 5259 6434 
  6023 frame_0_2..7 # down left rot right
  500 7930 4770 4985 frame_1_2_6 # down left rot left
  quit
# 8 down center
sub 8_down_center
  500 5353 6190 6003 5964 6794 5259 
  5768 6590 0 0 0 0 frame_0..11 # center down
  500 6825 3531 8548 7628 frame_3..5_7 # center down 1
  500 4966 8041 3968 4300 frame_3..5_7 # center down 2
  500 5312 6208 7549 5964 6794 5259 
  6434 6023 frame_0..7 # center down rot right
  500 6173 4770 4985 frame_1_2_6 # center down rot left
  quit
# 9 down right
sub 9_down_right
  500 5353 4416 6003 5964 6794 5259 
  5768 6590 0 0 0 0 frame_0..11 # down right
  500 6825 3531 8548 7628 frame_3..5_7 # down right 1
  500 4966 8041 3968 4300 frame_3..5_7 # down right 2
  500 5312 7549 5964 6794 5259 6434 
  6023 frame_0_2..7 # down right rot right
  500 4770 4985 frame_2_6 # down right rot left
  quit
# fast sequence (manoa 10-30)
sub fast_sequence_manoa_1030
  1500 6186 5407 6023 7060 4594 7295 
  5827 4848 0 0 0 0 frame_0..11 # frame0
  2500 5366 7860 4418 7647 4155 7451 
  6590 7628 frame_0..7 # Frame 1
  3700 7553 4416 4281 4379 7637 6355 
  6238 7862 frame_0..7 # Frame 2
  2300 5366 4105 7158 4045 7804 5827 
  7491 frame_0_2..7 # Frame 3
  1700 6296 5129 5749 5455 7087 4750 
  6649 7021 frame_0..7 # Frame 4
  2500 5312 5846 6903 3421 8156 5866 
  8000 frame_0_2..7 # Frame 5
  3400 7594 5866 4437 8077 4282 4633 frame_0_2..5_7 # Frame 6
  quit
# fast tip trank (speed10-30)
sub fast_tip_trank_speed1030
  600 5503 6190 6003 6903 4558 6669 
  5768 6023 0 0 0 0 frame_0..11 # frame0
  800 5872 6016 5318 6590 5291 4907 
  5161 4907 frame_0..7 # Frame 1
  700 5548 5259 5984 7354 frame_4..7 # Frame 2
  700 6492 5690 7080 5064 frame_2_5..7 # Frame 3
  700 6336 6171 4555 7491 4574 frame_3..7 # Frame 4
  900 5905 7412 frame_6_7 # Frame 5
  800 5240 6238 4692 5024 frame_2_3_6_7 # Frame 6
  1200 7060 6532 4888 7256 4555 7921 frame_2..7 # Frame 7
  900 6355 5083 frame_6_7 # Frame 8
  500 5380 6068 5729 6336 5621 6121 
  6825 6806 frame_0..7 # Frame 9
  1000 6225 5064 7099 4594 6786 4535 
  7745 frame_1..7 # Frame 10
  1000 5905 5438 6629 5729 5240 frame_2_4..7 # Frame 11
  quit
# gravity up left
sub gravity_up_left
  2800 5312 6068 5846 7491 5658 6512 
  5827 5944 0 0 0 0 frame_0..11 # Frame 0
  5800 7813 4416 8000 4118 6747 5866 frame_0_1_3..5_7 # Frame 1
  2800 5735 5442 7256 5144 5925 5807 
  6218 frame_0_1_3..7 # Frame 2
  quit
# gravity up right
sub gravity_up_right
  3400 5312 6068 5846 7491 5658 6512 
  5827 5944 0 0 0 0 frame_0..11 # Frame 0
  5100 8018 8000 5827 8000 4155 5788 
  6649 frame_0..4_6_7 # Frame 1
  2500 5503 5633 6766 4778 6238 5240 frame_0_1_3..5_7 # Frame 2
  quit
# homing
sub homing
  1000 5954 6190 6003 6903 4558 6669 
  5768 6023 0 0 0 0 frame_0..11 # homing1
  1000 6434 6512 6434 frame_2_5_6 # homing2
  500 5626 frame_0 # homing3
  1500 5571 5298 5083 frame_0_2_6 # homing4
  1000 5503 6003 6669 5768 frame_0_2_5_6 # homing5
  1000 5846 frame_5 # homing6
  1000 5845 4778 6943 frame_0_4_5 # homing7
  1000 5571 5298 4558 6512 5083 frame_0_2_4..6 # homing8
  1000 5312 6023 5846 4228 7334 6003 
  7001 frame_0_2..7 # homing9
  1000 7451 7080 5670 frame_5..7 # homing10
  1000 5503 6003 6903 4558 6669 5768 
  6023 frame_0_2..7 # homing11
  quit
# meeting with yael
sub meeting_with_yael
  2200 5954 6190 6003 6903 4558 6669 
  5768 6023 0 0 0 0 frame_0..11 # Frame 0
  2100 5312 7099 5841 frame_0_3_4 # Frame 1
  3000 6864 6281 4985 5005 frame_3..5_7 # Frame 2
  2600 7275 frame_7 # Frame 3
  1900 7021 6982 7530 frame_5..7 # Frame 4
  2100 5220 7530 4574 frame_2_5_6 # Frame 5
  2000 6277 5401 5103 5690 5455 frame_2_4..7 # Frame 6
  2200 5827 7275 frame_2_7 # Frame 7
  2900 6023 7271 frame_3_4 # Frame 8
  2200 5161 frame_7 # Frame 9
  2500 5279 5905 4966 7119 frame_2_3_6_7 # Frame 10
  1800 6186 5407 6023 7060 4594 6512 
  5827 7823 frame_0..7 # Frame 11
  2800 delay # Frame 12
  4000 5494 frame_7 # Frame 18
  4000 7295 4848 frame_5_7 # Frame 19
  quit
# playing with yael
sub playing_with_yael
  3400 6186 5407 6023 7060 4594 7295 
  5827 4848 0 0 0 0 frame_0..11 # frame 0
  4000 5366 4416 7432 7882 3971 8234 
  4966 7158 frame_0..7 # Frame 1
  5500 6200 5233 5905 5612 6831 5064 
  7314 5044 frame_0..7 # Frame 2
  4600 5312 7930 5161 4868 4711 6982 frame_0..2_5..7 # Frame 3
  5000 4418 6610 3788 7921 5944 7745 frame_2..7 # Frame 4
  6100 7294 6729 4202 4731 7161 5220 
  5279 5455 frame_0..7 # Frame 5
  3600 5448 4555 frame_0_1 # Frame 6
  5000 6903 7138 3531 8352 4966 7001 frame_2..7 # Frame 7
  3300 5899 5581 5827 5357 6574 5612 frame_0..5 # Frame 8
  3300 5435 5455 7087 4750 6669 5514 frame_2..7 # Frame 9
  3300 6062 5377 5914 5338 5161 6629 frame_2..7 # Frame 10
  3300 5318 4887 5658 4868 6943 6884 frame_2..7 # Frame 11
  3900 7581 5303 6355 4711 7161 6943 
  5416 7432 frame_0..7 # Frame 12
  4000 5216 6121 5435 6317 5024 5592 
  6982 frame_1..7 # Frame 13
  quit
# playing with yael 2
sub playing_with_yael_2
  3400 6186 7130 6023 7060 4594 7295 
  5827 4848 0 0 0 0 frame_0..11 # 1 see
  4000 5366 4416 7432 7882 3971 8234 
  4966 7158 frame_0..7 # 2 shay
  4000 7182 frame_1 # 3 shay left
  4600 5312 7930 5161 5612 6831 4868 
  4711 6982 frame_0..7 # 4 a
  5500 6200 6747 5905 5064 7314 5044 frame_0..2_5..7 # 5 b
  4000 4124 5533 8000 frame_2_5_6 # 6 rot right
  4000 5905 5064 7314 frame_2_5_6 # 7 rot left
  5000 5448 4972 7119 4924 8117 6453 frame_0_1_3..5_7 # 8 center yael a
  4000 6200 5612 6831 5064 5044 frame_0_3..5_7 # 9 b
  6100 7294 6729 4202 4731 7161 5220 
  5279 5455 frame_0..7 # 10 c
  3300 5899 5581 5827 5357 6574 5612 
  4966 7001 frame_0..7 # 11 d
  3300 5435 5455 7087 4750 6669 5514 frame_2..7 # 12 e
  3300 6062 5377 5914 5338 5161 6629 frame_2..7 # 13 f
  3300 5318 4887 5658 4868 6943 6884 frame_2..7 # 14 g
  3900 7581 5303 6355 4711 7161 6943 
  5416 7432 frame_0..7 # 15 h
  4000 5216 6121 5435 6317 5024 5592 
  6982 frame_1..7 # 16 i
  3600 5448 4555 4202 4731 7161 5220 
  5279 5455 frame_0..7 # 17 turn right
  5000 6903 7138 3531 8352 4966 7001 frame_2..7 # 18 shay right
  5000 4972 5905 7119 4924 8117 7314 
  6453 frame_1..7 # 19 end
  quit
# sarching down
sub sarching_down
  2200 5954 6190 6003 6903 4558 6669 
  5768 6023 0 0 0 0 frame_0..11 # Frame 0
  2100 5312 7099 5841 frame_0_3_4 # Frame 1
  2400 6864 6281 4985 5005 frame_3..5_7 # Frame 2
  2600 7275 frame_7 # Frame 3
  1900 7021 6982 7530 frame_5..7 # Frame 4
  2100 5220 7530 4574 frame_2_5_6 # Frame 5
  2000 6277 5401 5103 5690 5455 frame_2_4..7 # Frame 6
  2200 5827 7275 frame_2_7 # Frame 7
  2900 6023 7271 frame_3_4 # Frame 8
  2200 5161 frame_7 # Frame 9
  2500 5279 5905 4966 7119 frame_2_3_6_7 # Frame 10
  1800 5407 6023 6355 4594 6512 5827 
  7823 frame_1..7 # Frame 11
  quit
# serching front
sub serching_front
  3400 5312 6068 5846 7491 5658 6512 
  5827 5944 0 0 0 0 frame_0..11 # Frame 0
  3500 6610 6155 5788 5416 7454 5455 
  5729 7647 frame_0..7 # Frame 1
  3400 6364 7756 7549 4750 7747 6708 
  5964 6708 frame_0..7 # Frame 2
  5600 4572 4633 4946 7527 6199 4946 
  7060 frame_1..7 # Frame 3
  2600 5633 5788 4927 6977 5612 5768 
  7158 frame_1..7 # Frame 4
  2200 5768 4848 7307 6395 6101 6923 frame_2..7 # Frame 5
  4700 5312 5894 5827 8000 4814 9017 
  5807 8000 frame_0..7 # Frame 6
  3700 5831 5807 7706 5291 6277 5455 frame_0_2..5_7 # Frame 7
  quit
# serching up
sub serching_up
  3800 5312 6068 5846 7491 5658 6512 
  5827 5944 0 0 0 0 frame_0..11 # Frame 0
  4100 7581 8000 4613 4887 7051 5142 
  5435 7686 frame_0..7 # Frame 1
  4200 4729 6375 4653 7234 5416 6845 frame_1..6 # Frame 2
  3200 7393 5690 6501 4398 5592 7099 frame_2..7 # Frame 3
  3600 5251 5827 5514 6941 5024 7373 frame_1..5_7 # Frame 6
  3400 5312 5709 6923 5841 6982 5631 
  5103 frame_0_2..7 # Frame 7
  quit
# siluete
sub siluete
  500 5954 6190 6003 6903 4558 6669 
  5768 6023 0 0 0 0 frame_0..11 # Frame 6
  500 5790 4816 4105 5621 5298 5866 
  7197 frame_0_1_3..7 # Frame 0
  500 5954 4955 4633 6721 3968 5768 
  7158 frame_0_1_3..7 # Frame 1
  500 7881 4972 5925 4496 7784 8000 frame_0..4_7 # Frame 2
  500 7354 4924 frame_2_4 # Frame 3
  500 5790 4816 6003 4105 5621 5298 
  5866 7197 frame_0..7 # Frame 4
  500 5954 6190 6903 4558 6669 5768 
  6023 frame_0_1_3..7 # Frame 5
  quit
# thermic look
sub thermic_look
  500 5735 6190 6003 6903 3825 8548 
  5572 7862 0 0 0 0 frame_0..11 # to the mouth- next script
  500 6993 6023 6806 4374 6786 5749 
  6121 frame_0_2..7 # gravity
  500 5353 7982 6003 6825 6244 6551 
  5768 frame_0..6 # back to the bones
  quit

sub frame_0..11
  11 servo
  10 servo
  9 servo
  8 servo
  7 servo
  6 servo
  5 servo
  4 servo
  3 servo
  2 servo
  1 servo
  0 servo
  delay
  return

sub frame_0_1_3..5_7
  7 servo
  5 servo
  4 servo
  3 servo
  1 servo
  0 servo
  delay
  return

sub frame_0_3..5_7
  7 servo
  5 servo
  4 servo
  3 servo
  0 servo
  delay
  return

sub frame_1..7
  7 servo
  6 servo
  5 servo
  4 servo
  3 servo
  2 servo
  1 servo
  delay
  return

sub frame_2_6
  6 servo
  2 servo
  delay
  return

sub frame_1_2_4..7
  7 servo
  6 servo
  5 servo
  4 servo
  2 servo
  1 servo
  delay
  return

sub frame_2..7
  7 servo
  6 servo
  5 servo
  4 servo
  3 servo
  2 servo
  delay
  return

sub frame_3..5_7
  7 servo
  5 servo
  4 servo
  3 servo
  delay
  return

sub frame_2_4..7
  7 servo
  6 servo
  5 servo
  4 servo
  2 servo
  delay
  return

sub frame_1_2_6
  6 servo
  2 servo
  1 servo
  delay
  return

sub frame_1_3..5_7
  7 servo
  5 servo
  4 servo
  3 servo
  1 servo
  delay
  return

sub frame_0_2..7
  7 servo
  6 servo
  5 servo
  4 servo
  3 servo
  2 servo
  0 servo
  delay
  return

sub frame_0..7
  7 servo
  6 servo
  5 servo
  4 servo
  3 servo
  2 servo
  1 servo
  0 servo
  delay
  return

sub frame_0_2..5_7
  7 servo
  5 servo
  4 servo
  3 servo
  2 servo
  0 servo
  delay
  return

sub frame_4..7
  7 servo
  6 servo
  5 servo
  4 servo
  delay
  return

sub frame_2_5..7
  7 servo
  6 servo
  5 servo
  2 servo
  delay
  return

sub frame_3..7
  7 servo
  6 servo
  5 servo
  4 servo
  3 servo
  delay
  return

sub frame_6_7
  7 servo
  6 servo
  delay
  return

sub frame_2_3_6_7
  7 servo
  6 servo
  3 servo
  2 servo
  delay
  return

sub frame_0_1_3..7
  7 servo
  6 servo
  5 servo
  4 servo
  3 servo
  1 servo
  0 servo
  delay
  return

sub frame_0..4_6_7
  7 servo
  6 servo
  4 servo
  3 servo
  2 servo
  1 servo
  0 servo
  delay
  return

sub frame_2_5_6
  6 servo
  5 servo
  2 servo
  delay
  return

sub frame_0
  0 servo
  delay
  return

sub frame_0_2_6
  6 servo
  2 servo
  0 servo
  delay
  return

sub frame_0_2_5_6
  6 servo
  5 servo
  2 servo
  0 servo
  delay
  return

sub frame_5
  5 servo
  delay
  return

sub frame_0_4_5
  5 servo
  4 servo
  0 servo
  delay
  return

sub frame_0_2_4..6
  6 servo
  5 servo
  4 servo
  2 servo
  0 servo
  delay
  return

sub frame_5..7
  7 servo
  6 servo
  5 servo
  delay
  return

sub frame_0_3_4
  4 servo
  3 servo
  0 servo
  delay
  return

sub frame_7
  7 servo
  delay
  return

sub frame_2_7
  7 servo
  2 servo
  delay
  return

sub frame_3_4
  4 servo
  3 servo
  delay
  return

sub frame_5_7
  7 servo
  5 servo
  delay
  return

sub frame_0..2_5..7
  7 servo
  6 servo
  5 servo
  2 servo
  1 servo
  0 servo
  delay
  return

sub frame_0_1
  1 servo
  0 servo
  delay
  return

sub frame_0..5
  5 servo
  4 servo
  3 servo
  2 servo
  1 servo
  0 servo
  delay
  return

sub frame_1
  1 servo
  delay
  return

sub frame_1..6
  6 servo
  5 servo
  4 servo
  3 servo
  2 servo
  1 servo
  delay
  return

sub frame_1..5_7
  7 servo
  5 servo
  4 servo
  3 servo
  2 servo
  1 servo
  delay
  return

sub frame_0..4_7
  7 servo
  4 servo
  3 servo
  2 servo
  1 servo
  0 servo
  delay
  return

sub frame_2_4
  4 servo
  2 servo
  delay
  return

sub frame_0..6
  6 servo
  5 servo
  4 servo
  3 servo
  2 servo
  1 servo
  0 servo
  delay
  return

and this is the arduino code:

/*+++ -----------------------------------

 ========= keypad_to_mestro_v8 =============


change log:
-----------------------------
+ dont use QueueList for saving codes
+ display detailed status messages on lcd
+ limit queue size to 2


----------------------------------------*/
#include <Keypad.h>
#include <ctype.h> 
#include <LiquidCrystal.h>
#define DEVICE_1_NUMBER  1
#define DEVICE_2_NUMBER  2
#define DEL_LAST_CODE   -2
#define INVALID_CODE   250
#define BANK_SIZE      100
unsigned char buf[4]; 

boolean info_changed = true;

// =========================================== keypad definitions ==============================================================
const byte ROWS = 4; //four rows
const byte COLS = 4; //four columns
char keys[ROWS][COLS] = {
  {'1','2','3','A'},
  {'4','5','6','B'},
  {'7','8','9','C'},
  {'*','0','#','D'}
};
byte rowPins[ROWS] = {29, 25, 27, 23}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {37,35,33,31}; //connect to the column pinouts of the keypad


byte rowPins_b[ROWS] = {28,26,24,22}; //connect to the row pinouts of the keypad
byte colPins_b[COLS] = {36,34,32,30}; //connect to the column pinouts of the keypad


Keypad keypad_a = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS ); 
Keypad keypad_b = Keypad( makeKeymap(keys), rowPins_b, colPins_b, ROWS, COLS ); 


// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(53, 51, 49, 47, 45, 43);
// ============================================================================================================================


#define CMD_LIMIT  6

typedef struct {
  int key_seq_counter;
  int bank;
  int seq_idx;
  unsigned int code_current;
  unsigned int code_standby;
  unsigned int symbol_current;      // save the code symbol (like A4, B32, etc.) packed in 16 bytes
  unsigned int symbol_standby;
  int queue_size;
  boolean is_running;
  boolean is_loop;
} Device_info;

Device_info devices[2];



byte codes_a[4][BANK_SIZE];
byte codes_b[4][BANK_SIZE];

/*
int **codes_a;
int **codes_b;
*/







void setup_command_banks() {
  int i;
  for(i = 0; i < BANK_SIZE; i++) {
    codes_a[0][i] = INVALID_CODE;
  }
  for(i = 0; i < BANK_SIZE; i++) {
    codes_a[1][i] = INVALID_CODE;
  }
  for(i = 0; i < BANK_SIZE; i++) {
    codes_a[2][i] = INVALID_CODE;
  }
  for(i = 0; i < BANK_SIZE; i++) {
    codes_a[3][i] = INVALID_CODE;
  }
  
  for(i = 0; i < BANK_SIZE; i++) {
    codes_b[0][i] = INVALID_CODE;
  }
  for(i = 0; i < BANK_SIZE; i++) {
    codes_b[1][i] = INVALID_CODE;
  }
  for(i = 0; i < BANK_SIZE; i++) {
    codes_b[2][i] = INVALID_CODE;
  }
  for(i = 0; i < BANK_SIZE; i++) {
    codes_b[3][i] = INVALID_CODE;
  }
  
  
  // sequences for the 'A' matrix
  // ----------------------------------------------- bank A codes
  codes_a[0][1] = 0;
  codes_a[0][2] = 1;
  codes_a[0][3] = 2;
  codes_a[0][4] = 3;
  codes_a[0][5] = 4;
  codes_a[0][6] = 5;
  codes_a[0][7] = 6;
  codes_a[0][8] = 7;
  codes_a[0][9] = 8;
  codes_a[0][10] = 9;
  codes_a[0][11] = 10;
  codes_a[0][12] = 11;
  codes_a[0][13] = 12;
  codes_a[0][14] = 13;
  codes_a[0][15] = 14;
  codes_a[0][16] = 15;
  codes_a[0][17] = 16;
  codes_a[0][18] = 17;
  codes_a[0][19] = 18;
  codes_a[0][20] = 19;
  codes_a[0][21] = 20;
  codes_a[0][22] = 21;
  // etc...  
  
  
  codes_a[1][1] = 0;
  codes_a[1][2] = 1;
  codes_a[1][3] = 2;
  codes_a[1][4] = 3;
  codes_a[1][5] = 4;
  codes_a[1][6] = 5;
  codes_a[1][7] = 6;
  codes_a[1][8] = 7;
  codes_a[1][9] = 8;
  codes_a[1][10] = 11;
  codes_a[1][11] = 12;
  codes_a[1][12] = 25;
  // etc...  
  
  // bank C codes
  codes_a[2][1] = 13;
  codes_a[2][2] = 17;
  codes_a[2][3] = 18;
  codes_a[2][4] = 23;
  codes_a[2][5] = 26;
  codes_a[2][6] = 27;
  codes_a[2][7] = 28;

  // etc...  
  
  // bank D codes
  codes_a[3][1] = 9;
  codes_a[3][2] = 10;
  
  
  
  
  
  // -------------------------------------------------- bank B codes
   // sequences for the 'B' matrix
  codes_b[0][1] = 35;
  codes_b[0][2] = 34;
  codes_b[0][3] = 59;
  codes_b[0][4] = 66;
  codes_b[0][5] = 8;
 // etc...
 
  codes_b[1][1] = 9;
  codes_b[1][2] = 17;
  codes_b[1][16] = 0;
  codes_b[1][3] = 65;
  // etc...  
  
  // bank C codes
  codes_b[2][13] = 0;
  codes_b[2][2] = 22;
  codes_b[2][3] = 27;
  codes_b[2][4] = 36;
  // etc...  
  
  // bank D codes
  codes_b[3][22] = 55;
  codes_b[3][2] = 42;
  codes_b[3][99] = 1;
  
  codes_b[3][23] = 111;
  // etc...  
}


void init_device(int which) {
  devices[which-1].key_seq_counter = 0;
  devices[which-1].bank = INVALID_CODE;
  devices[which-1].seq_idx = 0;
  devices[which-1].code_current = 0xffff;
  devices[which-1].code_standby = 0xffff;
  devices[which-1].symbol_current = 0xffff;
  devices[which-1].symbol_standby = 0xffff;
  devices[which-1].is_running = false;
  devices[which-1].is_loop = false;
}

void init_device_key_process(int which) {
  devices[which-1].key_seq_counter = 0;
  devices[which-1].bank = INVALID_CODE;
  devices[which-1].seq_idx = 0;
}

void flash_led(int times, int d) {
  int del = d * 10;
  boolean no_delay = false;
  if(times == 0) {
    times = 1;
    no_delay = true;
  }
  for(int  i = 0; i < times; i++) {
    digitalWrite(13, HIGH);
    delay(del);
    digitalWrite(13, LOW);
    if(!no_delay) {
      delay(del);
    }
  }
}

unsigned long next_release = 0;


void setup() {
  char key;
  int tt = 0;
  
  // set up the LCD's number of columns and rows: 
  lcd.begin(20, 4);
  
  pinMode(19, OUTPUT);
  // initialize the serial communication:
  Serial.begin(9600);
  setup_command_banks();
  
  /*
  Device_info device_inf = devices[0];
  for(int t = 0; t < 5; t++) {
    device_inf.key_seq_counter++;
      //device_inf.cmd_buffer[device_inf.key_seq_counter] = key;
    Serial.print("## ");
    Serial.println(device_inf.key_seq_counter, DEC);
  }
  */
  
  // initial matrix test 
  /*
  while(1) {
    int read = 0;
    int k1 = keypad_a.getKey();
    while(k1 != NO_KEY) {
      Serial.write(k1);
      k1 = keypad_a.getKey();
      read++;
    }
    if(read > 0)
      Serial.println("");
    
    read = 0;
    int k2 = keypad_b.getKey();
    while(k2 != NO_KEY) {
      Serial.write(k2);
      k2 = keypad_b.getKey();
      read++;
    }
    if(read > 0)
      Serial.println("");
    
  }
  */
  init_device(1);
  init_device(2);
  
  
  digitalWrite(19, HIGH);
  delay(500);
  digitalWrite(19, LOW);
}


void trigger_seq(int device, int num) {
  /*
  // send a "start sequence" command to the Maestro controller (polulu protocol)
  buf[0] = 0xAA;
  buf[1] = (device == 1) ? DEVICE_1_NUMBER : DEVICE_2_NUMBER;  
  buf[2] = 0x27;
  buf[3] = num & 0xff; // number of the sequence to trigger
  */
  
  // send a "start sequence" command to the Maestro controller (compact protocol)
  buf[0] = 0xA7;
  buf[1] = num;  // number of the sequence to trigger
  
  
  devices[device-1].is_running = true;
  if(device == 1) {
    Serial.write(buf, 2); 
  }
  else if(device == 2) {
    Serial1.write(buf, 2); 
  }
}


boolean is_script_running(int device) {
  
  /*  polulu protocol ---------
  buf[0] = 0xAA;
  buf[1] = (device == 1) ? DEVICE_1_NUMBER : DEVICE_2_NUMBER;  
  buf[2] = 0x2E;
  Serial.write(buf, 3); 
  */
  
  if(device == 1) {
    // send an "is_running" command to the Maestro controller (compact protocol)
    Serial.write(0xAE);
    
    int count = 0;
    while(Serial.available() == 0) {
      // while no reply from the controller, wait and count for timeout
      delay(1);
      count++;
      if(count == 12)  //timeout
        return(false);
    }  
    // get reply
    byte res = Serial.read();
    if(res == 0) {
      return true;
    }
    else {
      return false;  
    }
  }
  else if(device == 2) {
    // send an "is_running" command to the Maestro controller (compact protocol)
    Serial1.write(0xAE);
    
    int count = 0;
    while(Serial1.available() == 0) {
      // while no reply from the controller, wait and count for timeout
      delay(1);
      count++;
      if(count == 12)  //timeout
        return(false);
    }  
    // get reply
    byte res = Serial1.read();
    if(res == 0) {
      return true;
    }
    else {
      return false;  
    }
  }
}


// ==================================================== for testing =======================================================================
void trigger_seq_tst(int device, int num) {
  devices[device-1].is_running = true;
  Serial.print(" ------> triggering device #");
  Serial.print(device, DEC);
  Serial.print(", code: ");
  Serial.println(num, DEC);
  next_release = millis() + 4000;
}


boolean is_script_running_tst(int device) {
  if(next_release == 0)
    return(false);
  if(millis() >=  next_release) 
    return(false);
  else
    return(true);  
}
// ========================================================================================================================================

/* 
  return 0 if data entry is complete for a normal sequence
  return 1 if data entry is complete for a loop sequence
  return -1 if data entry has not been complete yet or invalid code entered 
  return -2 (DEL_LAST_CODE) if standby code should be deleted
 */
int process_key(int device, char key) {
    Device_info *device_inf = &devices[device-1];
    int maestro_code;
    int key_idx = device_inf->key_seq_counter;
 
    if(key_idx == 0) {
       if(key == '0') {
         // delete last inserted code
         device_inf->bank = 255;
       }
       else if(key == '#') {
         // stop loop
         device_inf->is_loop = false;
         init_device_key_process(device);
         return(-1);
       }
       else if(isalpha(key)) {
         device_inf->bank = key - 'A';
         device_inf->key_seq_counter++; 
         return(-1);
       }
             
     }
     else if(isdigit(key) && (key_idx < 3)) {
       // first or second code digit  
       byte val = key - '0';
       device_inf->seq_idx = device_inf->seq_idx * 10 + val;
       device_inf->key_seq_counter++; 
       return(-1);
     }
     else if((key_idx > 1) && (key_idx < 5) && ((key == '*') || (key == '#'))) {
        if(device_inf->bank == 255) {
          init_device_key_process(device);
          return(DEL_LAST_CODE);
        }
        else if((device_inf->bank != INVALID_CODE) && (device_inf->seq_idx < BANK_SIZE)) {
          /*
          Serial.print("Trigerred - bank: ");
          Serial.println('A' + device_inf->bank);
          Serial.print(" index: ");
          Serial.println(device_inf->seq_idx, DEC);
          */
          return((key == '#') ? 1 : 0);
        }
    }
  
    // if we reached here, there must be an invalid code combination - reset processing
    init_device_key_process(device);
    return(-1);
}


int num_seq = 0;

void send_next_cmd(int device) {
  unsigned int next_code;
  
  if(devices[device-1].is_loop) {
      trigger_seq(device, devices[device-1].code_current & 0x00ff);
      return;
  }
  
  next_code = devices[device-1].code_current;
  
  if(next_code != -1) {
     trigger_seq(device, next_code & 0x00ff);
     if(next_code & 0x0100) {
        // Serial.println(" LOOP ");
        devices[device-1].is_loop = true; 
        devices[device-1].code_current = next_code & 0x00ff;       
     }
   }
}

void check_queue(int device) {
  Device_info *device_inf = &(devices[device-1]);
  
  if(device_inf->is_running && !is_script_running(device)) {
     
     device_inf->is_running = false;
     if(device_inf->is_loop) {
       // continue loop, no change to sequence codes
       send_next_cmd(device);
     }
     else {
       // not in a loop - advance standby code if exists
       info_changed = true;  
       device_inf->queue_size--;
       if(device_inf->queue_size == 1) {
         device_inf->code_current = device_inf->code_standby;
         device_inf->symbol_current = device_inf->symbol_standby;
         device_inf->code_standby = 0xffff;
         device_inf->symbol_standby = 0xffff;
         send_next_cmd(device);
       }
       else if(device_inf->queue_size == 0) {
         device_inf->code_current = 0xffff;
         device_inf->symbol_current = 0xffff;
       }
     }  
  }
}


void check_keypad(int device) { 
  int next_code;
  
  char key;
  Device_info *device_inf = &(devices[device-1]);
  
  if(device == 1) { 
    key = keypad_a.getKey();
  }
  else if(device == 2) {
    key = keypad_b.getKey();
  }
  
  if(key != NO_KEY) {
    /*
    Serial.print("=>");
    Serial.println(key);
    */
    int res = process_key(device, key);
    if(res != -1) {
      info_changed = true;
      
      if((res == DEL_LAST_CODE) && (device_inf->queue_size == 2)) {
         // delete last code from queue
          device_inf->code_standby = 0xffff;
          device_inf->symbol_standby = 0xffff;
          device_inf->queue_size = 1;
      }
      else {
        // find sequence code in the selected bank array    
        unsigned int maestro_code = (device == 1) ? codes_a[device_inf->bank][device_inf->seq_idx]
                                       : codes_b[device_inf->bank][device_inf->seq_idx];
        if(maestro_code == INVALID_CODE) {
          init_device_key_process(device);   
          return;
        }
        
        if(res == 1)  { // loop                              
           maestro_code = maestro_code | 0x0100;
        }
        
        unsigned int code_symbol = ((unsigned int)device_inf->bank) << 8;
        code_symbol |= device_inf->seq_idx;
      
        
        if(device_inf->is_running) {
          if(device_inf->queue_size == 1) {
            device_inf->code_standby = maestro_code;
            device_inf->symbol_standby = code_symbol;
            device_inf->queue_size = 2;
          
          }
        }
        else {
          device_inf->code_current = maestro_code;
          device_inf->symbol_current = code_symbol;
          device_inf->queue_size = 1;
          send_next_cmd(device);
        }  
          /*
            lcd.clear();
            lcd.setCursor(0, 0);
            lcd.print("push: ");
            lcd.print(code_name, DEC);
            lcd.print(" ");
            delay(1800);
          */
      }  
      init_device_key_process(device);      
    }
  }    
}

void display_line(int line, unsigned int val, char *text, int device) {
    lcd.setCursor(0, line-1);
    lcd.print(text);
    if((val != -1) && (val != 0xffff)) {
      byte bank =  (val >> 8) & 0x00ff ;
      lcd.print('A' + bank, BYTE);
      val = val & 0xff;
      lcd.print(val);
      if(devices[device-1].is_loop) {
        lcd.print(" L");
      }
    }
    else {
      lcd.print("None ");
    }
}

void display_info() {
  unsigned int head_running = devices[0].symbol_current;
  unsigned int head_waiting = devices[0].symbol_standby;
  
  unsigned int body_running = devices[1].symbol_current;
  unsigned int body_waiting = devices[1].symbol_standby;
  
  lcd.clear();
  display_line(1, head_running, "head running: ", 1); 
  display_line(2, head_waiting, "head standby: ", 1); 
  display_line(3, body_running, "body running: ", 2); 
  display_line(4, body_waiting, "body standby: ", 2); 
  
}

void loop() {  
   // repeat for device a, device b 
  for (int i = 1; i <= 2; i++) {
    check_keypad(i);
    check_queue(i); 
    
    // print info
    if(info_changed) {
       display_info();
       info_changed = false;     
    }
    
    
  }
   
}

hi again guys,
i did another check with the following simplified arduino code,
when im calling the sub routines from the maestro its working well, but if im calling subroutine 21, the maestro is shutting down.
is that some kind of stack overflow?
any idea why?

this is the new arduino code, the maestro code is in my last post.

unsigned char buf[4]; 


void setup() {
  Serial.begin(9600);
 // pinMode (13), OUTPUT;
  //digitalWrite (13, HIGH);
 //delay (1000);
   //digitalWrite (13, LOW);
   trigger_seq (21);
}


void trigger_seq(int num) {
   // send a "start sequence" command to the Maestro controller (compact protocol)
  buf[0] = 0xA7;
  buf[1] = num;  // number of the sequence to trigger

    Serial.write(buf, 2); 
  }

void loop() {  
  
    }

Hello, am_sy.

Since you are referring to code in a previous post, I merged the two topics together. It is OK to start a new topic if you have a new question, but if you are just reporting more information about an old problem then you should simply reply instead of starting a new topic.

You asked “is that some kind of stack overflow?” You should find out by looking in the Maestro Control Center’s Errors tab.

Thank you for simplifying your Arduino code. I don’t see anything wrong with it. Does the system works as expected if you change the argument of trigger_seq from 21 to 20?

Also I couldn’t see anything wrong with your subroutine #21 (thermic_look) but I think you should simplify the Maestro code anyway. Could you try entering the following as your Maestro script?

quit
sub test0
sub test1
sub test2
sub test3
sub test4
sub test5
sub test6
sub test7
sub test8
sub test9
sub test10
sub test11
sub test12
sub test13
sub test14
sub test15
sub test16
sub test17
sub test18
sub test19
sub test20
sub test21
sub test22
sub test23
sub test24
6400 0 servo
500 delay
7500 0 servo
500 delay
6400 0 servo
quit

This should define 25 subroutines that are all at the same address and do the same thing, which is to wiggle servo 0 once. Please try triggering subroutines 20, 21, and 22 from your Arduino by changing the argument to “trigger_seq” in the simple sketch you posted above, and tell us which subroutines you are able to trigger.

–David

thanks david,
for your question, up to sub routine 21, all the sequences run perfectly. sub routine 21 start and then the maestro is shutting down.
anyway i will test it with the script you posted and let you know what was the result.
thanks!
Amit

hi david
i tested the script you sent me, it worked well.
i continue up to 40 sub routines, no problem.

i re check my script, when the maestro shut down in routine 21, there is no “over flow” or any other sign of error at the control center.
you mentioned in your earlier reply that anyway the script should be more simple -
since im using maestro 24ch, there are many values…
the script is what i get automatically by doing “copy all sequences to script”.
i only changed the return with quit.
my purpose is to add also speed for each motor in each frame, and work with sub moving_wait instead of delay.

let me know how should i go on from here…
thanks,
Amit

Hello, am_sy.

It turns out that there is a bug in the Maestro Control Center that causes any calls to subroutines 64 through 127 to fail. This has been fixed in version 1.4.0.0 of the software. We plan to put it up officially on the website early next week, but until then you can download it here:
pololu.com/file/0J482/maestr … 110720.zip

Please install this new version and see if that fixes your problem.

Thank you for your help in figuring out this problem!

–David

thanks David,
i will check the new version and let you know how it works,
should i remove the last version and reinstall with the setup.exe file in the zip?
thanks,
A

You should not have to uninstall the old version but it would not hurt. The new installer should automatically replace the older version.

–David

its working well now,
thanks for your help!