-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMain
155 lines (127 loc) · 3.17 KB
/
Main
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
# MazeSolver
//Initialize Pins
int directionPinRight = 2;
int directionPinLeft = 7;
int pwmPin = 5;
int pwmPin2 = 6;
int sleepPin = 4;
int sleepPin2 = 3;
int echo1 = 8;
int trigger1 = 9;
int echo2 = 11;
int trigger2 = 10;
double wheelRadius = 2.6;
int trigger3 = 13;
int echo3 = 12;
int LEFT = 3;
int RIGHT = 2;
int STRAIGHT = 1;
int OBSTACLE_CLOSE = 6;
float straightDistance = 0, rightDistance = 0, leftDistance = 0;
void setup() {
pinMode(6,OUTPUT);
pinMode(7,OUTPUT);
pinMode(2,OUTPUT);
pinMode(3,OUTPUT);
pinMode(4,OUTPUT);
pinMode(5,OUTPUT);
pinMode(trigger1, OUTPUT);
pinMode(echo1,INPUT);
pinMode(trigger2, OUTPUT);
pinMode(echo2,INPUT);
pinMode(trigger3, OUTPUT);
pinMode(echo3,INPUT);
Serial.begin(9600);
}
void loop() {
TurnOnWheels();
GoStraight();
// straightDistance = (straightDistance*0.8) + (0.2*GetDistance(STRAIGHT));
// rightDistance = (rightDistance*0.8) + (0.2*GetDistance(RIGHT));
// leftDistance = (leftDistance*0.8) + (0.2*GetDistance(LEFT));
straightDistance = GetDistance(STRAIGHT);
rightDistance = GetDistance(RIGHT);
leftDistance = GetDistance(LEFT);
if (straightDistance < OBSTACLE_CLOSE) {
if (leftDistance < OBSTACLE_CLOSE && rightDistance < OBSTACLE_CLOSE) {
LeftTurn90();
LeftTurn90();
}
else if (leftDistance > rightDistance) {
LeftTurn90();
}
else if (rightDistance > leftDistance) {
RightTurn90();
}
}
}
void inchesToTravel(int x) {
return;
}
void GoStraight() {
digitalWrite(directionPinRight,HIGH);
digitalWrite(directionPinLeft,LOW);
analogWrite(pwmPin,127);
analogWrite(pwmPin2,127);
return;
}
void RightTurn90() {
digitalWrite(directionPinRight,LOW);
digitalWrite(directionPinLeft,LOW);
delay(525);
return;
}
void LeftTurn90() {
digitalWrite(directionPinRight,HIGH);
digitalWrite(directionPinLeft,HIGH);
delay(525);
return;
}
void TurnOnWheels () {
digitalWrite(sleepPin,HIGH);
digitalWrite(sleepPin2,HIGH);
analogWrite(pwmPin,127);
analogWrite(pwmPin2,127);
return;
}
void TurnOffWheels () {
digitalWrite(sleepPin,LOW);
digitalWrite(sleepPin2,LOW);
analogWrite(pwmPin,0);
analogWrite(pwmPin2,0);
return;
}
double GetDistance(int directions) {
double duration, distance;
if (directions == STRAIGHT) {
digitalWrite(trigger1,LOW);
delayMicroseconds(2);
digitalWrite(trigger1,HIGH);
delayMicroseconds(10);
duration = pulseIn(echo1,HIGH);
distance = (duration/2)*0.0343;
Serial.println("distance1 = ");
//Serial.println(distance);
}
else if (directions == RIGHT) {
digitalWrite(trigger2,LOW);
delayMicroseconds(2);
digitalWrite(trigger2,HIGH);
delayMicroseconds(10);
duration = pulseIn(echo2,HIGH);
distance = (duration/2)*0.0343;
Serial.println("distance2 = ");
//Serial.println(distance);
}
else if (directions == LEFT) {
digitalWrite(trigger3,LOW);
delayMicroseconds(3);
digitalWrite(trigger3,HIGH);
delayMicroseconds(10);
duration = pulseIn(echo3,HIGH);
distance = (duration/2)*0.0343;
Serial.println("distance3 = ");
//Serial.println(distance);
}
return distance;
}