-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathencoder
More file actions
81 lines (66 loc) · 1.72 KB
/
encoder
File metadata and controls
81 lines (66 loc) · 1.72 KB
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
int encoder = 11;
int curr_state;
int prev_state;
double curr_time;
double prev_time;
int time_change;
int counter = 0;
double speed;
// in3 and in4 are used to define direction of right motor
const int in3 = 5;
const int in4 = 4;
// enB is used for PWM to change speed of right motor
const int enB = 6;
const int in1 = 19;
const int in2 = 18;
const int enA = 2;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
delay(1000);
Serial.println("start");
// motor A - setting pin mode to OUTPUT
pinMode(in3,OUTPUT);
pinMode(in4,OUTPUT);
pinMode(enB,OUTPUT);
pinMode(encoder, INPUT_PULLUP);
prev_state = digitalRead(encoder);
Serial.println("first prev state ");
Serial.println(prev_state);
prev_time = 0;
}
void loop() {
// put your main code here, to run repeatedly:
//Serial.println("working");
digitalWrite(in3, HIGH);
digitalWrite(in4, LOW);
analogWrite(enB, 200);
//Serial.println("prev state ");
//Serial.println(prev_state);
curr_time = millis();
curr_state = digitalRead(encoder);
Serial.println("curr state ");
Serial.println(curr_state);
if (curr_state != prev_state) {
counter++;
}
//Serial.print("counter ");
//Serial.println(counter);
if (counter == 480){
Serial.print("counter ");
Serial.println(counter);
//Serial.print("current ");
//Serial.println(curr_time);
//Serial.print("previous ");
//Serial.println(prev_time);
time_change = curr_time - prev_time;
Serial.print("Time change ");
Serial.println(time_change);
speed = 6.5 * PI / (time_change / 1000);
Serial.print("Buggy speed ");
Serial.println(speed);
counter = 0;
prev_time = curr_time;
}
prev_state = curr_state;
}