@@ -31,8 +31,11 @@ Adafruit_PWMServoDriver pwm = Adafruit_PWMServoDriver();
31
31
// want these to be as small/large as possible without hitting the hard stop
32
32
// for max range. You'll have to tweak them as necessary to match the servos you
33
33
// 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
36
39
37
40
// our servo # counter
38
41
uint8_t servonum = 0 ;
@@ -43,29 +46,29 @@ void setup() {
43
46
44
47
pwm.begin ();
45
48
46
- pwm.setPWMFreq (60 ); // Analog servos run at ~60 Hz updates
49
+ pwm.setPWMFreq (FREQ ); // Analog servos run at ~60 Hz updates
47
50
48
51
delay (10 );
49
52
}
50
53
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!
53
56
void setServoPulse (uint8_t n, double pulse) {
54
57
double pulselength;
55
58
56
59
pulselength = 1000000 ; // 1,000,000 us per second
57
- pulselength /= 60 ; // 60 Hz
60
+ pulselength /= FREQ ; // Analog servos run at ~ 60 Hz updates
58
61
Serial.print (pulselength); Serial.println (" us per period" );
59
62
pulselength /= 4096 ; // 12 bits of resolution
60
63
Serial.print (pulselength); Serial.println (" us per bit" );
61
- pulse *= 1000000 ; // convert to us
64
+ pulse *= 1000000 ; // convert input seconds to us
62
65
pulse /= pulselength;
63
66
Serial.println (pulse);
64
67
pwm.setPWM (n, 0 , pulse);
65
68
}
66
69
67
70
void loop () {
68
- // Drive each servo one at a time
71
+ // Drive each servo one at a time using setPWM()
69
72
Serial.println (servonum);
70
73
for (uint16_t pulselen = SERVOMIN; pulselen < SERVOMAX; pulselen++) {
71
74
pwm.setPWM (servonum, 0 , pulselen);
@@ -78,6 +81,19 @@ void loop() {
78
81
79
82
delay (500 );
80
83
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
+
81
97
servonum ++;
82
- if (servonum > 7 ) servonum = 0 ;
98
+ if (servonum > 7 ) servonum = 0 ; // Testing the first 8 servo channels
83
99
}
0 commit comments