-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathNav2.ino
497 lines (473 loc) · 12.7 KB
/
Nav2.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
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
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
#include <Adafruit_NeoPixel.h>
#include <SparkFun_APDS9960.h>
#include<SoftwareSerial.h>
#include <Wire.h>
#include <Adafruit_Sensor.h>
#include <HMC5883L.h>
#include <U8g2lib.h>
#ifdef U8X8_HAVE_HW_SPI
#include <SPI.h>
#endif
#define PIN 6 // Pin NeoWheel is on
#define APDS9960_INT 2 //interupt pin
char Serialdata;
HMC5883L compass;
// Initiate Display ///////////////////////////////////////////////////////////////////////////////////////////
U8G2_SSD1306_128X64_NONAME_1_4W_SW_SPI u8g2(U8G2_R0, /* clock=*/ 10, /* data=*/ 9, /* cs=*/ U8X8_PIN_NONE, /* dc=*/ 11, /* reset=*/ 13);
// Initiate NeoPixel ///////////////////////////////////////////////////////////////////////////////////////////
Adafruit_NeoPixel strip = Adafruit_NeoPixel(12, PIN, NEO_GRB + NEO_KHZ800);
// Assign strip to heading directions
const int NW60=11;
const int NW30=10;
const int W0=9 ;
const int SW60=8 ;
const int SW30=7 ;
const int S0=6 ;
const int SE60 =5;
const int SE30=4 ;
const int E0=3 ;
const int NE60=2 ;
const int NE30 =1;
const int N0 =0 ;
// variable and initial value for "North" indicator
int indicator =0;
String data;
float heading;
float headingDegrees;
//APDS///////////////////////////////////////////////////////////////////////////////////////////
#define LIGHT_INT_HIGH 1000 // High light level for interrupt
#define LIGHT_INT_LOW 10 // Low light level for interrupt
int isr_flag = 0;
uint16_t ambient_light = 0;
uint16_t threshold = 0;
SparkFun_APDS9960 apds = SparkFun_APDS9960();
///////////////////////////////////////////////////////////////////////////////////////////
// communication hardware
SoftwareSerial mySerial(3,4); //(tx,rx)
void checkAmbient()
{
if ( isr_flag == 1 ) {
if(!apds.readAmbientLight(ambient_light)){
Serial.println("Error reading light values");
}
else{
// Serial.print("Interrupt! Ambient: ");
// Serial.println(ambient_light);
}
}
if(ambient_light>=1000){
strip.setBrightness(100);
}
else if(ambient_light>400 && ambient_light>1000){
strip.setBrightness(50);
}
else{
strip.setBrightness(10);
}
}
///////////////////////////////////////////////////////////////////////////////////////////
void setup(void)
{
strip.begin();
strip.show(); // Initialize all strip to 'off'
pinMode(APDS9960_INT, INPUT);
attachInterrupt(0, isr_flag = 1, FALLING);
Serial.begin(9600);
mySerial.begin(9600);
// Initialize APDS-9960 (configure I2C and initial values)
if ( apds.init() ) {
Serial.println(F("APDS-9960 initialization complete"));
} else {
Serial.println(F("Something went wrong during APDS-9960 init!"));
}
// Set high and low interrupt thresholds
if ( !apds.setLightIntLowThreshold(LIGHT_INT_LOW) ) {
Serial.println(F("Error writing low threshold"));
}
if ( !apds.setLightIntHighThreshold(LIGHT_INT_HIGH) ) {
Serial.println(F("Error writing high threshold"));
}
// Start running the APDS-9960 light sensor (no interrupts)
if ( apds.enableLightSensor(false) ) {
Serial.println(F("Light sensor is now running"));
} else {
Serial.println(F("Something went wrong during light sensor init!"));
}
// Read high and low interrupt thresholds
if ( !apds.getLightIntLowThreshold(threshold) ) {
Serial.println(F("Error reading low threshold"));
} else {
Serial.print(F("Low Threshold: "));
Serial.println(threshold);
}
if ( !apds.getLightIntHighThreshold(threshold) ) {
Serial.println(F("Error reading high threshold"));
} else {
Serial.print(F("High Threshold: "));
Serial.println(threshold);
}
u8g2.begin();
u8g2.clearBuffer();
drawIntro();
delay(2000);
while (!compass.begin())
{
Serial.println("Could not find a valid HMC5883L sensor, check wiring!");
delay(500);
while(1);
}
if(compass.begin())
{
// one cycle of red around the ring to signal we're good to go!
Start();
// Set measurement range
compass.setRange(HMC5883L_RANGE_1_3GA);
compass.setMeasurementMode(HMC5883L_CONTINOUS);
compass.setDataRate(HMC5883L_DATARATE_30HZ);
compass.setSamples(HMC5883L_SAMPLES_8);
compass.setOffset(0,0); // 0,0 default
}
}
///////////////////////////////////////////////////////////////////////////////////////////
void loop(void)
{
checkAmbient();
Vector norm = compass.readNormalize();
// Calculate heading
float heading = atan2(norm.YAxis, norm.XAxis);
// Set declination headingDegrees on your location and fix heading
// You can find your declination on: http://magnetic-declination.com/
// (+) Positive or (-) for negative
// For Bytom / Mumbai declination headingDegrees is 0.18 (positive)
// Formula: (deg + (min / 60.0)) / (180 / M_PI);
float declinationheadingDegrees = (0.18 - (26.0 / 60.0)) / (180 / M_PI);
heading += declinationheadingDegrees;
// Normalize to 0-360
if (heading < 0)
{
heading += 2 * PI;
}
if (heading > 2 * PI)
{
heading -= 2 * PI;
}
headingDegrees = heading * 180/M_PI;
// Serial.print(" Degress = ");
// Serial.println(headingDegrees);
// use heading to set & show Neostrip on ring
BLEData();
setIndicator();
u8g2.firstPage();
do {
drawCompass();
} while( u8g2.nextPage() );
showIndicator();
delay(50); // just to let things settle a bit
}
///////////////////////////////////////////////////////////////////////////////////////////
void setIndicator(){
if ((headingDegrees >= 0.25)&&(headingDegrees < 90.25)); // NNE to E
{
if ((headingDegrees >= 0.25) && (headingDegrees < 33.75))
{
indicator = NE30;
// Serial.println("NE30");
data="NE30";
}
if ((headingDegrees >= 33.75) && (headingDegrees < 60.25))
{
indicator = NE60;
// Serial.println("NE60");
data="NE60";
}
if ((headingDegrees >= 60.25) && (headingDegrees < 90.25))
{
indicator = E0;
//Serial.println("E0");
data="E0";
}
} //end if NNE to E
if ((headingDegrees >= 90.25) && (headingDegrees < 180.25)) // ESE to S
{
if ((headingDegrees >= 90.25) && (headingDegrees < 120.75))
{
indicator = SE30;
// Serial.println("SE30");
data="SE30";
}
if ((headingDegrees >= 120.75) && (headingDegrees < 150.25))
{
indicator = SE60;
//Serial.println("SE60");
data="SE60";
}
if ((headingDegrees >= 150.25) && (headingDegrees < 180.25))
{
indicator = S0;
// Serial.println("S0");
data="S0";
}
} //end if ESE to S
if ((headingDegrees < 270.25) && (headingDegrees > 180.25)) // SSW to W
{
if ((headingDegrees >= 180.25) && (headingDegrees < 210.75))
{
indicator = SW30;
//Serial.println("SW30");
data="SW30";
}
if ((headingDegrees >= 210.75) && (headingDegrees < 240.25))
{
indicator = SW60;
// Serial.println("SW60");
}
if ((headingDegrees >= 240.25) && (headingDegrees < 270.25))
{
indicator = W0;
// Serial.println("W0");
data="W0";
} //end if WSW
}
//end if SSW to W
if ((headingDegrees >= 270.25) || (headingDegrees < 0.25)) // WNW to N
{
if ((headingDegrees >= 270.25) && (headingDegrees < 300.75))
{
indicator = NW30;
// Serial.println("NW30");
data="NW30";
} //end if WNW
if ((headingDegrees >= 300.75) && (headingDegrees < 330.25))
{
indicator = NW60;
// Serial.println("NW60");
data="NW60";
} //end if NW
if ((headingDegrees >= 330.25) || (headingDegrees < 0.25))
{
indicator = N0;
// Serial.println("N0");
data="N0";
} //end if N
} // end if WNW to N
}
///////////////////////////////////////////////////////////////////////////////////////////
void showIndicator()
{
// set a little border to highlight the North indicator
int indicatorLeft = indicator - 1;
int indicatorRight = indicator + 1;
// scale / normalize to 0 - 11
if (indicatorLeft < 0)
{
indicatorLeft += 12;
}
if (indicatorRight > 11)
{
indicatorRight -= 12;
}
colorWipe(strip.Color(0, 0, 255), 0); //set All Blue (background dial color)
strip.setPixelColor(indicator, 255, 255, 255); // set indicator White
strip.setPixelColor(indicatorLeft, 0, 255, 0); // set indicator border GREEN
strip.setPixelColor(indicatorRight, 0, 255, 0); // set indicator border GREEN
strip.show(); // Push bits out!
}
///////////////////////////////////////////////////////////////////////////////////////////
void Start()
{
strip.clear();
checkAmbient();
StartrainbowCycle();
}
void StartrainbowCycle()
{
strip.clear();
uint16_t i, j;
for(j=0; j<256; j++) //1 Cycle
{
for(i=0; i< strip.numPixels(); i++)
{
strip.setPixelColor(i, Wheel(((i * 256 / strip.numPixels()) + j) & 255));
}
strip.show();
delay(5);
}
}
uint32_t Wheel(byte WheelPos)
{
WheelPos = 255 - WheelPos;
if(WheelPos < 85) {
return strip.Color(255 - WheelPos * 3, 0, WheelPos * 3);
}
if(WheelPos < 170) {
WheelPos -= 85;
return strip.Color(0, WheelPos * 3, 255 - WheelPos * 3);
}
WheelPos -= 170;
return strip.Color(WheelPos * 3, 255 - WheelPos * 3, 0);
}
///////////////////////////////////////////////////////////////////////////////////////////
void colorWipe(uint32_t c, uint8_t wait) {
for(uint16_t i=0; i<strip.numPixels(); i++) {
strip.setPixelColor(i, c);// end void showIndicator()
strip.show();
delay(wait);
}
}
///////////////////////////////////////////////////////////////////////////////////////////
void drawCompass() {
static int armLength = 20;
static int cx = 80;
static int cy = 20;
int armX, armY;
//convert degree to radian
float bearingRad = PI * (float) headingDegrees / 180.0;
armX = armLength*cos(bearingRad);
armY = -armLength*sin(bearingRad);
u8g2.setFont(u8g2_font_ncenB10_tr);
u8g2.setCursor(0, 60);
u8g2.print("Compass : ");
u8g2.setCursor(80, 60);
u8g2.print(headingDegrees);
u8g2.setCursor(0,20);
u8g2.print(data);
u8g2.drawLine(cx, cy, cx-armX, cy-armY);
u8g2.drawCircle(cx, cy, armLength, U8G2_DRAW_ALL);
}
///////////////////////////////////////////////////////////////////////////////////////////
void drawIntro() {
u8g2.firstPage(); // draw intro
do {
u8g2.setFont(u8g2_font_ncenB14_tr);
u8g2.setCursor(10,40);
u8g2.print("SmartNav");
u8g2.sendBuffer();
} while ( u8g2.nextPage() );
}
///////////////////////////////////////////////////////////////////////////////////////////
void BLEData()
{
while(mySerial.available())
{
Serialdata = mySerial.read();
}
switch(Serialdata)
{
case '0':
Serial.println("CLEAR");
for (int i=0; i<12;i++)
{
strip.clear();
strip.show();
}
break;
case '1':
Serial.println("Start");
Start();
break;
case '2':
Serial.println("Forward");
forward();
break;
case '3':
Serial.println("RIGHT");
Right();
break;
case '4':
Serial.println("LEFT");
left();
break;
case '5':
Serial.println("UTurn");
uTurn();
break;
case '6':
Serial.println("Start");
DestinationReach();
break;
}
}
void forward()
{
strip.clear();
for (int i=3; i<=9;i++)
{
strip.setPixelColor(i, strip.Color(0,250,0)); // Moderately bright green color.
strip.show();
}
}
void uTurn()
{
strip.clear();
for (int i=9; i<=11;i++)
{
for(int j=0;j<=3;j++)
{
strip.setPixelColor(i, strip.Color(250,0,0)); // bright red color.
strip.setPixelColor(j, strip.Color(250,0,0));
strip.show();
}
}
delay(500);
for (int i=9; i<=11;i++)
{
for(int j=0;j<=3;j++)
{
strip.setPixelColor(i, strip.Color(0,0,0));
strip.setPixelColor(j, strip.Color(0,0,0));
strip.show();
}
}
delay(500);
}
void Right()
{
strip.clear();
for(int i =6;i<=12;i++)
{
strip.setPixelColor(i, strip.Color(0,250,0)); // bright green color.
strip.setPixelColor(0, strip.Color(0,250,0)); // bright green color.
strip.show();
}
strip.setPixelColor(9, strip.Color(250,250,250)); // bright green color.
delay(500);
strip.show();
strip.setPixelColor(9, strip.Color(0,0,0)); // bright green color.
delay(500);
strip.show();
}
void left()
{
strip.clear();
for(int i =0;i<=3;i++)
{
for(int j=4;j<=6;j++)
{
strip.setPixelColor(i, strip.Color(0,250,0)); // bright green color.
strip.setPixelColor(j, strip.Color(0,250,0)); // bright green color.
strip.show();
}
}
strip.setPixelColor(3, strip.Color(250,250,250)); // bright green color.
delay(500);
strip.show();
strip.setPixelColor(3, strip.Color(0,0,0)); // bright green color.
delay(500);
strip.show();
}
void DestinationReach()
{
strip.clear();
for(int i=0;i<=12;i++)
{
strip.setPixelColor(i, strip.Color(180,180,180)); // bright green color.
strip.show();
}
delay(250);
for(int i=0;i<=12;i++)
{
strip.setPixelColor(i, strip.Color(0,0,0)); // bright green color.
strip.show();
}
delay(250);
}