Changing speed to my servo using Adafruit servo driver

Post here about your Arduino projects, get help - for Adafruit customers!

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
aldoz
 
Posts: 31
Joined: Mon May 12, 2014 7:24 pm

Changing speed to my servo using Adafruit servo driver

Post by aldoz »

Hi guys, finally I am using my Adafruit servo driver with my Arduino MEGA2560
the example "servo" code from Adafruit library is cool and I can move my servo using map function giving just the angle where my servo need to reach.

Before the come of Adafruit servo driver I was using my servo directly from my Arduino and I was able to use delay() method to change the speed of my servo.

Now, using the Adafruit I am not able to change the speed! I see frequency and PWM methods but I am not sure if these methods can modify the speed of my servo like when I using delay() method... (my experience is very limited..)

Can some of you modify the example code to make my servo running at half speed?

Here the code:

Code: Select all

#include <Wire.h>
#include <Adafruit_PWMServoDriver.h>

// called this way, it uses the default address 0x40
Adafruit_PWMServoDriver pwm = Adafruit_PWMServoDriver();
// you can also call it with a different address you want
//Adafruit_PWMServoDriver pwm = Adafruit_PWMServoDriver(0x41);

// Depending on your servo make, the pulse width min and max may vary, you 
// want these to be as small/large as possible without hitting the hard stop
// for max range. You'll have to tweak them as necessary to match the servos you
// have!
#define SERVOMIN  150 // this is the 'minimum' pulse length count (out of 4096)
#define SERVOMAX  600 // this is the 'maximum' pulse length count (out of 4096)

// our servo # counter
uint8_t servonum = 0;

void setup() {
  Serial.begin(9600);
  Serial.println("16 channel Servo test!");

  pwm.begin();
  
  pwm.setPWMFreq(60);  // Analog servos run at ~60 Hz updates
}

// you can use this function if you'd like to set the pulse length in seconds
// e.g. setServoPulse(0, 0.001) is a ~1 millisecond pulse width. its not precise!
void setServoPulse(uint8_t n, double pulse) {
  double pulselength;
  
  pulselength = 1000000;   // 1,000,000 us per second
  pulselength /= 60;   // 60 Hz
  Serial.print(pulselength); Serial.println(" us per period"); 
  pulselength /= 4096;  // 12 bits of resolution
  Serial.print(pulselength); Serial.println(" us per bit"); 
  pulse *= 1000;
  pulse /= pulselength;
  Serial.println(pulse);
  pwm.setPWM(n, 0, pulse);
}

void loop() {
  // Drive each servo one at a time
  Serial.println(servonum);
  for (uint16_t pulselen = SERVOMIN; pulselen < SERVOMAX; pulselen++) {
    pwm.setPWM(servonum, 0, pulselen);
  }
  delay(500);
  for (uint16_t pulselen = SERVOMAX; pulselen > SERVOMIN; pulselen--) {
    pwm.setPWM(servonum, 0, pulselen);
  }
  delay(500);

  servonum ++;
  if (servonum > 15) servonum = 0;
}
Thank you in advance!

User avatar
adafruit_support_bill
 
Posts: 88093
Joined: Sat Feb 07, 2009 10:11 am

Re: Changing speed to my servo using Adafruit servo driver

Post by adafruit_support_bill »

A delay will work, just as it does with the Servo library. To effectively control the speed, the delay needs to be inside the 'for' loop:

Code: Select all

  for (uint16_t pulselen = SERVOMIN; pulselen < SERVOMAX; pulselen++) 
 {
    pwm.setPWM(servonum, 0, pulselen);
    delay(50);  //  <-------- Increasing this delay will slow down the servo movement
  }

Locked
Please be positive and constructive with your questions and comments.

Return to “Arduino”