I have written the sketch below which waits for a serial monitor input of "o" then turns on the GPS and starts processing the datastream until it finds 7 sats or the time limit is reached. Then it prints out the lat, lon, time etc. and finally powers down the GPS. It is working just fine but here is where it gets interesting. If I press "o" again, it immediately prints the same output as before. The same timestamp, date, position, everything. On the other hand, if I close the serial monitor window and open a new one and press "o" it starts over from scratch like I would want. It seems like closing the window clears the last data out of memory. What is going on? Is there a way to make it start from scratch every time without closing the window?
#include <TinyGPS++.h> // Include the TinyGPS++ library
TinyGPSPlus tinyGPS; // Create a TinyGPSPlus object
#define GPS_BAUD 9600 // GPS module baud rate. GP3906 defaults to 9600.
#include <SoftwareSerial.h>
#define ARDUINO_GPS_RX 3 // GPS TX, Arduino RX pin
#define ARDUINO_GPS_TX 4 // GPS RX, Arduino TX pin
SoftwareSerial ssGPS(ARDUINO_GPS_TX, ARDUINO_GPS_RX); // Create a SoftwareSerial
#define gpsPort ssGPS // Alternatively, use Serial1 on the Leonardo
#define Serial Serial
unsigned long gpsTimout = 120000;
int GPSpower = 10;
void setup() {
Serial.begin(9600);
pinMode(GPSpower, OUTPUT); //
gpsPort.begin(GPS_BAUD);
}
void loop() {
char rx_byte;
if (Serial.available() > 0) { // is a character available?
rx_byte = Serial.read(); //
if (rx_byte == 'o') {
digitalWrite(GPSpower, HIGH);
unsigned long gpsTimer = 0;
unsigned long gpsStart = millis();
while ((tinyGPS.satellites.value() <= 6) && (gpsTimer <= gpsTimout)) {
unsigned long now = millis();
gpsTimer = (now - gpsStart);
Serial.print("Serching for Sats -- Current Count: ");
Serial.println(tinyGPS.satellites.value());
printTime();
Serial.println(gpsTimer);
smartDelay(1000);
}
//digitalWrite(GPSpower, LOW);
//Serial.println("GPS Toggled");
printGPSInfo();
delay(2000);
digitalWrite(GPSpower, LOW);
Serial.println("GPS Toggled");
}
}
}
void printGPSInfo()
{
// Print latitude, longitude, altitude in feet, course, speed, date, time,
// and the number of visible satellites.
Serial.print("Lat: "); Serial.println(tinyGPS.location.lat(), 6);
Serial.print("Long: "); Serial.println(tinyGPS.location.lng(), 6);
Serial.print("Alt: "); Serial.println(tinyGPS.altitude.feet());
Serial.print("Course: "); Serial.println(tinyGPS.course.deg());
Serial.print("Speed: "); Serial.println(tinyGPS.speed.mph());
Serial.print("Date: "); printDate();
Serial.print("Time: "); printTime();
Serial.print("Sats: "); Serial.println(tinyGPS.satellites.value());
Serial.println();
}
void printDate()
{
Serial.print(tinyGPS.date.day());
Serial.print("/");
Serial.print(tinyGPS.date.month());
Serial.print("/");
Serial.println(tinyGPS.date.year());
}
void printTime()
{
Serial.print(tinyGPS.time.hour());
Serial.print(":");
if (tinyGPS.time.minute() < 10) Serial.print('0');
Serial.print(tinyGPS.time.minute());
Serial.print(":");
if (tinyGPS.time.second() < 10) Serial.print('0');
Serial.println(tinyGPS.time.second());
}
static void smartDelay(unsigned long ms)
{
unsigned long start = millis();
do
{
// If data has come in from the GPS module
while (gpsPort.available())
tinyGPS.encode(gpsPort.read()); // Send it to the encode function
// tinyGPS.encode(char) continues to "load" the tinGPS object with new
// data coming in from the GPS module. As full NMEA strings begin to come in
// the tinyGPS library will be able to start parsing them for pertinent info
} while (millis() - start < ms);
}