Skip to content

Commit db1aba7

Browse files
committed
corrected the write() method
1 parent a23f2cb commit db1aba7

File tree

1 file changed

+13
-6
lines changed

1 file changed

+13
-6
lines changed

src/Servo_Hardware_PWM.cpp

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -376,16 +376,24 @@ void Servo::detachAll() {
376376

377377
void Servo::write(int value)
378378
{
379-
if (value < 0)
379+
float tempValue;
380+
381+
if (value <= 0)
382+
{
383+
tempValue = 0.0;
384+
}
385+
else if (value >= 180)
380386
{
381-
value = 0;
387+
tempValue = 180.0;
382388
}
383-
else if (value > 180)
389+
else
384390
{
385-
value = 180;
391+
tempValue = (float)value;
386392
}
387-
value = (((this->max - this->min) * value) / 180) + this->min;
388393

394+
tempValue = (((this->max - this->min) * tempValue) / 180.0) + this->min;
395+
396+
value = (int)tempValue;
389397
this->writeMicroseconds(value);
390398
}
391399

@@ -427,7 +435,6 @@ void Servo::writeMicroseconds(int value)
427435
}
428436
}
429437

430-
431438
int Servo::read() {
432439
float angle;
433440

0 commit comments

Comments
 (0)