Skip to content

Commit a74638f

Browse files
committed
added read methods
1 parent 00c760b commit a74638f

File tree

2 files changed

+35
-17
lines changed

2 files changed

+35
-17
lines changed

src/Servo_Hardware_PWM.cpp

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -398,30 +398,44 @@ void Servo::writeMicroseconds(int value)
398398
else if (value > this->max) {
399399
value = this->max;
400400
}
401+
this->pulseWidth = value;
402+
401403
if (this->servoPin == 2 && pinActive[0] == BOOL_TRUE) {
402404
OCR3B = 0x0;
403-
OCR3B = value * 2;
405+
OCR3B = this->pulseWidth * 2;
404406
}
405407
else if (this->servoPin == 3 && pinActive[1] == BOOL_TRUE) {
406408
OCR3C = 0x0;
407-
OCR3C = value * 2;
409+
OCR3C = this->pulseWidth * 2;
408410
}
409411
else if (this->servoPin == 7 && pinActive[2] == BOOL_TRUE) {
410412
OCR4B = 0x0;
411-
OCR4B = value * 2;
413+
OCR4B = this->pulseWidth * 2;
412414
}
413415
else if (this->servoPin == 8 && pinActive[3] == BOOL_TRUE) {
414416
OCR4C = 0x0;
415-
OCR4C = value * 2;
417+
OCR4C = this->pulseWidth * 2;
416418
}
417419
else if (this->servoPin == 44 && pinActive[4] == BOOL_TRUE) {
418420
OCR5C = 0x0;
419-
OCR5C = value * 2;
421+
OCR5C = this->pulseWidth * 2;
420422
}
421423
else if (this->servoPin == 45 && pinActive[5] == BOOL_TRUE) {
422424
OCR5B = 0x0;
423-
OCR5B = value * 2;
425+
OCR5B = this->pulseWidth * 2;
424426
}
425427
}
426428
}
429+
430+
int read()
431+
{
432+
int angle;
433+
angle = (((180 * (this->readMicroseconds() - this->min)) / (this->max - this->min));
434+
return angle;
435+
}
436+
437+
int readMicroseconds()
438+
{
439+
return this->pulseWidth;
440+
}
427441
#endif

src/Servo_Hardware_PWM.h

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -69,19 +69,23 @@ 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
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
7474
uint8_t attach(int pin, int min, int max, int defaultPos); //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
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
79+
int read(); //returns the current write angle in degrees
80+
int readMicroseconds(); //returns the current write angle in microseconds
81+
7982
private:
80-
uint8_t servoIndex; //number of attached Servos
81-
uint8_t servoPin; //pin number of the attached Servo
82-
uint16_t min; //lower pulse width limit
83-
uint16_t max; //upper pulse width limit
84-
uint16_t defaultPos; //pulse width when servo is attached
83+
uint8_t servoIndex; //number of attached Servos
84+
uint8_t servoPin; //pin number of the attached Servo
85+
uint16_t min; //lower pulse width limit
86+
uint16_t max; //upper pulse width limit
87+
uint16_t defaultPos; //pulse width when servo is attached
88+
uint16_t pulseWidth; //set pulse width
8589
};
8690

8791
#endif

0 commit comments

Comments
 (0)