MegaServo Library!

:)Apologies if I’m in the wrong place with this! This new library is just what the doctor ordered for my project but because I’m a newby at this I wondered if anyone could point out to me what I would have to change in the following code in order to independently control the servos with separate pots! Not looking for anyone to do my homework just a pointer or short example :slight_smile: Thanks, Goatboy

#include <MegaServo.h>
#define NBR_SERVOS 12 // the number of servos, up to 48 for Mega, 12 for other boards
#define FIRST_SERVO_PIN 2

MegaServo Servos[NBR_SERVOS] ; // max servos is 48 for mega, 12 for other boards

int pos = 0; // variable to store the servo position
int potPin = 0; // connect a pot to this pin.

void setup()
{
for( int i =0; i < NBR_SERVOS; i++)
Servos*.attach( FIRST_SERVO_PIN +i, 800, 2200);*
}
void loop()
{

  • pos = analogRead(potPin); // read a value from 0 to 1023*
  • for( int i =0; i <NBR_SERVOS; i++)*
    _ Servos*.write( map(pos, 0,1023,0,180)); _
    _
    delay(15); _
    _
    }*_

You need to move the "analogRead" inside the "for" loop, and instead of having a constant zero for "potPin" you'll need to supply the loop variable as the parameter of "analogRead" (remembering of course, just how many analogue inputs you've got)

One possible limitation would be that arduino has only 6 analog input pins ( or 8 on the Arduino mini and nano IIRC ). You will have to read up on using external ADCs to get more analog inputs.

In case you want to use only 6

#include <MegaServo.h>
#define NBR_SERVOS 6
#define FIRST_SERVO_PIN 2 
#define FIRST_POT_PIN 0

MegaServo Servos[NBR_SERVOS];
int pos = 0;

void setup()
{
 for( int i =0; i < NBR_SERVOS; i++)
   Servos[i].attach( FIRST_SERVO_PIN +i, 800, 2200);
}
void loop()
{

 for( int i =0; i <NBR_SERVOS; i++){
   pos = analogRead(FIRST_POT_PIN+i); 
   Servos[i].write( map(pos, 0,1023,0,180));  
 }
 delay(15);  
}

Now I haven’t tested that code in anyway. I don’t have even one servo with me :slight_smile: much less six servos.

;D ;)Thanks!! Really appreciate that!! :smiley: I’m still trying to get my head around how this stuff works! ;D Goatboy

Thanks guys for answerting.

BTW, if you only have a few servos then you don’t need to use an array. here is another way you can use the library:

#include <MegaServo.h>

MegaServo rollServo;
MegaServo pitchServo;
MegaServo yawServo;

void setup()
{
   rollServo.attach( 2, 800, 2200); // this servo on pin 2
   pitchServo.attach( 3, 800, 2200); // this servo on pin 3
   yawServo.attach( 4, 800, 2200); // this servo on pin 4
}
void loop()
{
  rollServo.write( map(analogRead(0), 0,1023,0,180));  // pot on analog pin 0
  pitchServo.write( map(analogRead(1), 0,1023,0,180));  // pot on analog pin 1
  yawServo.write( map(analogRead(2), 0,1023,0,180));  // pot on analog pin 2
  delay(15);
}

:slight_smile: Is that for use with an accelerometer? Can you tell me why I’m not getting a full 0-180 sweep with my servos using this code? :slight_smile: Thanks

Cool! I've figured out that now but I don't understand why the 800 and 2200 are written in unless it's to limit the sweep intentionally!?! :D

The default values for servo range are the same as the Arduino library, 544 and 2400 microseconds. Some servos are happier with a smaller range and the example above shows how to set this. But you can certainly do this:

rollServo.attach(2); // this servo on pin 2, using the default range

:D Nothing like the pleasure of learning!! ;D Thanks ;D

Have fun!
… and please do post progress on your project.

:D Yes, absolutely! Do you know why some servos create a hum when the pot is fully turned? :-?

Do you know why some servos create a hum when the pot is fully turned?

because you are asking the servo to move into a position beyond its design capability. Some servos will not rotate over the default range of values and you would need to restrict the range by using parameters to the attach function as shown above, or reduce the range used in the write function to prevent that happining.

:)Excellent ;D!!!! Thanks too much ;D

Hello again! Having trouble uploading a new topic so I thought I’d patch this new problem on to this one!! :slight_smile:
Here’s my code-

// Sweep
// by BARRAGAN http://barraganstudio.com

#include <Servo.h>

Servo myservo1; // create servo object to control a servo
Servo myservo2; // create servo object to control a servo
// a maximum of eight servo objects can be created

int pos = 0; // variable to store the servo position
int pos1= 0;

void setup()
{
myservo1.attach(9); // attaches the servo on pin 9 to the servo object
myservo2.attach(10); // attaches the servo on pin 10 to the servo object
}

void loop()
{
for(pos1= 50; pos1<= 160; pos1 += 180)
for(pos = 80; pos <= 105; pos += 30) // goes from 75 degrees to 110 degrees
{
myservo2.write(pos1); // tell servo to go to position in variable ‘pos’
delay(500); // waits 1000ms for the servo to reach the position
// in steps of 35 degree
myservo1.write(pos); // tell servo to go to position in variable ‘pos’
delay(500); // waits 1000ms for the servo to reach the position

}
for(pos1= 160; pos1>=50; pos1-=180)
for(pos = 105; pos>=80; pos-=30) // goes from 110 degrees to 75 degrees

{
myservo2.write(pos); // tell servo to go to position in variable ‘pos’
delay(500); // waits 1000ms for the servo to reach the position
myservo1.write(pos); // tell servo to go to position in variable ‘pos’
delay(500); // waits 1000ms for the servo to reach the position

}
}

The problem here other than the way it’s written is that I can’t get the first servo to go above or below the “105” of the second servo! Can you tell me where I’ve gone wrong? Thanks, Goatboy

In your last set of double for loops, you are setting both servos to the value of "pos".

Nothing less than fookin Awesome Ericski!!! I love seeing the error of my ways ;D, Goatboy............Long live this beautiful forum :)

The default values for servo range are the same as the Arduino library, 544 and 2400 microseconds. Some servos are happier with a smaller range and the example above shows how to set this. But you can certainly do this:

Hi, i have some questions about this issue.

  • Is it microseconds or miliseconds? The default arduino max PWM frequency is around 490Hz, so this means 0.002s or 2000 miliseconds... as for the lower limit 800.. if it is in microseconds this means the frequency is 1250Hz>500Hz. This is not possible with default PWM settings right?

Thanks for the help in advance. :)

  • Is it microseconds or miliseconds? The default arduino max PWM frequency is around 490Hz, so this means 0.002s or 2000 miliseconds... as for the lower limit 800.. if it is in microseconds this means the frequency is 1250Hz>500Hz. This is not possible with default PWM settings right?

microseconds

It's a common misunderstanding. Hobby servos are driven with using Pulse Position Modulation (PPM), not Pulse Width Modulation (PWM). The Arduino timer can simulate the PPM pulses over the range of 544 to 2400 microseconds, but it does not use the standard analogWrite settings.

I posted a diagram of a typical servo pulse in this thread: http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1232572239

0.002s or 2000 miliseconds

0.002s is 2 milliseconds, or 2000 microseconds

Thanks mem and AWOL. You are quite right, i understand now... The Arduino PWM frequency is around 490Hz, so a servo gets a pulse every 0.002s (2 microseconds). When we define the range in the MegaServo lib we are actually defining the pulse width. Thanks a lot! ;)