-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEncoderSquare.ino
More file actions
140 lines (113 loc) · 2.62 KB
/
EncoderSquare.ino
File metadata and controls
140 lines (113 loc) · 2.62 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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
// Dieser Code soll den Roboter immer ein Feld forwärts
// fahren lassen und dann um 90° nach rechts drehen
// Dies soll dazu dienen, die Encoder Werte besser fest-
// stellen zu können!
// geschrieben am 13.1.18
#include "Vars.h"
#include "Engine.h"
#include <LiquidCrystal_I2C.h>
volatile int encoderLeft = 0;
volatile int encoderRight = 0;
LiquidCrystal_I2C lcd(LCDAddress, LCDCols, LCDRows);
void setup() {
lcd.init();
lcd.backlight();
lcd.print("Starting up");
pinMode(ena, OUTPUT);
pinMode(enb, OUTPUT);
pinMode(IN1, OUTPUT);
pinMode(IN2, OUTPUT);
pinMode(IN3, OUTPUT);
pinMode(IN4, OUTPUT);
attachInterrupt(digitalPinToInterrupt(TACHO0), countL, CHANGE);
attachInterrupt(digitalPinToInterrupt(TACHO1), countR, CHANGE);
engineForward();
delay(2500);
}
void loop() {
turn(RIGHT);
engineStop();
lcd.clear();
lcd.print("DONE!");
delay(1000);
moveTileForward();
engineStop();
lcd.clear();
lcd.print("DONE!");
delay(1000);
}
void resetEncoder() {
encoderLeft = 0;
encoderRight = 0;
}
void moveTileForward() {
lcd.clear();
lcd.print("reaching new Tile!");
resetEncoder();
while (encoderLeft < leftMoveGoal || encoderRight < rightMoveGoal) {
if (encoderLeft < leftMoveGoal) {
engineLeftForward();
digitalWrite(ena, HIGHSPEED);
} else {
engineLeftStop();
}
if (encoderRight < rightMoveGoal) {
engineRightForward();
digitalWrite(enb, HIGHSPEED);
}
else {
engineRightStop();
}
}
}
void turn(int dir) {
lcd.clear();
String s = "turn ";
if (dir == RIGHT) {
s += "right";
} else {
s += "left";
}
lcd.print(s);
resetEncoder();
if (dir == RIGHT) {
while (encoderLeft < leftTurnGoal || encoderRight < rightTurnGoal) {
if (encoderLeft < leftTurnGoal) {
engineLeftForward();
digitalWrite(enb, MIDDLESPEED);
} else {
engineLeftStop();
}
if (encoderRight < rightTurnGoal) {
engineRightBackward();
digitalWrite(ena, MIDDLESPEED);
} else {
engineRightStop();
}
}
} else if (dir == LEFT) {
while (encoderLeft < leftTurnGoal || encoderRight < rightTurnGoal) {
if (encoderLeft < leftTurnGoal) {
engineLeftBackward();
digitalWrite(enb, MIDDLESPEED);
} else {
engineLeftStop();
}
if (encoderRight < rightTurnGoal) {
engineRightForward();
digitalWrite(ena, MIDDLESPEED);
} else {
engineRightStop();
}
}
lcd.clear();
lcd.print("DONE!");
delay(1000);
}
}
void countL() {
encoderLeft++;
}
void countR() {
encoderRight++;
}