Skip to content

Commit c2f4781

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

File tree

2 files changed

+24
-13
lines changed

2 files changed

+24
-13
lines changed

src/Servo_Hardware_PWM.cpp

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,16 @@ Servo::Servo()
2727
}
2828
}
2929

30-
uint8_t Servo::attach(int pin)
30+
uint8_t Servo::attach(int pin)
3131
{
32+
return this->attach(pin, MIN_PULSE_WIDTH, MAX_PULSE_WIDTH);
33+
}
34+
35+
uint8_t Servo::attach(int pin, int min, int max)
36+
{
37+
this->min = min;
38+
this->max = max;
39+
3240
if (this->servoIndex < MAX_SERVOS) {
3341
if (pin == 2) {
3442
if(pinActive[1] == BOOL_FALSE){
@@ -370,19 +378,19 @@ void Servo::write(int value)
370378
{
371379
value = 180;
372380
}
373-
value = (((MAX_PULSE_WIDTH - MIN_PULSE_WIDTH) * value) / 180) + MIN_PULSE_WIDTH;
381+
value = (((this->max - this->min) * value) / 180) + this->min;
374382

375383
this->writeMicroseconds(value);
376384
}
377385

378386
void Servo::writeMicroseconds(int value)
379387
{
380388
if ((this->servoIndex < MAX_SERVOS)) {
381-
if (value < MIN_PULSE_WIDTH) {
382-
value = MIN_PULSE_WIDTH;
389+
if (value < this->min) {
390+
value = this->min;
383391
}
384-
else if (value > MAX_PULSE_WIDTH) {
385-
value = MAX_PULSE_WIDTH;
392+
else if (value > this->max) {
393+
value = this->max;
386394
}
387395
if (this->servoPin == 2 && pinActive[0] == BOOL_TRUE) {
388396
OCR3B = 0x0;

src/Servo_Hardware_PWM.h

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -69,14 +69,17 @@ 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-
void detach(); //detach the used pin
74-
void detachAll(); //automatically detaches all used pins
75-
void write(int value); //write angle in degrees
76-
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+
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
7778
private:
78-
uint8_t servoIndex; //number of attached Servos
79-
uint8_t servoPin; //pin number of the attached Servo
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
8083
};
8184

8285
#endif

0 commit comments

Comments
 (0)