I am building a GPS tracking project to be used in agriculture. I am in way over my head but am very persistent (a common problem for farmers). I started out with the Adafruit Ultimate GPS Shield on an Uno. The Uno limited memory was a problem as the lat/long data will be used in calculations to show location relative to numbered tree rows. The Uno version did write to the SD card. Then switched to a Mega 2560 and have gotten all of the functions to work except writing data to the SD card. Hopefully there is an easy fix in the sketch.
The hardware is:
Arduino R3 2560
Adafruit Ultimate GPS shield
Adafruit Standard LCD 20X4 Product 198 with i2C/SPI back pack
QGP Waterproof 28dB Gain Antenna (Amazon)
Attached images show the LCD display and the Mega with shield.
Wiring numbers are the actual pin numbers printed on the MEGA .
Wiring is:
I2C back pack…5V to 5V, GND to GND, CLK to 21, DAT to 20
Ultimate GPS Shield:..3V to 14, CD to 14, CCS to 15, PPS to 16, PPS to 17, TX to 19, RX to 18…not a typo TX to 19, RX to 18….
Currently LCD displays time, Lat, Long, Speed…works well in the field but won’t write to SD card.
Serial monitor displays correctly.
Here is the current version of the sketch.
#include <SPI.h>
#include <Adafruit_GPS.h>
#include <SoftwareSerial.h>
#include <SD.h>
//#include <SDADA.h>
#include <avr/sleep.h>
#include <Adafruit_LiquidCrystal.h>
#define mySerial Serial1
Adafruit_GPS GPS(&mySerial);
Adafruit_LiquidCrystal lcd(0);
#define GPSECHO true
boolean usingInterrupt = false;
void useInterrupt(boolean); // Func prototype keeps Arduino 0023 happy
#define chipSelect 10
#define ledPin 13
uint32_t timer = millis();
File logfile;
void error(uint8_t errno) {
/*
if (SD.errorCode()) {
putstring("SD error: ");
Serial.print(card.errorCode(), HEX);
Serial.print(',');
Serial.println(card.errorData(), HEX);
}
*/
while(1) {
uint8_t i;
for (i=0; i<errno; i++) {
digitalWrite(ledPin, HIGH);
delay(100);
digitalWrite(ledPin, LOW);
delay(100);
}
for (i=errno; i<10; i++) {
delay(200);
}
}
}
void setup()
{
Serial.begin(115200);
delay(1000);
lcd.begin(20, 4); // pins for lcd, analog 4,5
delay(500);
lcd.setBacklight(HIGH);
lcd.print("Wait for it..");
delay(500);
GPS.begin(9600);
delay(500);
GPS.sendCommand(PMTK_SET_NMEA_OUTPUT_RMCGGA);
GPS.sendCommand(PMTK_SET_NMEA_UPDATE_1HZ); // 1 Hz update rate
GPS.sendCommand(PGCMD_ANTENNA);
delay(1000);
mySerial.println(PMTK_Q_RELEASE);
Serial.println("\r\nUltimate GPSlogger Shield");
pinMode(ledPin, OUTPUT);
// make sure that the default chip select pin is set to
pinMode(10, OUTPUT);
// see if the card is present and can be initialized:
if (!SD.begin(chipSelect, 11, 12, 13)) {
// if (!SD.begin(chipSelect)) { // if you're using an UNO, you can use this line instead
Serial.println("Card init. failed!");
error(2);
}
char filename[15];
strcpy(filename, "GPSLOG00.TXT");
for (uint8_t i = 0; i < 100; i++) {
filename[6] = '0' + i/10;
filename[7] = '0' + i%10;
// create if does not exist, do not open existing, write, sync after write
if (! SD.exists(filename)) {
break;
}
}
logfile = SD.open(filename, FILE_WRITE);
if( ! logfile ) {
Serial.print("Couldnt create ");
Serial.println(filename);
error(3);
}
Serial.print("Writing to ");
Serial.println(filename);
}
void loop() // run over and over again
{
char c = GPS.read();
if ((c) && (GPSECHO))
Serial.write(c);
if (GPS.newNMEAreceived()) {
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 (timer > millis()) timer = millis();
if (millis() - timer > 2000) {
timer = millis(); // reset the timer
Serial.print("\nTime: ");
Serial.print(GPS.hour, DEC); Serial.print(':');
Serial.print(GPS.minute, DEC); Serial.print(':');
Serial.print(GPS.seconds, DEC); Serial.print('.');
Serial.println(GPS.milliseconds);
Serial.print("Date: ");
Serial.print(GPS.day, DEC); Serial.print('/');
Serial.print(GPS.month, DEC); Serial.print("/20");
Serial.println(GPS.year, DEC);
Serial.print("Fix: "); Serial.print((int)GPS.fix);
Serial.print(" quality: "); Serial.println((int)GPS.fixquality);
if (GPS.fix) {
Serial.print("Location: ");
Serial.print(GPS.latitude, 4); Serial.print(GPS.lat);
Serial.print(", ");
Serial.print(GPS.longitude, 4); Serial.println(GPS.lon);
Serial.print("Speed (knots): "); Serial.println(GPS.speed);
Serial.print("Angle: "); Serial.println(GPS.angle);
Serial.print("Altitude: "); Serial.println(GPS.altitude);
Serial.print("Satellites: "); Serial.println((int)GPS.satellites);
lcd.setCursor(0, 0);
float lat = GPS.latitude*0.01;
lcd.print("Lat: ");
lcd.print (lat,0);
lcd.print (".");
float decpart;
decpart=lat -(int(lat));
float decparted=decpart/60;
lcd.print (decparted*10000000000,0);
float lon = GPS.longitude*0.01;
lcd.setCursor(0, 1);
lcd.print("Long: ");
lcd.print (lon,0);
float decpart1;
decpart1=lon -(int(lon));
float decparted1=decpart1/60;
if(decparted1<.1)
lcd.print (".0");
lcd.print (decparted1*10000000000,0);
float speed = GPS.speed;
lcd.setCursor(0, 2);
lcd.print("Speed(mph): ");
lcd.print (int(speed)/1.151, 4);
lcd.setCursor(0, 3);
int ghour = GPS.hour;
int gminute = GPS.minute;
lcd.print("Time: ");
if(ghour<8) //8 for ca
lcd.print (int(ghour+17)); //17 for ca
else
lcd.print (int(ghour-7));// 7 for ca
lcd.print (":");
if(gminute<10)
lcd.print ("0");
lcd.print (int(gminute), DEC);
Serial.println("Log");
}
}
}
Thanks in advance for any and all help.