Pages: [1] 2   Go Down
Author Topic: GPS sketch Works on UNO not on Mega  (Read 2170 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 36
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I have the tinyGPS examples working fine with my UNO

but they do not read back GPS info when compiled and loaded to the MEGA

the header line prints to the serial monitor fine but no GPS data

the sketch specifies pin 2 and 3 as the txd and rxd

Does the mega assign pins differently ?

I have changed the baud to 9600 (what my GPS uses)

I have tried many different pins on the Mega but nothing

The other example sketches are the same: UNO yes, Mega no

I think it has to do with not getting the serial from the GPS as the rest of the sketch seems to work
(for example I get the 'header' print out in the serial monitor -- just no lat long etc -- mostly **** or 0

e(ta change xxx to ****)

Here is the slightly modified example code from tinyGPS (unmodified code doesn't work either on mega)


Code:
#include <SoftwareSerial.h>

#include <SoftwareSerial.h>

#include <TinyGPS.h>

/* This sample code demonstrates the normal use of a TinyGPS object.
   It requires the use of SoftwareSerial, and assumes that you have a
   4800-baud serial GPS device hooked up on pins 3(rx) and 4(tx).
*/

TinyGPS gps;
SoftwareSerial ss(2, 3);

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()
{
  Serial.begin(9600);
  ss.begin(9600);
  
  Serial.print("Testing TinyGPS library v. "); Serial.println(TinyGPS::library_version());
  Serial.println("by Mikal Hart");
  Serial.println();
  Serial.print("Sizeof(gpsobject) = "); Serial.println(sizeof(TinyGPS));
  Serial.println();
  Serial.println("Sats HDOP Latitude Longitude Fix  Date       Time       Date Alt     Course Speed Card  Distance Course Card  Chars Sentences Checksum");
  Serial.println("          (deg)    (deg)     Age                        Age  (m)     --- from GPS ----  ---- to London  ----  RX    RX        Fail");
  Serial.println("--------------------------------------------------------------------------------------------------------------------------------------");
}

void loop()
{
  bool newdata = false;
  unsigned long start = millis();
  
  // Every second we print an update
  while (millis() - start < 1000)
  {
    if (feedgps())
      newdata = true;
  }
  
  gpsdump(gps);
}

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;
  
  print_int(gps.satellites(), TinyGPS::GPS_INVALID_SATELLITES, 5);
  print_int(gps.hdop(), TinyGPS::GPS_INVALID_HDOP, 5);
  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);
  print_int(age, TinyGPS::GPS_INVALID_AGE, 5);

  print_date(gps);

  print_float(gps.f_altitude(), TinyGPS::GPS_INVALID_F_ALTITUDE, 8, 2);
  print_float(gps.f_course(), TinyGPS::GPS_INVALID_F_ANGLE, 7, 2);
  print_float(gps.f_speed_kmph(), TinyGPS::GPS_INVALID_F_SPEED, 6, 2);
  print_str(gps.f_course() == TinyGPS::GPS_INVALID_F_ANGLE ? "*** " : TinyGPS::cardinal(gps.f_course()), 6);
  print_int(flat == TinyGPS::GPS_INVALID_F_ANGLE ? 0UL : (unsigned long)TinyGPS::distance_between(flat, flon, LONDON_LAT, LONDON_LON) / 1000, 0xFFFFFFFF, 9);
  print_float(flat == TinyGPS::GPS_INVALID_F_ANGLE ? 0.0 : TinyGPS::course_to(flat, flon, 51.508131, -0.128002), TinyGPS::GPS_INVALID_F_ANGLE, 7, 2);
  print_str(flat == TinyGPS::GPS_INVALID_F_ANGLE ? "*** " : TinyGPS::cardinal(TinyGPS::course_to(flat, flon, LONDON_LAT, LONDON_LON)), 6);

  gps.stats(&chars, &sentences, &failed);
  print_int(chars, 0xFFFFFFFF, 6);
  print_int(sentences, 0xFFFFFFFF, 10);
  print_int(failed, 0xFFFFFFFF, 9);
  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 (ss.available())
  {
    if (gps.encode(ss.read()))
      return true;
  }
  return false;
}

« Last Edit: May 29, 2012, 08:23:46 pm by SolarPanels » Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 547
Posts: 45982
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
#include <SoftwareSerial.h>
You have 4 hardware serial ports. Why do you (think you) need to use software serial, too?
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 36
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset


Not sure why, Paul

but it was written that way

are you suggesting on the mega to use one of the other 4 ports

I think I tried that but it didn't work (or more likely my code was bad  smiley-grin

can you point me in the right direction ?
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 36
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

OK I give


2 hours and still don't see the code

Logged

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 452
Posts: 18694
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

What code don't you see?

It helps to show what you see in the serial monitor. For example the sketch prints:

Code:
Testing TinyGPS library v.

Do you see that? And what else?

Since your example doesn't compile with the TinyGPS library it would be helpful to post a link to that.
Logged

Gosport, UK
Offline Offline
Faraday Member
**
Karma: 19
Posts: 3114
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
SoftwareSerial ss(2, 3);

http://arduino.cc/en/Reference/SoftwareSerial
Quote
Not all pins on the Mega and Mega 2560 support change interrupts, so only the following can be used for RX: 10, 11, 12, 13, 50, 51, 52, 53, 62, 63, 64, 65, 66, 67, 68, 69
Logged

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 452
Posts: 18694
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
SoftwareSerial ss(2, 3);

You could replace that by Serial1 (for example) by changing the 3 lines that refer to "ss" (and deleting the above line).
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 36
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

when I replace it with

Serial1 (18, 19)

I get this error message

expected constructor, destructor, or type conversion before "(" token 
Logged

Andrews SC
Offline Offline
Full Member
***
Karma: 0
Posts: 212
If Arduino is not the answer, you are asking the wrong question!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

On a 2560:

Serial0 is pins 0 (rx) & 1 (tx)
Serial1 is pins 18 (rx) & 19 (tx)
Serial2 is pins 16 (rx) & 17 (tx)
Serial3 is pins 14 (rx) & 15 (tx)

You refer to those ports as seen on page http://arduino.cc/en/Reference/Serial

You do not specify the pin numbers, the arduino already knows what pin number they are.
Logged

Steve Spence - ET/AT/R&D KK4HFJ

Andrews SC
Offline Offline
Full Member
***
Karma: 0
Posts: 212
If Arduino is not the answer, you are asking the wrong question!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

I'm berring he'll have to fix TinyGPS.h to not use softserial as well, but to use one of the hardware ports.
Logged

Steve Spence - ET/AT/R&D KK4HFJ

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 452
Posts: 18694
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
SoftwareSerial ss(2, 3);

You could replace that by Serial1 (for example) by changing the 3 lines that refer to "ss" (and deleting the above line).

...

I get this error message

expected constructor, destructor, or type conversion before "(" token 

Delete that line.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 36
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

IT'S WORKING !!!

The sketch is too long to post

But I changed this in the beginning: Added "Serial1.begin(9600)

and changed a loop near the end to look at Serial1

Quote
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()
{
  Serial.begin(9600);
  Serial1.begin(9600);
  



And changed this near the end

Quote
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 : ' ');
  feedgps();
}

static bool feedgps()
{
  while (Serial1.available())
  {
    if (gps.encode(Serial1.read()))
      return true;
  }
  return false;
}

Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 1
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Wish I'd read this a couple of weeks ago.

I had the same problem, trying to use the TinyGPS examples on my Mega 2560.

I eventually figured out to use the hardware Serial1 on the Mega 2560 and got things working.

But didn't know why the example supplied didn't work!

The post, above, from dxw00d explains things!

Wish there were comments explaining this included in the example sketches, to avoid such problems for future users!
eg. state that 2560 should use different pins for softwareserial, not (2,3), or else should use hardware serial.

regards,

Logged

Anaheim CA.
Offline Offline
Faraday Member
**
Karma: 44
Posts: 2806
...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

The Adafruit library is a better Lib, I think and it also has descriptions of pins for a mega and a "How To" as well. Adafruit usually has good working libs and examples.
In my tender experience. I bought one of those 1.8" tft lcd's from them and besides not having the color orange.. yet <BG> Was an easy set of examples on how to use a GLCD...

Doc
Logged

--> WA7EMS <--
“The solution of every problem is another problem.” -Johann Wolfgang von Goethe
I do answer technical questions PM'd to me with whatever is in my clipboard

Offline Offline
Newbie
*
Karma: 0
Posts: 12
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

if i use hardware serial, do i need to change to UART mode?

Wish I'd read this a couple of weeks ago.

I had the same problem, trying to use the TinyGPS examples on my Mega 2560.

I eventually figured out to use the hardware Serial1 on the Mega 2560 and got things working.

But didn't know why the example supplied didn't work!

The post, above, from dxw00d explains things!

Wish there were comments explaining this included in the example sketches, to avoid such problems for future users!
eg. state that 2560 should use different pins for softwareserial, not (2,3), or else should use hardware serial.

regards,


Logged

Pages: [1] 2   Go Up
Jump to: