-
Notifications
You must be signed in to change notification settings - Fork 0
/
Encoder.ino
94 lines (78 loc) · 1.77 KB
/
Encoder.ino
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#include <Servo.h>
Servo c;
#define ctrl_in A0
int A = 12;
int B = 11;
int I = 10;
int countTick = 0;
int countIndex = 0;
char precTick = 0;
char precIndex = 0;
char tick = 0;
char tickB = 0;
char index = 0;
int signal = 0;
int stage0 = 120;
int stage1 = 240;
int stage2 = 360;
int stage3 = 480;
int stage4 = 600;
//int multiplier = 20;
int stableValue = 0;
int reefState0 = 0;
int reefState1 = 63;
int reefState2 = reefState1 + 100; //plane
int reefState3 = reefState2 + 50;
int reefState4 = reefState3 + 50;
void setup() {
pinMode(A, INPUT);
pinMode(B, INPUT);
pinMode(I, INPUT);
pinMode(ctrl_in, INPUT);
Serial.begin(9600);
c.attach(9);
}
void loop() {
tick = digitalRead(A);
tickB = digitalRead(B);
index = digitalRead(I);
signal = analogRead(ctrl_in);
//Serial.print("Signal: ");
//Serial.println(signal);
if (signal <= stage0) {
stableValue = reefState0;
} else if (signal <= stage1) {
stableValue = reefState1;
} else if (signal <= stage2) {
stableValue = reefState2;
} else if (signal <= stage3) {
stableValue = reefState3;
} else if (signal <= stage4) {
stableValue = reefState4;
}
if(tick != precTick)
{
if(tick != tickB)
{
countTick = countTick + tick;
precTick = tick;
}
else
{
countTick = countTick - tick;
precTick = tick;
}
char tick_message[50];
sprintf(tick_message, "tick: %i %i %i\n", countTick, tick, tickB);
Serial.print(tick_message);
}
double ratio = ((double)abs(abs(countTick) - stableValue))/50.0;
if (abs(abs(countTick) - stableValue) > 50) {
ratio = 1;
}
if (countTick > stableValue) {
c.writeMicroseconds(500 + 1000 * (1 - ratio));
} else if (countTick < stableValue) {
c.writeMicroseconds(2500 - 1000 * (1 - ratio));
}
}