Control Servo Motor Control

I'm trying to control 6 servos individually which I can however I can't control the speed of the servos.

      leg1.write(20);
      leg2.write(160);
      leg3.write(80);
      leg4.write(110);
      leg5.write(70);
      leg6.write(130);

This is how I control the position of the servos. I know you can do for (pos = 0; pos <= 180; pos += 1) however I tried this for the servos, and I couldn't get them to move independently at the same time.

Which Arduino board are you using? If it's one of the supprted, you could give my MobaTools library a try. There are nearly the same methods as with Servo.h, but you can set the servo speed too.
The library can be installed by the library manager.

You can not do a for() loop on each servo in sequence if you want them to move at the same time. For each servo, you need to compare your current position to the end position, and if you aren't done, take one step and then move on to the next servo. Repeat until all servos are in their final position. Arrays would be your friend doing this exercise vs. variable1, variable2, etc.

I'm using an Arduino Uno, but I also have a mega

I need all of them to move at the same time as well as I need a set of legs e.g leg1 and 2 to equal 180 in total as they are all connected to the same plate above connected by some rods

The standard AVR Processors are supported.

No problem with MobaTools. Set speed, and start them all as in your #1 post. They will all move at the same time.

Would I need to change any other parts of the code from servo.h library to your mobatools library?

I don't know, because I don't know your code ( You should post it here ).

Mostly you only need to set the speed of the servos when changing from Servo.h to MobaTools.h. ( Of course there is no such method in the Servo.h library.

something like this will move them all


#include <Servo.h>

const byte LEG_COUNT = 6;

Servo leg[LEG_COUNT];

const byte servoPin[LEG_COUNT] = { 2,3,4,5,6,7 };  // whatever the real pins are

void setup() {
  for( int i = 0; i < LEG_COUNT; ++i ) {
    leg[i].attach(servoPin[i]);
  }
}

int finalPosition[LEG_COUNT] = { 20, 160, 80, 110, 70, 130 };  // final position for each leg

void loop() {
  for (int pos = 0; pos <= 180; ++pos) {
    bool done = true; // assume we are done
    for ( int i=0; i < LEG_COUNT; ++i ) {
      if ( pos <= finalPosition[i] ) {
        // need to move, so not done
        done = false;
        leg[i].write(pos);
      }
    }
    delay(15);    // adjust to correct speed
    if (done) break;  // if all legs are done, no need to continue for() loop
  }
  while(1);
}

or switch over to the MobaTools library

I was able to work with MobaTools.h and its just what I need for my project! Thank you!!

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.