-
Notifications
You must be signed in to change notification settings - Fork 0
/
X9C.cpp
57 lines (51 loc) · 1.25 KB
/
X9C.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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#include "X9C.h"
X9C::X9C(int8_t CS, int8_t INC, int8_t UD) : CS(CS), INC(INC), UD(UD) {
pinMode(CS, OUTPUT);
pinMode(INC, OUTPUT);
pinMode(UD, OUTPUT);
// initialize resistor value
digitalWrite(INC, HIGH);
digitalWrite(UD, HIGH);
digitalWrite(CS, LOW);
for (uint8_t i = lowerLimit; i <= upperLimit; ++i) {
delay(1);
digitalWrite(INC, LOW);
delay(1);
digitalWrite(INC, HIGH);
}
delay(1);
digitalWrite(CS, HIGH);
wiperPos = upperLimit;
}
void X9C::setPos(int16_t pos) {
if (pos < lowerLimit) pos = lowerLimit;
if (pos > upperLimit) pos = upperLimit;
if (pos == wiperPos) return;
int16_t pulseCount;
if (pos < wiperPos) {
pulseCount = wiperPos - pos;
digitalWrite(UD, LOW);
}
else {
pulseCount = pos - wiperPos;
digitalWrite(UD, HIGH);
}
digitalWrite(INC, HIGH);
digitalWrite(CS, LOW);
for (int16_t i = 0; i < pulseCount; ++i) {
delay(1);
digitalWrite(INC, LOW);
delay(1);
digitalWrite(INC, HIGH);
}
delay(1);
digitalWrite(CS, HIGH);
wiperPos = pos;
}
int16_t X9C::getPos() {
return wiperPos;
}
void X9C::reset() {
wiperPos = lowerLimit;
this->setPos(upperLimit);
}