forked from balassy/useless-box
-
Notifications
You must be signed in to change notification settings - Fork 0
/
speed-servo.cpp
41 lines (34 loc) · 939 Bytes
/
speed-servo.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#include "speed-servo.h"
const int SLOW_MOVE_DELAY = 15;
const int Fast_MOVE_DELAY = 2;
void SpeedServo::attach(uint8_t pin) {
_servo.attach(pin);
}
// Valid position: 0-180.
void SpeedServo::moveNowTo(int newPosition) {
_lastPosition = newPosition;
_servo.write(newPosition);
}
// Valid position: 0-180.
void SpeedServo::moveFastTo(int newPosition) {
_moveTo(newPosition, Fast_MOVE_DELAY);
}
// Valid position: 0-180.
void SpeedServo::moveSlowTo(int newPosition) {
_moveTo(newPosition, SLOW_MOVE_DELAY);
}
// Valid position: 0-180.
void SpeedServo::_moveTo(int newPosition, unsigned long stepDelay) {
if(newPosition > _lastPosition) {
for (int pos = _lastPosition; pos <= newPosition; pos++) {
_servo.write(pos);
delay(stepDelay);
}
} else {
for (int pos = _lastPosition; pos >= newPosition; pos--) {
_servo.write(pos);
delay(stepDelay);
}
}
_lastPosition = newPosition;
}