Hello everyone,
I have some major updates. The GPS works now with no problems. The receiver is apart of my senior design project. The LCD display displays the lattitude and longitude live. We used Google maps to find the coordinates around the engineering building to test it out. A piezo is being used for audible feedback; and it works fine. Now, we would like the LCD to display the number of fixed satellites in and out of range. Also, in conjunction with the piezo to make an annoying sound when the satellites are out of range then a normal tone within range. A GPS is most accurate when four satellites are in range. The code is below, I apologize for the lack of comments.
#include <TinyGPS.h>
#include <LiquidCrystal.h>
TinyGPS gps;
LiquidCrystal lcd(52, 50, 28, 26, 24, 22);
static void gpsdump(TinyGPS &gps);
static bool feedgps();
static void print_float(float val, float invalid, int len, int prec);
static void print_int(unsigned long val, unsigned long invalid, int len);
static void print_date(TinyGPS &gps);
static void print_str(const char *str, int len);
void setup()
{
lcd.begin(16, 2);
lcd.print("LON");
lcd.setCursor(0, 1);
lcd.print("LAT");
Serial.begin(115200);
Serial3.begin(57600);
Serial.println();
Serial.println("Latitude Longitude");
Serial.println("(deg) (deg) ");
Serial.println("------------------");
}
void loop()
{
float flat, flon;
gps.f_get_position(&flat, &flon);
bool newdata = false;
unsigned long start = millis();
// Every second we print an update
while (millis() - start < 1000)
{
if (feedgps())
newdata = true;
}
gpsdump(gps);
if (flat<=41.940719 && flat>= 41.93923 && flon>=-88.763013 && flon<=-88.761898)
{
tone (9,261, 50);
lcd.print("Engr Bldg");
}
lcd.setCursor(4,1);
lcd.print(flat,5);
lcd.setCursor(4,0);
lcd.print(flon,5);
}
static void gpsdump(TinyGPS &gps)
{
float flat, flon;
unsigned long age, date, time, chars = 0;
unsigned short sentences = 0, failed = 0;
static const float LONDON_LAT = 51.508131, LONDON_LON = -0.128002;
gps.f_get_position(&flat, &flon, &age);
print_float(flat, TinyGPS::GPS_INVALID_F_ANGLE, 9, 5);
print_float(flon, TinyGPS::GPS_INVALID_F_ANGLE, 10, 5);
gps.stats(&chars, &sentences, &failed);
print_int(chars, 0xFFFFFFFF, 6);
Serial.println();
}
static void print_int(unsigned long val, unsigned long invalid, int len)
{
char sz[32];
if (val == invalid)
strcpy(sz, "*******");
else
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);
feedgps();
}
static void print_float(float val, float invalid, int len, int prec)
{
char sz[32];
if (val == invalid)
{
strcpy(sz, "*******");
sz[len] = 0;
if (len > 0)
sz[len-1] = ' ';
for (int i=7; i<len; ++i)
sz[i] = ' ';
Serial.print(sz);
}
else
{
Serial.print(val, prec);
int vi = abs((int)val);
int flen = prec + (val < 0.0 ? 2 : 1);
flen += vi >= 1000 ? 4 : vi >= 100 ? 3 : vi >= 10 ? 2 : 1;
for (int i=flen; i<len; ++i)
Serial.print(" ");
}
feedgps();
}
static void print_date(TinyGPS &gps)
{
int year;
byte month, day, hour, minute, second, hundredths;
unsigned long age;
gps.crack_datetime(&year, &month, &day, &hour, &minute, &second, &hundredths, &age);
if (age == TinyGPS::GPS_INVALID_AGE)
Serial.print("******* ******* ");
else
{
char sz[32];
sprintf(sz, "%02d/%02d/%02d %02d:%02d:%02d ",
month, day, year, hour, minute, second);
Serial.print(sz);
}
print_int(age, TinyGPS::GPS_INVALID_AGE, 5);
feedgps();
}
static void print_str(const char *str, int len)
{
int slen = strlen(str);
for (int i=0; i<len; ++i)
Serial.print(i<slen ? str[i] : ' ');
feedgps();
}
static bool feedgps()
{
while (Serial3.available())
{
if (gps.encode(Serial3.read()))
return true;
}
return false;
}
I added this line of code in the void loop and I compiled. But, it doesn't print the correct information.
int satellites = gps.satellites();
if( satellites > 3)
{
Serial.print("number of satellies");Serial.println( gps.satellites() );
}
else
{
Serial.println ("nothing");
}
Before that I searched for code that specifically printed the number of satellites
#include <NewSoftSerial.h>
#include <TinyGPS.h>
TinyGPS gps;
NewSoftSerial nss(2, 3);
void setup()
{
Serial.begin(115200);
nss.begin(4800);
Serial.println("TinyGPS Satellite Count Mod Test");
Serial.println();
}
void loop()
{
if (nss.available())
{
if (gps.encode(nss.read()))
{
Serial.print("Satellites in view: ");
Serial.println(gps.satsinview(), DEC);
if (gps.fixtype() == TinyGPS::GPS_FIX_NO_FIX)
{
Serial.print("No fix.");
}
else
{
// we have a fix (could be GPS_FIX_2D or GPS_FIX_3D)
Serial.print("Fix type: ");
if (gps.fixtype() == TinyGPS::GPS_FIX_2D)
Serial.println("2D");
else
Serial.println("3D");
Serial.print("Satellites used: ");
Serial.println(gps.satsused(), DEC);
// You can do the rest of your coding here - e.g. gpsdump()
}
}
}
}
I tested out the above code but, 'satsused' and 'satsinview' were not recognized.
Any suggestions?