/
GPS-Alt.ino
164 lines (128 loc) · 4.86 KB
/
GPS-Alt.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
// Elevation display by Kris Linquist
// Requires SunRise.h / TimeLib.h / Adafruit_GPS.h / SoftwareSerial.h / HK16K33.h
// Tested and works great with the Adafruit Ultimate GPS module
// using MTK33x9 chipset
// ------> http://www.adafruit.com/products/746
//And the Adafruit 7 segment I2C LCD
#include <Adafruit_GPS.h>
#include <SoftwareSerial.h>
#include <TimeLib.h>
#include <SunRise.h>
#include "HT16K33.h"
HT16K33 seg(0x70);
SunRise sr;
tmElements_t te; //Time elements structure
time_t unixTime; // a time stamp
int daytimeBrightness = 15;
int nighttimeBrightness = 0;
int currentBrightness;
// Connect the GPS Power pin to 5V
// Connect the GPS Ground pin to ground
// Connect the GPS TX (transmit) pin to Digital 8
// Connect the GPS RX (receive) pin to Digital 7
// you can change the pin numbers to match your wiring:
SoftwareSerial mySerial(8, 7);
Adafruit_GPS GPS(&mySerial);
// 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
//
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);
seg.begin();
Wire.setClock(100000);
seg.displayOn();
seg.displayClear();
//Just show 0.000 until we get a GPS fix
seg.displayFloat(0,4);
Serial.print("Startup: setting brightness to: ");
Serial.println(nighttimeBrightness);
seg.brightness(nighttimeBrightness);
currentBrightness = nighttimeBrightness;
delay(5000);
// 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);
delay(1000);
// Ask for firmware version
mySerial.println(PMTK_Q_RELEASE);
}
uint32_t timer = millis();
void loop() // run over and over again
{
char c = GPS.read();
// if you want to debug, this is a good time to do it!
if ((c) && (GPSECHO))
Serial.write(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
}
// approximately every 2 seconds or so, update the display
if (millis() - timer > 2000) {
timer = millis(); // reset the timer
te.Second = GPS.seconds;
te.Hour = GPS.hour;
te.Minute = GPS.minute;
te.Day = GPS.day;
te.Month = GPS.month;
int actualYear = 2000 + GPS.year;
te.Year = actualYear - 1970;
unixTime = makeTime(te);
float lata = GPS.latitude/100;
float longi = 0 - (GPS.longitude/100);
int tz = -8;
sr.calculate(lata, longi, unixTime);
Serial.print("Unixtime: ");
Serial.println(unixTime);
if (sr.isVisible){
Serial.println("It's daytime!");
if (currentBrightness != daytimeBrightness) {
Serial.println("Setting brightness to day");
seg.brightness(daytimeBrightness);
currentBrightness = daytimeBrightness;
}
} else {
Serial.println("It's nighttime!");
if (currentBrightness != nighttimeBrightness) {
Serial.println("Setting brightness to night");
seg.brightness(nighttimeBrightness);
currentBrightness = nighttimeBrightness;
}
}
Serial.print("Fix: "); Serial.print((int)GPS.fix);
Serial.print(" quality: "); Serial.println((int)GPS.fixquality);
if (GPS.fix) {
//Meters to feet...
double doubFt = GPS.altitude * 3.28084;
//Round to nearest 10
int mod = (int) doubFt % 10;
if (mod < 5) {
doubFt = doubFt - mod;
} else {
doubFt = doubFt + (10 - mod);
}
seg.displayInt((int) doubFt);
Serial.print("Satellites: "); Serial.println((int)GPS.satellites);
}
}
}