Skip to content

Commit 2631ff3

Browse files
authored
Add example for writeMicroseconds()
The writeMicroseconds() function is used to mimic the Arduino Servo library writeMicroseconds() behavior. The writeMicroseconds() function is based on setServoPulse() but uses microseconds instead of seconds. This is to match with the Arduino Servo library writeMicroseconds() behavior. This example follows what setPWM() example does driving the servo through the range of positions but using writeMicroseconds() writeMicroseconds() is not expected to be precise due to calculation rounding converting between microseconds and ticks. New constants were added to simplify changes like the frequency of the PCA9685 chip.
1 parent cbe4af0 commit 2631ff3

File tree

1 file changed

+25
-9
lines changed

1 file changed

+25
-9
lines changed

examples/servo/servo.ino

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,11 @@ Adafruit_PWMServoDriver pwm = Adafruit_PWMServoDriver();
3131
// want these to be as small/large as possible without hitting the hard stop
3232
// for max range. You'll have to tweak them as necessary to match the servos you
3333
// have!
34-
#define SERVOMIN 150 // this is the 'minimum' pulse length count (out of 4096)
35-
#define SERVOMAX 600 // this is the 'maximum' pulse length count (out of 4096)
34+
#define SERVOMIN 150 // This is the 'minimum' pulse length count (out of 4096)
35+
#define SERVOMAX 600 // This is the 'maximum' pulse length count (out of 4096)
36+
#define USMIN 611 // This is the rounded 'minimum' microsecond length based on the minimum pulse of 150
37+
#define USMAX 2441 // This is the rounded 'maximum' microsecond length based on the maximum pulse of 600
38+
#define FREQ 60 // Analog servos run at ~60 Hz updates
3639

3740
// our servo # counter
3841
uint8_t servonum = 0;
@@ -43,29 +46,29 @@ void setup() {
4346

4447
pwm.begin();
4548

46-
pwm.setPWMFreq(60); // Analog servos run at ~60 Hz updates
49+
pwm.setPWMFreq(FREQ); // Analog servos run at ~60 Hz updates
4750

4851
delay(10);
4952
}
5053

51-
// you can use this function if you'd like to set the pulse length in seconds
52-
// e.g. setServoPulse(0, 0.001) is a ~1 millisecond pulse width. its not precise!
54+
// You can use this function if you'd like to set the pulse length in seconds
55+
// e.g. setServoPulse(0, 0.001) is a ~1 millisecond pulse width. It's not precise!
5356
void setServoPulse(uint8_t n, double pulse) {
5457
double pulselength;
5558

5659
pulselength = 1000000; // 1,000,000 us per second
57-
pulselength /= 60; // 60 Hz
60+
pulselength /= FREQ; // Analog servos run at ~60 Hz updates
5861
Serial.print(pulselength); Serial.println(" us per period");
5962
pulselength /= 4096; // 12 bits of resolution
6063
Serial.print(pulselength); Serial.println(" us per bit");
61-
pulse *= 1000000; // convert to us
64+
pulse *= 1000000; // convert input seconds to us
6265
pulse /= pulselength;
6366
Serial.println(pulse);
6467
pwm.setPWM(n, 0, pulse);
6568
}
6669

6770
void loop() {
68-
// Drive each servo one at a time
71+
// Drive each servo one at a time using setPWM()
6972
Serial.println(servonum);
7073
for (uint16_t pulselen = SERVOMIN; pulselen < SERVOMAX; pulselen++) {
7174
pwm.setPWM(servonum, 0, pulselen);
@@ -78,6 +81,19 @@ void loop() {
7881

7982
delay(500);
8083

84+
// Drive each servo one at a time using writeMicroseconds(), it's not precise due to calculation rounding!
85+
// The writeMicroseconds() function is used to mimic the Arduino Servo library writeMicroseconds() behavior.
86+
for (uint16_t microsec = USMIN; microsec < USMAX; microsec++) {
87+
pwm.writeMicroseconds(servonum, microsec);
88+
}
89+
90+
delay(500);
91+
for (uint16_t microsec = USMAX; microsec > USMIN; microsec--) {
92+
pwm.writeMicroseconds(servonum, microsec);
93+
}
94+
95+
delay(500);
96+
8197
servonum ++;
82-
if (servonum > 7) servonum = 0;
98+
if (servonum > 7) servonum = 0; // Testing the first 8 servo channels
8399
}

0 commit comments

Comments
 (0)