-
Notifications
You must be signed in to change notification settings - Fork 0
/
motor.ino
106 lines (90 loc) · 1.91 KB
/
motor.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
// Developed by Saad Bin Khalid
// Contact : [email protected]
#include "Car.h"
Car myCar(150); // car with speed 150 (range: 0-255)
double left, right, mid; // to store readings of sensors
const double safe = 10, danger = 3, minSafe = 6; // distance in inches
// to make sure car stays in the safe zone
void adjust()
{
if ( mid <= danger ) // middle sensor is in danger?
{
while (mid <= minSafe )
{
myCar.back();
mid = myCar.SignalMid();
}
}
if ( right <= danger || left <= danger) // left or right sensor is in danger?
{
while (right <= minSafe || left <= minSafe)
{
myCar.back();
right = myCar.SignalRight();
left = myCar.SignalLeft();
}
}
}
void setup()
{
// nothing
}
void loop()
{
// read sensors:
left = myCar.SignalLeft();
right = myCar.SignalRight();
mid = myCar.SignalMid();
// if there is nothing in the way
if (left > safe && mid > safe && right > safe)
{
myCar.forward();
}
// if right detects something
if (left > safe && mid > safe && right <= safe)
{
adjust();
myCar.left();
}
// if mid detects something
if (left > safe && mid <= safe && right > safe)
{
adjust();
if (right < left)
myCar.left();
else
myCar.right();
}
// if left is open only
if (left > safe && mid <= safe && right <= safe)
{
adjust();
myCar.left();
}
// if left detects something
if (left <= safe && mid > safe && right > safe)
{
adjust();
myCar.right();
}
// if mid is open only
if (left <= safe && mid > safe && right <= safe)
{
adjust();
if (right < left)
myCar.left();
else
myCar.right();
}
// if right is open only
if (left <= safe && mid <= safe && right > safe)
{
adjust();
myCar.right();
}
// if the way is blocked completely
if (left <= safe && mid <= safe && right <= safe)
{
adjust();
}
}