-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCSAEA-Maze-Robot.ino
164 lines (126 loc) · 3.09 KB
/
CSAEA-Maze-Robot.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
#include <Servo.h>
//Variables
const int motor1a = 10;
const int motor1b = 9;
const int motor2a = 6;
const int motor2b = 5;
const int turnTime = 695;
Servo servo;
int servoPosition = 0;
const int trig = 4;
const int echo = 3;
float leftDistance = 0.0;
float frontDistance;
float rightDistance;
void setup() {
Serial.begin(9600);
pinSetup();
}
void loop() {
getForwardDistance();
forward();
// Drive forward until abstructed and turn or reverse according to scan results
if (frontDistance < 5) {
stopMotors();
scan();
// If the front and left sides are blocked turn right
if (leftDistance > 10) turnRight();
else if (rightDistance > 10) turnLeft();
else backUpAndScan();
}
}
// *** F U N C T I O N S ***
void scan() {
n // Rotate servo to left position and record distance
servo.write(10);
delay(500);
leftDistance = distance();
Serial.println(leftDistance);
// Rotate servo to right position and record distance
servo.write(170);
delay(500);
rightDistance = distance();
Serial.println(rightDistance);
}
// Read the distance in front of the robot
void getForwardDistance() {
servo.write(90);
delay(500);
frontDistance = distance();
Serial.println(frontDistance);
}
// Backup until opening is seen on left
void backUpAndScan() {
servo.write(170);
delay(500);
leftDistance = distance();
while(leftDistance < 5){
backwards();
leftDistance = distance();
}
servo.write(90);
turnLeft();
delay(500);
}
// Returns the distance from the ultra sonic sensor
float distance() {
float echoTime; // Stores the echo time of a ping
float calculatedDistance; // Stores the distance calculated from the echo time
// Send 10ms ultrasonic pulse
digitalWrite(trig, HIGH);
delayMicroseconds(10);
digitalWrite(trig, LOW);
echoTime = pulseIn(echo, HIGH);
calculatedDistance = echoTime / 148.0;
return calculatedDistance;
}
void forward() {
digitalWrite(motor1a, HIGH);
digitalWrite(motor1b, LOW);
digitalWrite(motor2a, HIGH);
digitalWrite(motor2b, LOW);
}
void backwards() {
digitalWrite(motor1a, LOW);
digitalWrite(motor1b, HIGH);
digitalWrite(motor2a, LOW);
digitalWrite(motor2b, HIGH);
}
// Turn robot left 90 degrees
void turnLeft() {
digitalWrite(motor1a, HIGH);
digitalWrite(motor1b, LOW);
digitalWrite(motor2a, LOW);
digitalWrite(motor2b, LOW);
delay(turnTime);
digitalWrite(motor1a, LOW);
}
// Turn robot right 90 degrees
void turnRight() {
digitalWrite(motor1a, LOW);
digitalWrite(motor1b, LOW);
digitalWrite(motor2a, HIGH);
digitalWrite(motor2b, LOW);
delay(turnTime);
digitalWrite(motor2a, LOW);
}
// Stop robot movement
void stopMotors() {
digitalWrite(motor1a, LOW);
digitalWrite(motor1b, LOW);
digitalWrite(motor2a, LOW);
digitalWrite(motor2b, LOW);
}
//PinSetup
void pinSetup() {
// Set motor controller pin modes
pinMode(motor1a, OUTPUT);
pinMode(motor1b, OUTPUT);
pinMode(motor2a, OUTPUT);
pinMode(motor2b, OUTPUT);
// Set servo pin to 11
servo.attach(11);
// Set ultrasonic sensor pin modes
pinMode(trig, OUTPUT);
pinMode(echo, INPUT);
}