-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfull_code.ino
More file actions
243 lines (214 loc) · 6.38 KB
/
full_code.ino
File metadata and controls
243 lines (214 loc) · 6.38 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
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
/******************************************************************************
MLX90614 ------------- Arduino
VDD ------------------ 3.3V
VSS ------------------ GND
SDA ------------------ SDA (A4 on older boards) WHITE
SCL ------------------ SCL (A5 on older boards) YELLOW
******************************************************************************/
/*Pin 9 sets the enable and speed of shield outputs 1 & 2
Pin 10 sets the enable and speed of shield outputs 3 & 4
Pin 8 from Uno controls the state of shield output 1
Pin 11 from Uno controls the state of shield output 2
Pin 12 from Uno controls the state of shield output 3
Pin 13 from Uno controls the state of shield output 4
*/
#include <Wire.h> // I2C library, required for MLX90614
#include <SparkFunMLX90614.h> // SparkFunMLX90614 Arduino library
#include <Servo.h>
IRTherm therm; // Create an IRTherm object to interact with throughout
int right_motor_pos = 8; // pin 8 --- motor (+) white wire
int right_motor_neg = 11; // pin 11 --- motor (-) black wire
int right_motor_speed = 9; // pin 9 --- motor speed signal
int left_motor_pos = 12; // pin 12 --- motor (+) white wire
int left_motor_neg = 13; // pin 13 --- motor (-) black wire
int left_motor_speed = 10; // pin 10 --- motor speed signal
#define trigPin 5
#define echoPin 6
long duration, cm, inches;
Servo head;
int pos;
int heat_direction;
int temp;
long distance;
void setup() {
Serial.begin(9600);
therm.begin(); // Initialize thermal IR sensor
therm.setUnit(TEMP_F); // Set the library's units to Farenheit
head.attach(2);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
pinMode(left_motor_pos, OUTPUT);
pinMode(left_motor_neg, OUTPUT);
pinMode(left_motor_speed, OUTPUT);
pinMode(right_motor_pos, OUTPUT);
pinMode(right_motor_neg, OUTPUT);
pinMode(right_motor_speed, OUTPUT);
head.write(53); /* Center Head*/
}
void loop(){
go_forward();
do {
distance = distance_read();
temp = temp_read();
delay(25);
} while (distance >= 45 and temp < 75);
if (distance < 45 and temp < 75) {
go_backward(500);
turn_left(500);
} else if (temp >= 75){
analogWrite(left_motor_speed,0);
analogWrite(right_motor_speed,0);
heat_direction = heat_search();
if (heat_direction == 1) {
turn_left(200);
delay(300);
temp = temp_read();
if (temp >= 75){
go_forward();
do {
distance = distance_read();
temp = temp_read();
delay(25);
} while (distance >= 45 and temp >= 75);
if (distance < 45) {
analogWrite(left_motor_speed,0);
analogWrite(right_motor_speed,0);
do {
//Serial.println("See human left");
distance = distance_read();
delay(25);
} while (distance < 45);
}
}
} else if (heat_direction == 2) {
turn_right(300);
delay(300);
temp = temp_read();
if (temp >= 75){
go_forward();
do {
distance = distance_read();
temp = temp_read();
delay(25);
} while (distance >= 45 and temp >= 75);
if (distance < 45) {
analogWrite(left_motor_speed,0);
analogWrite(right_motor_speed,0);
do {
//Serial.println("See human right");
distance = distance_read();
delay(25);
} while (distance < 45);
}
}
} else if (heat_direction == 3) {
temp = temp_read();
if(temp >= 75){
go_forward();
do {
distance = distance_read();
temp = temp_read();
delay(25);
} while (distance >= 45 and temp >= 75);
if (distance < 45) {
analogWrite(left_motor_speed,0);
analogWrite(right_motor_speed,0);
do {
//Serial.println("See human center");
distance = distance_read();
delay(25);
} while (distance < 45);
}
}
}
}
}
void go_forward() {
analogWrite(left_motor_speed,255);
analogWrite(right_motor_speed,255);
digitalWrite(left_motor_neg,LOW);
digitalWrite(right_motor_pos,LOW);
digitalWrite(left_motor_pos,HIGH);
digitalWrite(right_motor_neg,HIGH);
}
void go_backward(int duration){
analogWrite(left_motor_speed,255);
analogWrite(right_motor_speed,255);
digitalWrite(left_motor_pos,LOW);
digitalWrite(right_motor_neg,LOW);
digitalWrite(left_motor_neg,HIGH);
digitalWrite(right_motor_pos,HIGH);
delay(duration);
}
void turn_right(int duration){
analogWrite(left_motor_speed,255);
analogWrite(right_motor_speed,255);
digitalWrite(right_motor_neg,LOW);
digitalWrite(left_motor_neg,LOW);
digitalWrite(right_motor_pos,HIGH);
digitalWrite(left_motor_pos,HIGH);
delay(duration);
}
void turn_left(int duration) {
analogWrite(left_motor_speed,255);
analogWrite(right_motor_speed,255);
digitalWrite(right_motor_pos,LOW);
digitalWrite(left_motor_pos,LOW);
digitalWrite(right_motor_neg,HIGH);
digitalWrite(left_motor_neg,HIGH);
delay(duration);
}
long distance_read() {
digitalWrite(trigPin, LOW);
delayMicroseconds(5);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
pinMode(echoPin, INPUT);
duration = pulseIn(echoPin, HIGH);
cm = (duration/2) /29.1;
if (cm == 5 or cm > 3000) {
distance_read();
}
else {
return(cm);
}
}
long temp_read()
{
if (therm.read()) // On success, read() will return 1, on fail 0.
{
// Use the object() and ambient() functions to grab the object and ambient
// temperatures.
// They'll be floats, calculated out to the unit you set with setUnit().
/*Serial.println("Object: " + String(therm.object(), 2)); */
}
//Serial.println(therm.object());
return(therm.object());
}
long heat_search(){
float right_temp;
float left_temp;
float center_temp;
head.write(10);
delay(2000);
right_temp = temp_read();
head.write(96);
delay(2000);
left_temp = temp_read();
head.write(53);
delay(2000);
center_temp = temp_read();
if (left_temp < 75 and right_temp < 75 and center_temp < 75){
return(0); /* nothing */
} else if (left_temp > right_temp and left_temp > center_temp){
Serial.println("left");
return(1); /* left */
} else if (right_temp > left_temp and right_temp > center_temp){
Serial.println("right");
return(2); /* right */
} else {
Serial.println("center");
return(3); /* center */
}
}