-
Notifications
You must be signed in to change notification settings - Fork 775
/
Copy pathFlora_NeoGeo_Watch.ino
566 lines (481 loc) · 15.6 KB
/
Flora_NeoGeo_Watch.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
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
// SPDX-FileCopyrightText: 2019 Anne Barela for Adafruit Industries
//
// SPDX-License-Identifier: MIT
// Test code for Adafruit Flora GPS modules
//
// This code shows how to listen to the GPS module in an interrupt
// which allows the program to have more 'freedom' - just parse
// when a new NMEA sentence is available! Then access data when
// desired.
//
// Tested and works great with the Adafruit Flora GPS module
// ------> http://adafruit.com/products/1059
// Pick one up today atAdafruit https://www.adafruit.com/
// and help support open source hardware & software!
#include <Adafruit_GPS.h>
#include <Adafruit_NeoPixel.h>
#include <SoftwareSerial.h>
#include <TimeLib.h>
#include <Wire.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_LSM303DLH_Mag.h>
Adafruit_GPS GPS(&Serial1);
/* Assign a unique ID to this sensor at the same time */
Adafruit_LSM303DLH_Mag_Unified mag = Adafruit_LSM303DLH_Mag_Unified(12345);
// Set GPSECHO to 'false' to turn off echoing the GPS data to the Serial console
// Set to 'true' if you want to debug and listen to the raw GPS sentences
#define GPSECHO false
// this keeps track of whether we're using the interrupt
// off by default!
boolean usingInterrupt = false;
//--------------------------------------------------|
// WAYPOINT |
//--------------------------------------------------|
//Please enter the latitude and longitude of your |
//desired destination: |
#define GEO_LAT 48.009551
#define GEO_LON -88.771131
//--------------------------------------------------|
//Your NeoPixel ring may not line up with ours. |
//Enter which NeoPixel led is your top LED (0-15). |
#define TOP_LED 1
//--------------------------------------------------|
//Your compass module may not line up with ours. |
//Once you run compass mode, compare to a separate |
//compass (like one found on your smartphone). |
//Point your TOP_LED north, then count clockwise |
//how many LEDs away from TOP_LED the lit LED is |
#define LED_OFFSET 0
//--------------------------------------------------|
// Navigation location
float targetLat = GEO_LAT;
float targetLon = GEO_LON;
// Trip distance
float tripDistance;
Adafruit_NeoPixel strip = Adafruit_NeoPixel(16, 6, NEO_GRB + NEO_KHZ800);
// Offset hours from gps time (UTC)
//const int offset = 1; // Central European Time
//const int offset = -4; // Eastern Daylight Time (USA)
const int offset = -5; // Central Daylight Time (USA)
//const int offset = -8; // Pacific Standard Time (USA)
//const int offset = -7; // Pacific Daylight Time (USA)
int topLED = TOP_LED;
int compassOffset = LED_OFFSET;
int lastMin = 16;
int lastHour = 16;
int startLED = 0;
int startLEDlast = 16;
int lastCombined = 0;
int start = 0;
int mode = 0;
int lastDir = 16;
int dirLED_r = 0;
int dirLED_g = 0;
int dirLED_b = 255;
int compassReading;
// Calibration offsets
float magxOffset = 2.55;
float magyOffset = 27.95;
// Pushbutton setup
int buttonPin = 10; // the number of the pushbutton pin
int buttonState; // the current reading from the input pin
int lastButtonState = HIGH; // the previous reading from the input pin
long buttonHoldTime = 0; // the last time the output pin was toggled
long buttonHoldDelay = 2500; // how long to hold the button down
// the following variables are long's because the time, measured in miliseconds,
// will quickly become a bigger number than can be stored in an int.
long lastDebounceTime = 0; // the last time the output pin was toggled
long debounceDelay = 50; // the debounce time; increase if the output flickers
long menuDelay = 2500;
long menuTime;
float fLat = 0.0;
float fLon = 0.0;
void setup()
{
// connect at 115200 so we can read the GPS fast enough and echo without dropping chars
// also spit it out
Serial.begin(115200);
Serial.println("Adafruit GPS library basic test!");
// 9600 NMEA is the default baud rate for Adafruit MTK GPS's- some use 4800
GPS.begin(9600);
// uncomment this line to turn on RMC (recommended minimum) and GGA (fix data) including altitude
GPS.sendCommand(PMTK_SET_NMEA_OUTPUT_RMCGGA);
// uncomment this line to turn on only the "minimum recommended" data
//GPS.sendCommand(PMTK_SET_NMEA_OUTPUT_RMCONLY);
// For parsing data, we don't suggest using anything but either RMC only or RMC+GGA since
// the parser doesn't care about other sentences at this time
// Set the update rate
GPS.sendCommand(PMTK_SET_NMEA_UPDATE_1HZ); // 1 Hz update rate
// For the parsing code to work nicely and have time to sort thru the data, and
// print it out we don't suggest using anything higher than 1 Hz
// Request updates on antenna status, comment out to keep quiet
GPS.sendCommand(PGCMD_ANTENNA);
/* Initialise the sensor */
if(!mag.begin())
{
/* There was a problem detecting the LSM303 ... check your connections */
Serial.println("Ooops, no LSM303 detected ... Check your wiring!");
while(1);
}
// Ask for firmware version
Serial1.println(PMTK_Q_RELEASE);
strip.begin();
strip.show(); // Initialize all pixels to 'off'
// Make input & enable pull-up resistors on switch pins for pushbutton
pinMode(buttonPin, INPUT);
digitalWrite(buttonPin, HIGH);
}
uint32_t gpsTimer = millis();
uint32_t startupTimer = millis();
uint32_t compassTimer = millis();
void loop() // run over and over again
{
compassCheck();
// read the state of the switch into a local variable:
int buttonState = digitalRead(buttonPin);
if (buttonState == LOW) {
buttonCheck();
}
lastButtonState = buttonState;
//Serial.println(buttonState);
// read data from the GPS in the 'main loop'
char c = GPS.read();
// if you want to debug, this is a good time to do it!
if (GPSECHO)
if (c) Serial.print(c);
// if a sentence is received, we can check the checksum, parse it...
if (GPS.newNMEAreceived()) {
// a tricky thing here is if we print the NMEA sentence, or data
// we end up not listening and catching other sentences!
// so be very wary if using OUTPUT_ALLDATA and trytng to print out data
Serial.println(GPS.lastNMEA()); // this also sets the newNMEAreceived() flag to false
if (!GPS.parse(GPS.lastNMEA())) // this also sets the newNMEAreceived() flag to false
return; // we can fail to parse a sentence in which case we should just wait for another
}
// if millis() or timer wraps around, we'll just reset it
if (gpsTimer > millis()) gpsTimer = millis();
if (start == 0) {
if (GPS.fix) {
// set the Time to the latest GPS reading
setTime(GPS.hour, GPS.minute, GPS.seconds, GPS.day, GPS.month, GPS.year);
delay(50);
adjustTime(offset * SECS_PER_HOUR);
delay(500);
tripDistance = (double)calc_dist(fLat, fLon, targetLat, targetLon);
start = 1;
}
}
// approximately every 60 seconds or so, update time
if ((millis() - gpsTimer > 60000) && (start == 1)) {
gpsTimer = millis(); // reset the timer
if (GPS.fix) {
// set the Time to the latest GPS reading
setTime(GPS.hour, GPS.minute, GPS.seconds, GPS.day, GPS.month, GPS.year);
delay(50);
adjustTime(offset * SECS_PER_HOUR);
delay(500);
}
}
if (GPS.fix) {
fLat = decimalDegrees(GPS.latitude, GPS.lat);
fLon = decimalDegrees(GPS.longitude, GPS.lon);
}
if (mode == 0) {
clockMode();
}
if (mode == 1) {
navMode();
}
if (mode == 2) {
compassMode();
}
}
// Fill the dots one after the other with a color
void colorWipe(uint32_t c, uint8_t wait) {
for(uint16_t i=0; i<strip.numPixels(); i++) {
strip.setPixelColor(i, c);
strip.show();
delay(wait);
}
}
void buttonCheck() {
menuTime = millis();
int buttonState = digitalRead(buttonPin);
if (buttonState == LOW && lastButtonState == HIGH) {
buttonHoldTime = millis();
}
if (buttonState == LOW && lastButtonState == LOW) {
if ((millis() - buttonHoldTime) > buttonHoldDelay) {
if(mode == 2) {
mode = 0;
lastMin = 16;
lastHour = 16;
colorWipe(strip.Color(0, 0, 0), 20);
buttonHoldTime = millis();
}
else {
mode = mode + 1;
colorWipe(strip.Color(0, 0, 0), 20);
buttonHoldTime = millis();
}
}
}
}
void clockMode() {
if (start == 1) {
strip.setPixelColor(startLEDlast, strip.Color(0, 0, 0));
strip.show();
float gpsMin = (minute() + (second()/60.0));
unsigned int ledMin = 0;
int minTemp = 0;
minTemp = topLED - (gpsMin + 1.875)/3.75;
if (minTemp < 0) {
ledMin = minTemp + 16;
}
else {
ledMin = minTemp;
}
float gpsHour = (hour() + (minute()/60.0));
if (gpsHour > 12) {
gpsHour = gpsHour - 12;
}
unsigned int ledHour = 0;
int hourTemp = 0;
hourTemp = topLED - (gpsHour + .375)/.75;
if (hourTemp < 0) {
ledHour = hourTemp + 16;
}
else {
ledHour = hourTemp;
}
if ((ledHour == ledMin) && (lastCombined == 0)) {
strip.setPixelColor(lastHour, strip.Color(0, 0, 0));
strip.setPixelColor(lastMin, strip.Color(0, 0, 0));
strip.setPixelColor(ledHour, strip.Color(255, 0, 255));
strip.show();
lastCombined = 1;
lastHour = ledHour;
lastMin = ledMin;
}
else {
if (lastHour != ledHour) {
strip.setPixelColor(lastHour, strip.Color(0, 0, 0));
strip.setPixelColor(ledHour, strip.Color(255, 50, 0));
strip.show();
lastHour = ledHour;
}
if (lastMin != ledMin) {
strip.setPixelColor(lastMin, strip.Color(0, 0, 0));
strip.setPixelColor(ledMin, strip.Color(200, 200, 0));
if (lastCombined == 1) {
strip.setPixelColor(ledHour, strip.Color(255, 0, 0));
lastCombined = 0;
}
strip.show();
lastMin = ledMin;
}
}
}
else {
// if millis() or timer wraps around, we'll just reset it
if (startupTimer > millis()) startupTimer = millis();
// approximately every 10 seconds or so, update time
if (millis() - startupTimer > 200) {
startupTimer = millis(); // reset the timer
if (startLED == 16) {
startLED = 0;
}
strip.setPixelColor(startLEDlast, strip.Color(0, 0, 0));
strip.setPixelColor(startLED, strip.Color(0, 255, 0));
strip.show();
startLEDlast = startLED;
startLED++;
//delay(200);
}
}
}
void navMode() {
if (start == 1) {
compassCheck();
headingDistance((double)calc_dist(fLat, fLon, targetLat, targetLon));
if ((calc_bearing(fLat, fLon, targetLat, targetLon) - compassReading) > 0) {
compassDirection(calc_bearing(fLat, fLon, targetLat, targetLon)-compassReading);
}
else {
compassDirection(calc_bearing(fLat, fLon, targetLat, targetLon)-compassReading+360);
}
}
else {
// if millis() or timer wraps around, we'll just reset it
if (startupTimer > millis()) startupTimer = millis();
// approximately every 10 seconds or so, update time
if (millis() - startupTimer > 200) {
startupTimer = millis(); // reset the timer
if (startLED == 16) {
startLED = 0;
}
strip.setPixelColor(startLEDlast, strip.Color(0, 0, 0));
strip.setPixelColor(startLED, strip.Color(0, 0, 255));
strip.show();
startLEDlast = startLED;
startLED++;
}
}
}
int calc_bearing(float flat1, float flon1, float flat2, float flon2)
{
float calc;
float bear_calc;
float x = 69.1 * (flat2 - flat1);
float y = 69.1 * (flon2 - flon1) * cos(flat1/57.3);
calc=atan2(y,x);
bear_calc= degrees(calc);
if(bear_calc<=1){
bear_calc=360+bear_calc;
}
return bear_calc;
}
void headingDistance(int fDist)
{
//Use this part of the code to determine how far you are away from the destination.
//The total trip distance (from where you started) is divided into five trip segments.
float tripSegment = tripDistance/5;
if (fDist >= (tripSegment*4)) {
dirLED_r = 255;
dirLED_g = 0;
dirLED_b = 0;
}
if ((fDist >= (tripSegment*3))&&(fDist < (tripSegment*4))) {
dirLED_r = 255;
dirLED_g = 0;
dirLED_b = 0;
}
if ((fDist >= (tripSegment*2))&&(fDist < (tripSegment*3))) {
dirLED_r = 255;
dirLED_g = 255;
dirLED_b = 0;
}
if ((fDist >= tripSegment)&&(fDist < (tripSegment*2))) {
dirLED_r = 255;
dirLED_g = 255;
dirLED_b = 0;
}
if ((fDist >= 5)&&(fDist < tripSegment)) {
dirLED_r = 255;
dirLED_g = 255;
dirLED_b = 0;
}
if ((fDist < 5)) { // You are now within 5 meters of your destination.
//Serial.println("Arrived at destination!");
dirLED_r = 0;
dirLED_g = 255;
dirLED_b = 0;
}
}
unsigned long calc_dist(float flat1, float flon1, float flat2, float flon2)
{
float dist_calc=0;
float dist_calc2=0;
float diflat=0;
float diflon=0;
diflat=radians(flat2-flat1);
flat1=radians(flat1);
flat2=radians(flat2);
diflon=radians((flon2)-(flon1));
dist_calc = (sin(diflat/2.0)*sin(diflat/2.0));
dist_calc2= cos(flat1);
dist_calc2*=cos(flat2);
dist_calc2*=sin(diflon/2.0);
dist_calc2*=sin(diflon/2.0);
dist_calc +=dist_calc2;
dist_calc=(2*atan2(sqrt(dist_calc),sqrt(1.0-dist_calc)));
dist_calc*=6371000.0; //Converting to meters
return dist_calc;
}
// Convert NMEA coordinate to decimal degrees
float decimalDegrees(float nmeaCoord, char dir) {
uint16_t wholeDegrees = 0.01*nmeaCoord;
int modifier = 1;
if (dir == 'W' || dir == 'S') {
modifier = -1;
}
return (wholeDegrees + (nmeaCoord - 100.0*wholeDegrees)/60.0) * modifier;
}
void compassMode() {
dirLED_r = 0;
dirLED_g = 0;
dirLED_b = 255;
compassDirection(compassReading);
}
void compassCheck() {
// if millis() or timer wraps around, we'll just reset it
if (compassTimer > millis()) compassTimer = millis();
// approximately every 10 seconds or so, update time
if (millis() - compassTimer > 50) {
/* Get a new sensor event */
sensors_event_t event;
mag.getEvent(&event);
float Pi = 3.14159;
compassTimer = millis(); // reset the timer
// Calculate the angle of the vector y,x
float heading = (atan2(event.magnetic.y + magyOffset,event.magnetic.x + magxOffset) * 180) / Pi;
// Normalize to 0-360
if (heading < 0)
{
heading = 360 + heading;
}
compassReading = heading;
}
}
void compassDirection(int compassHeading)
{
//Serial.print("Compass Direction: ");
//Serial.println(compassHeading);
unsigned int ledDir = 2;
int tempDir = 0;
//Use this part of the code to determine which way you need to go.
//Remember: this is not the direction you are heading, it is the direction to the destination (north = forward).
if ((compassHeading > 348.75)||(compassHeading < 11.25)) {
tempDir = topLED;
}
for(int i = 1; i < 16; i++){
float pieSliceCenter = 45/2*i;
float pieSliceMin = pieSliceCenter - 11.25;
float pieSliceMax = pieSliceCenter + 11.25;
if ((compassHeading >= pieSliceMin)&&(compassHeading < pieSliceMax)) {
if (mode == 2 ) {
tempDir = topLED - i;
}
else {
tempDir = topLED + i;
}
}
}
if (tempDir > 15) {
ledDir = tempDir - 16;
}
else if (tempDir < 0) {
ledDir = tempDir + 16;
}
else {
ledDir = tempDir;
}
if (mode == 1) {
ledDir = ledDir + compassOffset;
if (ledDir > 15) {
ledDir = ledDir - 16;
}
}
else {
ledDir = ledDir + compassOffset;
if (ledDir > 15) {
ledDir = ledDir - 16;
}
}
if (lastDir != ledDir) {
strip.setPixelColor(lastDir, strip.Color(0, 0, 0));
strip.setPixelColor(ledDir, strip.Color(dirLED_r, dirLED_g, dirLED_b));
strip.show();
lastDir = ledDir;
}
}