1 | #include <SPI.h>
|
2 | #include <Adafruit_GPS.h>
|
3 | #include <SoftwareSerial.h>
|
4 | #include <SD.h>
|
5 | #include <avr/sleep.h>
|
6 |
|
7 |
|
8 | // these constants describe the pins. They won't change:
|
9 | const int xpin = A1; // x-axis of the accelerometer
|
10 | const int ypin = A2; // y-axis
|
11 | const int zpin = A3; // z-axis (only on 3-axis models)
|
12 | int sampleDelay = 1000;
|
13 |
|
14 |
|
15 | SoftwareSerial mySerial(8, 7);
|
16 | Adafruit_GPS GPS(&mySerial);
|
17 |
|
18 | // Set GPSECHO to 'false' to turn off echoing the GPS data to the Serial console
|
19 | // Set to 'true' if you want to debug and listen to the raw GPS sentences
|
20 | #define GPSECHO true
|
21 | /* set to true to only log to SD when GPS has a fix, for debugging, keep it false */
|
22 | #define LOG_FIXONLY false
|
23 |
|
24 | // this keeps track of whether we're using the interrupt
|
25 | // off by default!
|
26 | boolean usingInterrupt = false;
|
27 | void useInterrupt(boolean); // Func prototype keeps Arduino 0023 happy
|
28 |
|
29 | // Set the pins used
|
30 | #define chipSelect 10
|
31 | #define ledPin 13
|
32 |
|
33 | File logfile;
|
34 |
|
35 | // read a Hex value and return the decimal equivalent
|
36 | uint8_t parseHex(char c) {
|
37 | if (c < '0')
|
38 | return 0;
|
39 | if (c <= '9')
|
40 | return c - '0';
|
41 | if (c < 'A')
|
42 | return 0;
|
43 | if (c <= 'F')
|
44 | return (c - 'A')+10;
|
45 | }
|
46 |
|
47 | // blink out an error code
|
48 | void error(uint8_t errno) {
|
49 |
|
50 | while(1) {
|
51 | uint8_t i;
|
52 | for (i=0; i<errno; i++) {
|
53 | digitalWrite(ledPin, HIGH);
|
54 | delay(100);
|
55 | digitalWrite(ledPin, LOW);
|
56 | delay(100);
|
57 | }
|
58 | for (i=errno; i<10; i++) {
|
59 | delay(200);
|
60 | }
|
61 | }
|
62 | }
|
63 |
|
64 | void setup() {
|
65 |
|
66 | Serial.begin(115200);
|
67 | Serial.println("\r\nUltimate GPSlogger Shield");
|
68 | pinMode(ledPin, OUTPUT);
|
69 |
|
70 | pinMode(10, OUTPUT);
|
71 |
|
72 | analogReference(EXTERNAL);
|
73 |
|
74 | pinMode(xpin, INPUT);
|
75 | pinMode(ypin, INPUT);
|
76 | pinMode(zpin, INPUT);
|
77 |
|
78 |
|
79 | if (!SD.begin(chipSelect, 11, 12, 13)) {
|
80 |
|
81 | Serial.println("Card init. failed!");
|
82 | error(2);
|
83 | }
|
84 | char filename[15];
|
85 | strcpy(filename, "GPSLOG00.TXT");
|
86 | for (uint8_t i = 0; i < 100; i++) {
|
87 | filename[6] = '0' + i/10;
|
88 | filename[7] = '0' + i%10;
|
89 |
|
90 | if (! SD.exists(filename)) {
|
91 | break;
|
92 | }
|
93 | }
|
94 |
|
95 | logfile = SD.open(filename, FILE_WRITE);
|
96 | if( ! logfile ) {
|
97 | Serial.print("Couldnt create ");
|
98 | Serial.println(filename);
|
99 | error(3);
|
100 | }
|
101 | Serial.print("Writing to ");
|
102 | Serial.println(filename);
|
103 |
|
104 | GPS.begin(9600);
|
105 |
|
106 | // uncomment this line to turn on RMC (recommended minimum) and GGA (fix data) including altitude
|
107 | GPS.sendCommand(PMTK_SET_NMEA_OUTPUT_RMCGGA);
|
108 | // uncomment this line to turn on only the "minimum recommended" data
|
109 | //GPS.sendCommand(PMTK_SET_NMEA_OUTPUT_RMCONLY);
|
110 | // For logging data, we don't suggest using anything but either RMC only or RMC+GGA
|
111 | // to keep the log files at a reasonable size
|
112 | // Set the update rate
|
113 | GPS.sendCommand(PMTK_SET_NMEA_UPDATE_1HZ); // 100 millihertz (once every 10 seconds), 1Hz or 5Hz update rate
|
114 |
|
115 | // Turn off updates on antenna status, if the firmware permits it
|
116 | GPS.sendCommand(PGCMD_NOANTENNA);
|
117 |
|
118 | useInterrupt(true);
|
119 |
|
120 | Serial.println("Ready!");
|
121 | }
|
122 |
|
123 |
|
124 | // Interrupt is called once a millisecond, looks for any new GPS data, and stores it
|
125 | SIGNAL(TIMER0_COMPA_vect) {
|
126 | char c = GPS.read();
|
127 | // if you want to debug, this is a good time to do it!
|
128 | #ifdef UDR0
|
129 | if (GPSECHO)
|
130 | if (c) UDR0 = c;
|
131 | // writing direct to UDR0 is much much faster than Serial.print
|
132 | // but only one character can be written at a time.
|
133 | #endif
|
134 | }
|
135 |
|
136 | void useInterrupt(boolean v) {
|
137 | if (v) {
|
138 | // Timer0 is already used for millis() - we'll just interrupt somewhere
|
139 | // in the middle and call the "Compare A" function above
|
140 | OCR0A = 0xAF;
|
141 | TIMSK0 |= _BV(OCIE0A);
|
142 | usingInterrupt = true;
|
143 | }
|
144 | else {
|
145 | // do not call the interrupt function COMPA anymore
|
146 | TIMSK0 &= ~_BV(OCIE0A);
|
147 | usingInterrupt = false;
|
148 | }
|
149 | }
|
150 |
|
151 | void loop() {
|
152 |
|
153 | int x = analogRead(xpin);
|
154 |
|
155 | delay(1);
|
156 |
|
157 | int y = analogRead(ypin);
|
158 |
|
159 | delay(1);
|
160 |
|
161 | int z = analogRead(zpin);
|
162 |
|
163 | float zero_G = 512.0;
|
164 |
|
165 | float scale = 102.3;
|
166 |
|
167 | float x_real = (((float)x - zero_G)/scale);
|
168 | float y_real = (((float)x - zero_G)/scale);
|
169 | float z_real = (((float)z - zero_G)/scale);
|
170 | String trennen = " ; ";
|
171 | String beschleunigungen = x_real + trennen + y_real + trennen + z_real;
|
172 | Serial.println(beschleunigungen);
|
173 | delay(sampleDelay);
|
174 |
|
175 |
|
176 | if (! usingInterrupt) {
|
177 | // read data from the GPS in the 'main loop'
|
178 | char c = GPS.read();
|
179 | // if you want to debug, this is a good time to do it!
|
180 | if (GPSECHO)
|
181 | if (c) Serial.print(c);
|
182 | }
|
183 |
|
184 | // if a sentence is received, we can check the checksum, parse it...
|
185 | if (GPS.newNMEAreceived()) {
|
186 |
|
187 | char *stringptr = GPS.lastNMEA();
|
188 |
|
189 | if (!GPS.parse(stringptr)) // this also sets the newNMEAreceived() flag to false
|
190 | return; // we can fail to parse a sentence in which case we should just wait for another
|
191 |
|
192 | // Sentence parsed!
|
193 | Serial.println("OK");
|
194 | if (LOG_FIXONLY && !GPS.fix) {
|
195 | Serial.print("No Fix");
|
196 | return;
|
197 | }
|
198 |
|
199 | // Rad. lets log it!
|
200 | Serial.println("Log");
|
201 |
|
202 | uint8_t stringsize = strlen(stringptr);
|
203 | if (stringsize != logfile.write((uint8_t *)stringptr, stringsize)) //write the string to the SD file
|
204 | error(4);
|
205 |
|
206 | if (strstr(stringptr, "RMC") || strstr(stringptr, "GGA"))
|
207 | logfile.print(" ; ");
|
208 | logfile.println(beschleunigungen);
|
209 | logfile.flush();
|
210 |
|
211 | Serial.println();
|
212 | }
|
213 | }
|