I created some basic code, and it does not give me the same data on the serial port that the tutorial program "Parse' and "echo" do.
Can anyone identify my errors?
Code: Select all
/* In this version of the code read all sensors in an interrupt routine */
#include <Adafruit_GPS.h>
#include <SoftwareSerial.h>
#include <TimerOne.h>
// GPS Global variables
// For the Adafruit GPS module with software serial:
// Connections: GPS Power pin to 5V, GPS Ground pin to ground
// GPS TX (transmit) pin to Digital 3, GPS RX (receive) pin to Digital 2
SoftwareSerial gpsSerial(3, 2); /* define a new software serial port on the Arduino called gpsSerial
where pin 3 is the rx, pin 2 is the tx on the Arduino */
Adafruit_GPS GPS(&gpsSerial);
/*-------------------------------------------------------------------------*/
volatile int timerflag;
void setup()
{
interrupts (); //enable interrupts
/*---------------------SET UP INTERRUPT TIMER FOR DATA POLLING -------*/
Timer1.initialize(1000000); // set a timer of length 1000000 microseconds (1 second)
Timer1.attachInterrupt(timeToPollSensors); //sets a flag here so code can run "pollSensors" on each timer interrupt
/*---------------------------SET UP GPS SENSOR-----------------*/
Serial.begin(115200); // connect serial port at 115200 so we can spit out GPS data fast enough
GPS.begin(9600); // 9600 NMEA is the default baud rate for Adafruit MTK GPSs
GPS.sendCommand(PMTK_SET_NMEA_OUTPUT_RMCONLY); // turn on only the "minimum recommended" data
GPS.sendCommand(PMTK_SET_NMEA_UPDATE_1HZ); /* Set the update rate to 1 Hz so the parsing
code will have time to sort thru the data, and print it - (try higher if nec) */
}
void loop() // run over and over again
{
if (timerflag) {
timerflag =0;
pollSensors();
}
}
//Timer interrupt function
void timeToPollSensors()
{timerflag = 1;
}
void pollSensors()
{
/*----------------READ GPS SENSOR----------------------------*/
GPS.read();
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
}
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.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);
}
}