-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patharduinoRobotCar.ino
321 lines (268 loc) · 7.18 KB
/
arduinoRobotCar.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
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
/*
Robot car controlled by 4 light sensors
*/
#define BLACK 0
#define GREY 1
#define WHITE 2
#define LEFT 0
#define RIGHT 1
//-------------------------------------------------------------------------------------------------
int leftPinColor = 0;
int rightPinColor = 0;
/* analog pins for the 4 light sensors*/
int leftPin = A4;
int rightPin = A5;
int leftPinPrevColor;
int rightPinPrevColor;
/* LEDs */
int LEDPin = 6;
int LEDTest = 11;
/* digital pins for the Hbridge controlling the 2 motors */
int motor1PinA = 2;
int motor1PinB = 4;
int enablePinMotor1 = 5;
int motor2PinA = 7;
int motor2PinB = 8;
int enablePinMotor2 = 9;
// algorithms variables
int lastTransition = 0;
int lastTransitionSensor;
int transitionThreshold = 100;
int loopDelay = 5;
int changeDirection = false;
int timerFor = 0;
int timerBack = 0;
//-------------------------------------------------------------------------------------------------
//------------------------------------------initialization-----------------------------------------
void setup() {
//Serial.begin(9600);
pinMode(leftPin, INPUT);
pinMode(rightPin, INPUT);
// if necessary enable the pull up resistor GEHT GRAD NICHT
// digitalWrite(18, HIGH);
// pinMode(leftPin, INPUT_PULLUP);
// digitalWrite(19, HIGH);
// pinMode(rightPin, INPUT_PULLUP);
pinMode(motor1PinA, OUTPUT);
pinMode(motor1PinB, OUTPUT);
pinMode(motor2PinA, OUTPUT);
pinMode(motor2PinB, OUTPUT);
// set enablePin high so that motors can turn on:
digitalWrite(enablePinMotor1, HIGH);
digitalWrite(enablePinMotor2, HIGH);
analogWrite(LEDPin, 255); //LED for rotation detection on
//Serial.println("Car initialized, ready to go!");
}
//
//-----------------------------------main loop-----------------------------------------------------------
void loop() {
if (changeDirection)
{
return;
}
checkRightSensorToCenter();
// retrieve values of lightness
readSensors();
//driveToBrightness();
// print values
printLightValues(leftPin, rightPin);
//delay(loopDelay);
timerFor += loopDelay;
timerBack += loopDelay;
//Stop();
}
//
//----------------------------------readSensors-------------------------------------------------------
void readSensors()
{
leftPinColor = analogRead(leftPin);
rightPinColor = analogRead(rightPin);
}
//
//----------------------------------determine direction-----------------------------------------------
void driveToBrightness()
{
// if(timerFor < 500){
// timerFor += loopDelay;
// Forward();
// }else{
// timerBack = 0;
// if (timerBack < 500){
// timerBack += loopDelay;
// Backward();
// }else{
// timerFor = 0;
// }
// }
//
Backward();
//transfer python sudo code to arduino code
analogWrite(enablePinMotor1, 0);
analogWrite(enablePinMotor2, 250);
}
//
//----------------------------------debug functions --------------------------------------------------
void printLightValues(int left, int right)
{
int s = analogRead(right);
//Serial.println(s);
if (s > 300) {
analogWrite(LEDTest, 255);
//Serial.print("moved ");
//
}else{
analogWrite(LEDTest, 0);
}
// Serial.print("light sensors: ");
// Serial.print(analogRead(left));
// Serial.print(" ");
// Serial.println(analogRead(right));
}
//
//----------------------------------turning functions --------------------------------------------------
void checkRightSensorToCenter()
{
bool outIn = (rightPinColor == BLACK && rightPinPrevColor == WHITE)
|| (rightPinColor == GREY && rightPinPrevColor == BLACK)
|| (rightPinColor == WHITE && rightPinPrevColor == GREY);
if (outIn && leftPinColor == rightPinPrevColor)
{
if(driveStraigh(RIGHT)){
return;
}
//turnRight();
}
if (outIn && leftPinColor == leftPinColor)
{
if(driveStraigh(RIGHT)){
return;
}
//turnLeft();
}
lastTransition = millis();
}
void checkLeftSensorToCenter()
{
bool outIn = (leftPinColor == BLACK && leftPinPrevColor == WHITE)
|| (leftPinColor == GREY && leftPinPrevColor == BLACK)
|| (leftPinColor == WHITE && leftPinPrevColor == GREY);
if (outIn && rightPinColor == leftPinPrevColor)
{
if(driveStraigh(LEFT)){
return;
}
//turnLeft();
}
if (outIn && rightPinColor == leftPinColor)
{
if(driveStraigh(LEFT)){
return;
}
//turnRight();
}
lastTransition = millis();
}
void checkRightSensorToBorder()
{
bool inOut = (rightPinColor == GREY && rightPinPrevColor == WHITE)
|| (rightPinColor == BLACK && rightPinPrevColor == GREY)
|| (rightPinColor == WHITE && rightPinPrevColor == BLACK);
if (inOut && leftPinColor == rightPinPrevColor)
{
if(driveStraigh(RIGHT)){
//turn180();
changeDirection = true;
return;
}
//turnStrongerRight();
}
if (inOut && leftPinColor == rightPinColor)
{
if(driveStraigh(RIGHT)){
//turn180();
changeDirection = true;
return;
}
//turnStrongerLeft();
}
changeDirection = true;
lastTransition = millis();
}
void checkLeftSensorToBorder()
{
bool inOut = (leftPinColor == GREY && leftPinPrevColor == WHITE)
|| (leftPinColor == BLACK && leftPinPrevColor == GREY)
|| (leftPinColor == WHITE && leftPinPrevColor == BLACK);
if (inOut && rightPinColor == leftPinPrevColor)
{
if(driveStraigh(LEFT)){
//turn180();
changeDirection = true;
return;
}
//turnStrongerRight();
}
if (inOut && rightPinColor == leftPinColor)
{
if(driveStraigh(LEFT)){
//turn180();
changeDirection = true;
return;
}
//turnStrongerLeft();
}
changeDirection = true;
lastTransition = millis();
}
bool driveStraigh(int sensor)
{
if((millis() - lastTransition < transitionThreshold) && (lastTransitionSensor != sensor))
{
lastTransitionSensor = sensor;
lastTransition = millis();
return true;
}
return false;
}
//
//----------------------------------motor functions --------------------------------------------------
void Forward()
{
Serial.println("move forward");
digitalWrite(motor1PinA, LOW);
digitalWrite(motor1PinB, HIGH);
digitalWrite(motor2PinA, HIGH);
digitalWrite(motor2PinB, LOW);
}
void Backward() // inverse voltage of both motors
{
Serial.println("move backward");
digitalWrite(motor1PinA, LOW);
digitalWrite(motor1PinB, HIGH);
digitalWrite(motor2PinA, LOW);
digitalWrite(motor2PinB, HIGH);
}
void RotateLeft() // inverse voltage of only one motor
{
Serial.println("rotate left");
digitalWrite(motor1PinA, LOW);
digitalWrite(motor1PinB, HIGH);
digitalWrite(motor2PinA, HIGH);
digitalWrite(motor2PinB, LOW);
}
void RotateRight() // inverse voltage of only one motor
{
Serial.println("rotate right");
digitalWrite(motor1PinA, HIGH);
digitalWrite(motor1PinB, LOW);
digitalWrite(motor2PinA, LOW);
digitalWrite(motor2PinB, HIGH);
}
void Stop()
{
Serial.println("stop");
digitalWrite(motor1PinA, LOW);
digitalWrite(motor1PinB, LOW);
digitalWrite(motor2PinA, LOW);
digitalWrite(motor2PinB, LOW);
}