Demo entry 6329153

GPS code

   

Submitted by anonymous on Nov 29, 2016 at 17:31
Language: Arduino. Code size: 4.9 kB.

#include "TinyGPS++.h"
#include "SoftwareSerial.h"

/*
 * DOVE TROVATE SCRITTO IL 
 * CODICE CHE FINISCE CON % 
 * E POI VA A CAPO E INIZIA 
 * CON % QUELLE DUE PARTI VANNO UNITE
 * 
 * ESEMPIO:
 *  
 * serial_connection%
 * %.begin(9600);
 * 
 * IO QUANDO COMPILO IL CODICE
 * 
 * serial_connection.begin(9600);
 * 
 */


//RX=pin 10, TX=pin 11
SoftwareSerial serial_connection(10, 11); 
//This is the GPS object that will pretty 
//much do all the grunt work with the NMEA data
TinyGPSPlus gps;
void setup()
{
  //This opens up communications to the 
  //Serial monitor in the Arduino IDE
  Serial.begin(9600);
  //This opens up communications to the GPS
  serial_connection.begin(9600);
  //Just show to the monitor that the sketch has started
  Serial.println("GPS Start");
  Serial.println(F("Sats HDOP Latitude   Longitude   "
  "Fix  Date       Time     Date Alt    "
  "Course Speed Card  Distance Course "
  "Card  Chars Sentences Checksum"));
  Serial.println(F("          (deg)      (deg)       "
  "Age                      Age  (m)    --- from "
  "GPS ----  ---- to Rome  ---"
  "-     RX    RX        Fail"));
  Serial.println(F("-----------------------------"
  "--------------------------------------------"
  "---------------------------------"
  "-----------------------------"));
}

void loop()
{
//While there are characters to come from the GPS
while(serial_connection.available())
{
  //This feeds the serial NMEA data 
  //into the library one char at a time
  gps.encode(serial_connection.read());
}
//This will pretty much be fired all the time 
//anyway but will at least reduce it to only 
//after a package of NMEA data comes in
if(gps.location.isUpdated())
{
  //Get the latest info from the gps object 
  //which it derived from the data sent by the GPS unit
 static const double ROME_LAT = 41.8919300, ROME_LON = 12.5113300;
 
 printInt(gps.satellites.value(), gps.satellites.isValid(), 5);
 printInt(gps.hdop.value(), gps.hdop.isValid(), 5);
 printFloat(gps.location.lat(), gps.location.isValid(), 11, 6);
 printFloat(gps.location.lng(), gps.location.isValid(), 12, 6);
 printInt(gps.location.age(), gps.location.isValid(), 5);
 printDateTime(gps.date, gps.time);
 printFloat(gps.altitude.meters(), gps.altitude.isValid(), 7, 2);
 printFloat(gps.course.deg(), gps.course.isValid(), 7, 2);
 printFloat(gps.speed.kmph(), gps.speed.isValid(), 6, 2);
 printStr(gps.course.isValid()? TinyGPSPlus::cardinal(
  gps.course.value()) : ""
 "*** ", 6);
 unsigned long distanceKmToRome =
  (unsigned long)TinyGPSPlus::distanceBetween(
    gps.location.lat(),
    gps.location.lng(),
    ROME_LAT, 
    ROME_LON) / 1000;
printInt(distanceKmToRome, gps.location.isValid(), 9);

double courseToRome =
  TinyGPSPlus::courseTo(
    gps.location.lat(),
    gps.location.lng(),
    ROME_LAT, 
    ROME_LON);

printFloat(courseToRome, gps.location.isValid(), 7, 2);

const char *cardinalToRome = TinyGPSPlus::cardinal(courseToRome);

printStr(gps.location.isValid() ? cardinalToRome : "*** ", 6);

printInt(gps.charsProcessed(), true, 6);
printInt(gps.sentencesWithFix(), true, 10);
printInt(gps.failedChecksum(), true, 9);
Serial.println();

smartDelay(1000);

if (millis() > 5000 && gps.charsProcessed() < 10)
  Serial.println(F("No GPS data received: check wiring"));
}
}
static void smartDelay(unsigned long ms)
{
unsigned long start = millis();
do 
{
  while (serial_connection.available())
    gps.encode(serial_connection.read());
} while (millis() - start < ms);
}

static void printFloat(float val, bool valid, int len, int prec)
{
if (!valid)
{
  while (len-- > 1)
    Serial.print('*');
  Serial.print(' ');
}
else
{
  Serial.print(val, prec);
  int vi = abs((int)val);
  int flen = prec + (val < 0.0 ? 2 : 1); // . and -
  flen += vi >= 1000 ? 4 : vi >= 100 ? 3 : vi >= 10 ? 2 : 1;
  for (int i=flen; i<len; ++i)
    Serial.print(' ');
}
smartDelay(0);
}

static void printInt(unsigned long val, bool valid, int len)
{
char sz[32] = "*****************";
if (valid)
  sprintf(sz, "%ld", val);
sz[len] = 0;
for (int i=strlen(sz); i<len; ++i)
  sz[i] = ' ';
if (len > 0) 
  sz[len-1] = ' ';
Serial.print(sz);
smartDelay(0);
}

static void printDateTime(TinyGPSDate &d, TinyGPSTime &t)
{
if (!d.isValid())
{
  Serial.print(F("********** "));
}
else
{
  char sz[32];
  sprintf(sz, "%02d/%02d/%02d ", d.day(), d.month(), d.year());
  Serial.print(sz);
}

if (!t.isValid())
{
  Serial.print(F("******** "));
}
else
{
  char sz[32];
  sprintf(sz, "%02d:%02d:%02d ", t.hour(), t.minute(), t.second());
  Serial.print(sz);
}

printInt(d.age(), d.isValid(), 5);
smartDelay(0);
}

static void printStr(const char *str, int len)
{
int slen = strlen(str);
for (int i=0; i<len; ++i)
  Serial.print(i<slen ? str[i] : ' ');
smartDelay(0);
}

This snippet took 0.01 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).