Go Down

Topic: Wrong signal generated by using Servo library[Solved, but keep discussion] (Read 2 times) previous topic - next topic

littleming

Dear all,

I am modified and tested the program for controlling 12 servo motors today. My program is used "Servo" library. The board is Mega1280. But I found that the PWM mark space ratio are wrong from pin 3 and pin 11. The pulse wave period should be 20ms, but more than one pulse is shown inside a period.

My program is shown below:
#include <Servo.h>

Servo myservo[12];

#define SERVO_TO_PIN(x) (x+2)

int pos = 5;  //Beginning Degree
int RotateDirection = 0;

void setup()
{
 Serial.begin(9600);
 for(int ServoNum = 0; ServoNum < 11; ServoNum++)
 {
   myservo[ServoNum].attach(SERVO_TO_PIN(ServoNum));
 }
 Serial.println("Completed Setup");
}

void loop()
{
 pos += RotateDirection;
 
 if(pos >= 175)
 {
   RotateDirection = -10;
 }
 
 if(pos <=5)
 {
   RotateDirection = 10;
 }
 
 for(int ServoNum = 0; ServoNum <= 11; ServoNum++)
 {                                  
   myservo[ServoNum].write(pos);// tell servo to go to position in variable 'pos'
   delay(15);                       // waits 15ms for the servo to reach the position
   Serial.print("The angle of the servo");
   Serial.print(ServoNum);
   Serial.print(" is ");
   Serial.print(myservo[ServoNum].read());
   Serial.print('\n');
   delay(500);  
 }
}

Please help, thank you very much.

johnwasser

You attach 11 servos and then write to 12 servos.
Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

littleming

#2
Feb 15, 2013, 02:00 am Last Edit: Feb 15, 2013, 03:09 am by littleming Reason: 1
Do you mean I should modify to

void setup()
{
 Serial.begin(9600);
 for(int ServoNum = 0; ServoNum <= 11; ServoNum++)
 {
   myservo[ServoNum].attach(SERVO_TO_PIN(ServoNum));
 }
 Serial.println("Completed Setup");
}

oric_dan

That's what he means, I believe. Also, you should bracket your code using the # icon,
Code: [Select]
void setup()
{
  Serial.begin(9600);
  for(int ServoNum = 0; ServoNum <= 11; ServoNum++)
  {
    myservo[ServoNum].attach(SERVO_TO_PIN(ServoNum));
  }
  Serial.println("Completed Setup");
}

littleming

Sorry I am a beginner of the Arduino. Where should I put the bracket in my code by using #?

Go Up