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
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
| #include <Servo.h>
#include <Wire.h> //I2C Arduino Library
#define addr 0x1E //I2C Address for The HMC5883
const int MPU_addr=0x68; // I2C address of the MPU-6050
const int echoPin = 5;
const int trigPin = 6;
// Create a Servo object for each servo
Servo servo1;
Servo servo2;
Servo servo3;
Servo servo4;
int E1 = 10;
int E2 = 11;
int M1 = 12;
int M2 = 13;
int startbyte; // start byte, begin reading input
int i; // iterator
int var1; // which motor to pulse?
int var2; // speed / servo angle 0-180
int userInput[3]; // raw input from serial buffer, 3 bytes
int minPulse = 600; // minimum servo position, us (microseconds)
int maxPulse = 2400; // maximum servo position, us
long duration;
int distance;
int gyrox;
int gyroy;
int gyroz;
byte error, address;
int nDevices;
// Buzzer - Pin 4
const int buzzer = 4;
void setup()
{
//SET PINS FOR SERVOS
servo1.attach(2, minPulse, maxPulse);
servo2.attach(9, minPulse, maxPulse);
servo3.attach(7, minPulse, maxPulse);
servo4.attach(3, minPulse, maxPulse);
pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
pinMode(echoPin, INPUT); // Sets the echoPin as an Input
//START I2C
Wire.begin();
// HMC5883 COMPASS ON I2C
Wire.beginTransmission(addr); //start talking
Wire.write(0x0D); // Set the Register
Wire.write(0x00); // Tell the HMC5883 to Continuously Measure
Wire.endTransmission();
// MPU650 GYRO ON I2C
Wire.setClock(400000UL); // Set I2C frequency to 400kHz
Wire.beginTransmission(MPU_addr);
Wire.write(0x6B);
Wire.write(0); // wake up the mpu6050
Wire.endTransmission(true);
Serial.begin(9600);
Serial.println("Droid! on Arduiono is starting up");
// START YOUR ENGINES
pinMode(M1, OUTPUT); // Sets M1 as an output
pinMode(M2, OUTPUT); // Sets M2 as an output
{
int value;
for(value = 0 ; value <= 255; value+=5)
{
digitalWrite(M1,HIGH);
digitalWrite(M2, HIGH);
analogWrite(E2, value);
analogWrite(E1, value);
delay(30);
}
delay(1000);
}
{
int value;
for(value = 0 ; value <= 255; value+=5)
{
digitalWrite(M1,LOW);
digitalWrite(M2, LOW);
analogWrite(E2, value);
analogWrite(E1, value);
delay(30);
}
delay(1000);
digitalWrite(M1,LOW);
digitalWrite(M2, LOW);
analogWrite(E2, 0);
analogWrite(E1, 0);
delay(30);
}
// SCAN I2C BUS FOR DEVICES
I2CScan();
// TEST GYRO
Gyro1();
// TEST COMPASS
Compass1();
// TEST DISTANCE
distance = Distance501();
Serial.println(String("501,")+ distance);
// TEST Buzzer
tone(buzzer, 1500);
delay(100);
noTone(buzzer);
delay(100);
tone(buzzer, 1500);
delay(100);
noTone(buzzer);
delay(100);
tone(buzzer, 1500);
delay(100);
noTone(buzzer);
}
void loop()
{
// Wait for serial input (min 3 bytes in buffer)
if (Serial.available() > 2) {
// Read the first byte
startbyte = Serial.read();
// If it's really the startbyte (255) ...
if (startbyte == 255) {
// ... then get the next two bytes
for (i=0;i<2;i++) {
userInput[i] = Serial.read();
}
// First byte = servo to move?
var1 = userInput[0];
// Second byte = which position?
var2 = userInput[1];
// Packet error checking and recovery
if (var2 == 255) { var1 = 255; }
// Assign new position to appropriate servo
switch (var1) {
case 1:
servo1.write(var2); // move servo1 to 'var2'
break;
case 2:
servo2.write(var2); // move servo2 to 'var2'
break;
case 3:
servo3.write(var2); // move servo3 to 'var2'
break;
case 4:
servo3.write(var2); // move servo3 to 'var2'
break;
case 5:
digitalWrite(M1, HIGH);
analogWrite(E1, var2);
break;
case 6:
digitalWrite(M2, HIGH);
analogWrite(E2, var2);
break;
case 7:
digitalWrite(M1, LOW);
analogWrite(E1, var2);
break;
case 8:
digitalWrite(M2, LOW);
analogWrite(E2, var2);
break;
// Empty
case 80:
Serial.println("Button Down - 80");
break;
case 81:
Serial.println("Button Up - 81");
break;
// Empty
case 82:
Serial.println("Button Down - 82");
break;
case 83:
Serial.println("Button Up - 83");
break;
// Empty
case 84:
Serial.println("Button Down - 84");
break;
case 85:
Serial.println("Button Up - 85");
break;
// Empty
case 86:
Serial.println("Button Down - 86");
break;
case 87:
Serial.println("Button Up - 87");
break;
// Empty
case 88:
Serial.println("Button Down - 88");
break;
case 89:
Serial.println("Button Up - 89");
break;
// Empty
case 90:
// Serial.println("Button Down - 90");
I2CScan();
break;
case 91:
//Serial.println("Button Up - 91");
break;
// Empty
case 92:
Gyro1();
break;
case 93:
// Serial.println("Button Up - 93");
break;
// Compass x,y,z
case 94:
//Serial.println("Button Down - 94");
Compass1();
break;
case 95:
// Serial.println("Button Up - 95");
break;
// UltraSonic Distance calculation
case 96:
distance = Distance501();
Serial.println(String("501,")+ distance);
break;
case 97:
break;
// Buzzer
case 98:
Serial.println("Button Down - 98 - Buzzer On");
// Send 1KHz sound signal
tone(buzzer, 1500);
break;
case 99:
Serial.println("Button up - 99 - Buzzer Off");
// Stop sound...
noTone(buzzer);
break;
}
}
}
}
int Compass1(){
int x,y,z; //triple axis data
//Read the data.. 2 bytes for each axis.. 6 total bytes
Wire.requestFrom(addr, 6);
if(6<=Wire.available()){
x = Wire.read()<<8; //MSB x
x |= Wire.read(); //LSB x
z = Wire.read()<<8; //MSB z
z |= Wire.read(); //LSB z
y = Wire.read()<<8; //MSB y
y |= Wire.read(); //LSB y
}
// Show Values
Serial.println(String("500,x=")+ x + "y=" + y + "z=" + z);
}
int Distance501(){
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
// Sets the trigPin on HIGH state for 10 micro seconds
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH); // Reads the echoPin, returns the sound wave travel time in microseconds
distance= duration*0.034/2;
return distance;
}
int Gyro1(){
gyrox = Gyro_X();
gyroy = Acc_Y();
gyroz = Acc_Z();
Serial.println(String("511,x=")+ gyrox + "y=" + gyroy + "z=" + gyroz);
}
double Gyro_X()
{
Wire.beginTransmission(MPU_addr);
Wire.write(0x43);
Wire.endTransmission(false);
Wire.requestFrom(MPU_addr,2);
double GyX=Wire.read()<<8|Wire.read();
Wire.endTransmission(true);
return GyX;
}
double Acc_Y()
{
Wire.beginTransmission(MPU_addr);
Wire.write(0x3D);
Wire.endTransmission(false);
Wire.requestFrom(MPU_addr,2);
double AccY=Wire.read()<<8|Wire.read();
Wire.endTransmission(true);
return AccY;
}
double Acc_Z()
{
Wire.beginTransmission(MPU_addr);
Wire.write(0x3F);
Wire.endTransmission(false);
Wire.requestFrom(MPU_addr,2);
double AccZ=Wire.read()<<8|Wire.read();
Wire.endTransmission(true);
return AccZ;
}
int I2CScan(){
nDevices = 0;
for(address = 1; address < 127; address++ )
{
// The i2c_scanner uses the return value of
// the Write.endTransmisstion to see if
// a device did acknowledge to the address.
Wire.beginTransmission(address);
error = Wire.endTransmission();
if (error == 0)
{
Serial.print("I2C device found at address 0x");
if (address<16)
Serial.print("0");
Serial.print(address,HEX);
Serial.println(" !");
nDevices++;
}
else if (error==4)
{
Serial.print("Unknown error at address 0x");
if (address<16)
Serial.print("0");
Serial.println(address,HEX);
}
}
if (nDevices == 0)
Serial.println("No I2C devices found\n");
else
Serial.println("done\n");
} |