Skip to content

Commit 80011eb

Browse files
committed
added attach(pin, min, max, default) and dependencies
1 parent c2f4781 commit 80011eb

File tree

2 files changed

+26
-18
lines changed

2 files changed

+26
-18
lines changed

src/Servo_Hardware_PWM.cpp

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,19 @@ Servo::Servo()
2929

3030
uint8_t Servo::attach(int pin)
3131
{
32-
return this->attach(pin, MIN_PULSE_WIDTH, MAX_PULSE_WIDTH);
32+
return this->attach(pin, MIN_PULSE_WIDTH, MAX_PULSE_WIDTH, DEFAULT_PULSE_WIDTH);
3333
}
3434

35-
uint8_t Servo::attach(int pin, int min, int max)
35+
uint8_t Servo::attach(int pin, int min, int max)
36+
{
37+
return this->attach(pin, min, max, DEFAULT_PULSE_WIDTH);
38+
}
39+
40+
uint8_t Servo::attach(int pin, int min, int max, int default)
3641
{
3742
this->min = min;
3843
this->max = max;
44+
this->default = default;
3945

4046
if (this->servoIndex < MAX_SERVOS) {
4147
if (pin == 2) {
@@ -58,7 +64,7 @@ uint8_t Servo::attach(int pin, int min, int max)
5864
//setting the output to non inverted:
5965
TCCR3A |= (1 << COM3B1);
6066

61-
OCR3B = DEFAULT_PULSE_WIDTH; //setting the pulse width
67+
OCR3B = this->default; //setting the pulse width
6268

6369
//OC3B, Port E, Bit 4; setting pin 2 as output:
6470
DDRE |= (1 << PE4); //bit 4 (pin 2) as output
@@ -86,7 +92,7 @@ uint8_t Servo::attach(int pin, int min, int max)
8692
//setting the output to non inverted:
8793
TCCR3A |= (1 << COM3C1);
8894

89-
OCR3C = DEFAULT_PULSE_WIDTH; //setting the pulse width
95+
OCR3C = this->default; //setting the pulse width
9096

9197
//OC3C, Port E, Bit 5; setting pin 3 as output:
9298
DDRE |= (1 << PE5); //bit 5 (pin 3) as output
@@ -114,7 +120,7 @@ uint8_t Servo::attach(int pin, int min, int max)
114120
//setting the output to non inverted:
115121
TCCR4A |= (1 << COM4B1);
116122

117-
OCR4B = DEFAULT_PULSE_WIDTH; //setting the pulse width
123+
OCR4B = this->default; //setting the pulse width
118124

119125
//OC4B, Port H, Bit 4; setting pin 7 as output:
120126
DDRH |= (1 << PH4); //bit 4 (pin 7) as output
@@ -142,7 +148,7 @@ uint8_t Servo::attach(int pin, int min, int max)
142148
//setting the output to non inverted:
143149
TCCR4A |= (1 << COM4C1);
144150

145-
OCR4C = DEFAULT_PULSE_WIDTH; //setting the pulse width
151+
OCR4C = this->default; //setting the pulse width
146152

147153
//OC4C, Port H, Bit 5; setting pin 8 as output:
148154
DDRH |= (1 << PH5); //bit 5 (pin 8) as output
@@ -170,7 +176,7 @@ uint8_t Servo::attach(int pin, int min, int max)
170176
//setting the output to non inverted:
171177
TCCR5A |= (1 << COM5C1);
172178

173-
OCR5C = DEFAULT_PULSE_WIDTH; //setting the pulse width
179+
OCR5C = this->default; //setting the pulse width
174180

175181
//OC5C, Port L, Bit 5; setting pin 44 as output:
176182
DDRL |= (1 << PL5); //bit 5 (pin 44) as output
@@ -198,7 +204,7 @@ uint8_t Servo::attach(int pin, int min, int max)
198204
//setting the output to non inverted:
199205
TCCR5A |= (1 << COM5B1);
200206

201-
OCR5B = DEFAULT_PULSE_WIDTH; //setting the pulse width
207+
OCR5B = this->default; //setting the pulse width
202208

203209
//OC5B, Port L, Bit 4; setting pin 45 as output:
204210
DDRL |= (1 << PL4); //bit 4 (pin 45) as output

src/Servo_Hardware_PWM.h

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -69,17 +69,19 @@ class Servo
6969
{
7070
public:
7171
Servo();
72-
uint8_t attach(int pin); //attach the given pin; returns servoIndex number or 255 if too many servos
73-
uint8_t attach(int pin, int min, int max); //attach the given pin and set the upper and lower pulse width limits; returns servoIndex number or 255 if too many servos
74-
void detach(); //detach the used pin
75-
void detachAll(); //automatically detaches all used pins
76-
void write(int value); //write angle in degrees
77-
void writeMicroseconds(int value); //write pulse width in microseconds
72+
uint8_t attach(int pin); //attach the given pin; returns servoIndex number or 255 if too many servos
73+
uint8_t attach(int pin, int min, int max); //attach the given pin and set the upper and lower pulse width limits; returns servoIndex number or 255 if too many servos
74+
uint8_t attach(int pin, int min, int max, int default); //attach the given pin, set the upper and lower pulse width limits and set the pulse width when servo is attached; returns servoIndex number or 255 if too many servos
75+
void detach(); //detach the used pin
76+
void detachAll(); //automatically detaches all used pins
77+
void write(int value); //write angle in degrees
78+
void writeMicroseconds(int value); //write pulse width in microseconds
7879
private:
79-
uint8_t servoIndex; //number of attached Servos
80-
uint8_t servoPin; //pin number of the attached Servo
81-
uint8_t min; //lower pulse width limit
82-
uint8_t max; //upper pulse width limit
80+
uint8_t servoIndex; //number of attached Servos
81+
uint8_t servoPin; //pin number of the attached Servo
82+
uint8_t min; //lower pulse width limit
83+
uint8_t max; //upper pulse width limit
84+
uint8_t default; //pulse width when servo is attached
8385
};
8486

8587
#endif

0 commit comments

Comments
 (0)