Control each motors with Mini Maestro 24-Channel & Arduino

hello,
I want to control 48 motors with arduino.
So i got two Mini Maestro 24-Channel USB Servo Controllers.

I succeed in chaining two controllers with this code.
(Using aruino mega 2560 and power came from 5v/60A power supply.)

int minDeg=1000  ;
int maxDeg=2000;
int deg[48];
int delta=1;

void setup() {
  Serial.begin(115200); 
  for (byte i=0;i<48;i++) {
    deg[i]=2000;
  }
} 

void loop() {
  doDeg();
  doMove1();
  doMove2();
  delay(5);
}

void doDeg() {
  for (int i=0;i<48;i++) {
    deg[i]-=delta;
    deg[i]=constrain(deg[i],minDeg,maxDeg);
    if ((deg[i]==minDeg)||(deg[i]==maxDeg)) {
      delta*=-1;
    }
  }
}

void doMove1() {
  Serial.write(0xAA); // command
  Serial.write(0x0C); // device number
  Serial.write(0x1F); // motor to forward
  Serial.write(24); 
  Serial.write(0);
  for (byte i=0;i<24;i++) {
    Serial.write((deg[i]*4)&0x7F);
    Serial.write(((deg[i]*4)>>7)&0x7F);
  }
}


void doMove2() {
  Serial.write(0xAA); // command
  Serial.write(0x0C); // device number
  Serial.write(0x1F); // motor to forward
  Serial.write(24);
  Serial.write(0);
  for (byte i=24;i<48;i++) {
    Serial.write((deg[i]*4)&0x7F);
    Serial.write(((deg[i]*4)>>7)&0x7F);
  }
}

This case, the motors moved at the same time, same angle.
But now, i want to control each motor.

So i tried with this code

#define sw_pin 7

void setup()
{
  Serial.begin(9600);   
  pinMode(sw_pin,INPUT_PULLUP);
}

void set_target(unsigned char servo, unsigned int target){

    //Send a Pololu Protocol command
    Serial.write(0xAA); //start byte
    Serial.write(0x0C); //device id
    Serial.write(0x04); //command number
    Serial.write(servo); //servo number
    Serial.write(0);
    Serial.write((target*4) & 0x7F);  
    Serial.write(((target*4) >> 7) & 0x7F);
  
}

void loop()
{
  if(digitalRead(sw_pin) == HIGH){
    
    set_target(0,1000);
    delay(1000);
    set_target(0,2000);
    delay(1000);
    
    set_target(2,1000);
    delay(1000);
    set_target(2,2000);
    delay(1000);

    set_target(1,1000);
    delay(1000);
    set_target(1,2000);
    delay(1000);

    set_target(3,1000);
    delay(1000);
    set_target(3,2000);
    delay(1000);
  }
  
}

And It works. but i want to change the speed of motors slow.

I know that I can control the motor speed 0-127(0x00-0x7F).
So i changed 7F → 01, but i didn’t work.

how can i change the speed of motors?

Thanks for reading :^)

You can use the “Set Speed” command to adjust the speed of your servos. You can find this command in the “Serial Servo Commands” section of the user’s guide. In order to use this command, you need to set your command byte to 0x07. For example to set the speed of a servo on channel 0 to 1, your code would look something like this:

    Serial.write(0xAA); //start byte
    Serial.write(0x0C); //device number
    Serial.write(0x07); //command number to specify the "set speed" command
    Serial.write(0); //servo channel
    Serial.write((0x01) & 0x7F);  //low bits of speed
    Serial.write(0); //high bits of speed

Please note that you will not see the effect of set speed on the first movement of a servo after startup or after a target position of zero. This is because the Maestro needs an initial position of the servo to accurately set the speed. More information on this can be found in the “FAQs” tab of the Maestro product page.

-Brandon

Great!
Thanks for your help,Brandon. I solve the problem. :smiley: