-
Notifications
You must be signed in to change notification settings - Fork 0
/
CodeForArduinoNano.ino
75 lines (59 loc) · 1.72 KB
/
CodeForArduinoNano.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
#include <SoftwareSerial.h>
SoftwareSerial BT_Serial(2, 3); // RX, TX
#include <Wire.h> // I2C communication library
const int MPU = 0x68; // I2C address of the MPU6050 accelerometer
int16_t AcX, AcY, AcZ;
int flag = 0;
void setup() { // put your setup code here, to run once
Serial.begin(9600); // start serial communication at 9600bps
BT_Serial.begin(9600);
// Initialize interface to the MPU6050
Wire.begin();
Wire.beginTransmission(MPU);
Wire.write(0x6B);
Wire.write(0);
Wire.endTransmission(true);
delay(500);
}
void loop() {
Read_accelerometer(); // Read MPU6050 accelerometer
if (AcX < 60 && flag == 0) {
flag = 1;
BT_Serial.write('f');
}
if (AcX > 130 && flag == 0) {
flag = 1;
BT_Serial.write('b');
}
if (AcY < 60 && flag == 0) {
flag = 1;
BT_Serial.write('l');
}
if (AcY > 130 && flag == 0) {
flag = 1;
BT_Serial.write('r');
}
if ((AcX > 70) && (AcX < 120) && (AcY > 70) && (AcY < 120) && (flag == 1)) {
flag = 0;
BT_Serial.write('s');
}
delay(100);
}
void Read_accelerometer() {
// Read the accelerometer data
Wire.beginTransmission(MPU);
Wire.write(0x3B); // Start with register 0x3B (ACCEL_XOUT_H)
Wire.endTransmission(false);
Wire.requestFrom(MPU, 6, true); // Read 6 registers total, each axis value is stored in 2 registers
AcX = Wire.read() << 8 | Wire.read(); // X-axis value
AcY = Wire.read() << 8 | Wire.read(); // Y-axis value
AcZ = Wire.read() << 8 | Wire.read(); // Z-axis value
AcX = map(AcX, -17000, 17000, 0, 180);
AcY = map(AcY, -17000, 17000, 0, 180);
AcZ = map(AcZ, -17000, 17000, 0, 180);
Serial.print(AcX);
Serial.print("\t");
Serial.print(AcY);
Serial.print("\t");
Serial.println(AcZ);
}