-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathOrientationSensor.ino
More file actions
201 lines (123 loc) · 4.38 KB
/
OrientationSensor.ino
File metadata and controls
201 lines (123 loc) · 4.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
#include <Wire.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_BNO055.h>
#include <utility/imumaths.h>
/* Set the delay between fresh samples EXPECT HIGH REFRESH*/
#define BNO055_SAMPLERATE_DELAY_MS (10)
float horizontal_angle = 0;
float horizontal_offset = 0;
float verticle_angle = 0;
float verticle_offset = 0;
const int buttonGndPin = 6;
const int buttonPin = 7;
int buttonState = 0;
int prevButtonState = 0;
int incoming = 0;
Adafruit_BNO055 bno = Adafruit_BNO055(55);
void displaySensorDetails(void)
{
sensor_t sensor;
bno.getSensor(&sensor);
Serial.println("------------------------------------");
Serial.print ("Sensor: "); Serial.println(sensor.name);
Serial.print ("Driver Ver: "); Serial.println(sensor.version);
Serial.print ("Unique ID: "); Serial.println(sensor.sensor_id);
Serial.print ("Max Value: "); Serial.print(sensor.max_value); Serial.println(" xxx");
Serial.print ("Min Value: "); Serial.print(sensor.min_value); Serial.println(" xxx");
Serial.print ("Resolution: "); Serial.print(sensor.resolution); Serial.println(" xxx");
Serial.println("------------------------------------");
Serial.println("");
Serial.println("waiting 2 seconds");
delay(2000);
}
/**************************************************************************/
/*
Arduino setup function (automatically called at startup)
*/
/**************************************************************************/
void setup(void)
{
Serial.begin(9600);
Serial.println("\r\nORIENTATION SENSOR STARTED");
delay(1000);
/* Initialise the sensor */
if(!bno.begin())
{
/* There was a problem detecting the BNO055 ... check your connections */
Serial.print("Ooops, no BNO055 detected ... Check your wiring or I2C ADDR!");
while(1);
}
delay(1000);
/* Display some basic information on this sensor */
displaySensorDetails();
bno.setExtCrystalUse(true);
pinMode(buttonGndPin, OUTPUT);
digitalWrite(buttonGndPin, LOW);
pinMode(buttonPin, INPUT);
delay(3000);
Mouse.begin();
}
/**************************************************************************/
/*
Arduino loop function, called once 'setup' is complete (your own code
should go here)
*/
/**************************************************************************/
void loop(void)
{
/* Get a new sensor event */
sensors_event_t event;
bno.getEvent(&event);
//CONVERT OUTPUT FROM Adafruit_BNO055
//**************************************************
if(event.orientation.x > 180){
horizontal_angle = (event.orientation.x -360) + horizontal_offset;
}else{
horizontal_angle = event.orientation.x + horizontal_offset;
}
verticle_angle = event.orientation.y + verticle_offset;
//CONVERT OUTPUT FROM Adafruit_BNO055
//get current button state
buttonState = digitalRead(buttonPin);
//DISPLAY DATA FROM Adafruit_BNO055
//*************************************************************
Serial.print("X: ");
Serial.print(event.orientation.x, 4);
Serial.print(" horizontal angle: ");
Serial.print(horizontal_angle, 4);
Serial.print(" horizontal offest: ");
Serial.print(horizontal_offset, 4);
Serial.print(" Y: ");
Serial.print(event.orientation.y, 4);
Serial.print(" verticle angle: ");
Serial.print(verticle_angle, 4);
Serial.print(" verticle offest: ");
Serial.print(verticle_offset, 4);
Serial.print(" Z: ");
Serial.print(event.orientation.z, 4);
Serial.print(" button: ");
Serial.print(buttonState);
Serial.print(" millis:: ");
Serial.print(millis());
Serial.println("");
//ACT ON DATA
//************************************************************
//if button is on then move mouse
if(buttonState==1){
Mouse.move((int)horizontal_angle, (int)verticle_angle, 0);
}
//check that the button was off but is now on (change of state)
if(buttonState==1&&prevButtonState==0){
Serial.println("RESET CENTER");
if(event.orientation.x > 180){
horizontal_offset = -(event.orientation.x -360);
}else{
horizontal_offset = -event.orientation.x;
}
verticle_offset = -(event.orientation.y);
}
//save the current button state for the next loop
prevButtonState = buttonState;
//************************************************************
delay(BNO055_SAMPLERATE_DELAY_MS);
}