-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrotoryEncoder.ino
84 lines (67 loc) · 2.1 KB
/
rotoryEncoder.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
/* Arduino Rotary Encoder Tutorial
by Dejan Nedelkovski, www.HowToMechatronics.com
*/
#define outputA 6
#define outputB 8
#define index A1
double wheelRadius = 0.13335; //wheel radius in meters
int state = 0;
long int period;
double timeRotation; //time for one revolution in seconds
double rotPerSec;
double roverSpeed; //speed of rover in mph
//Time in microseconds
int time1 = 0;
int time2 = 0;
int counter = 0;
int aState;
int aLastState;
int motorDirection;
int frequency = 3000; //Units in hertz
double delayTime = 1000 / (frequency * 2); //Units in millisec
void setup() {
pinMode (outputA, INPUT);
pinMode (outputB, INPUT);
pinMode (index, INPUT);
Serial.begin (9600);
// Reads the initial state of the outputA
aLastState = digitalRead(outputA);
//indexLastState = digitalRead(index);
}
void loop() {
aState = digitalRead(outputA); // Reads the "current" state of the outputA
// If Channel A goes from low to high, we count a pulse
if (aState == HIGH && aLastState == LOW) {
// If the outputB state is different to the outputA state, that means the encoder is rotating clockwise
if (digitalRead(outputB) == HIGH) {
motorDirection = 1;
counter ++;
}
else {
motorDirection = 0;
counter--;
}
}
//If channel A goes from high to low, start timer
if (aState == LOW && aLastState == HIGH && state == 0){
time1 = micros();
state = 1;
}
//Once channel A goes from low to high, increment period
if (aState == HIGH && aLastState == LOW && state == 1){
time2 = micros();
state = 2;
}
//If half period is reached
if (state == 2){
period = 2 * (time2 - time1);
timeRotation = 500 * period;
rotPerSec = 1000000 / timeRotation;
roverSpeed = 2 * M_PI * wheelRadius * rotPerSec * 2.23694;
Serial.println(roverSpeed); //speed of motor in mph
state = 0;
}
//IMPLEMENT ROLLING AVERAGE REDUCE VARIANCE
aLastState = aState; // Updates the previous state of the outputA with the current state
//indexLastState = indexState;
}