Mega 2560 and serial issues

Hi all, I having trouble with my Mega 2560. I have an NEO-6M GPS module connected up to Serial 1 pins (RX18 TX19) and a Nextion LCD to Serial 3 (RX15 TX14). My issue seems that I cannot correctly read the GPS. If I run the below code, it seems that I'm only getting the time and date once. The time and date I get are then repeated as the same values over and over. If I change the code and upload onto an UNO, everything works with no issues. I believe the issue is how I'm using the serials on the Mega. No matter how I set the Serials by define or leaving it as the actual names, I can't get any further with this and I'm looking for a fresh pair of eyes on the code. the code is part of a much larger project. So what I have done is created a new project and start with just getting the GPS informations. When I add the Nextion stuff, this is where I seems to have an issues. Then serial outputs to the Nextion work every time.

I have searched the forums and I can't seem to see an issue like mine.

#include <TinyGPS++.h>
#include "EasyNextionLibrary.h"  // Include EasyNextionLibrary
// The TinyGPS++ object
TinyGPSPlus gps;
#define nexSer Serial3
EasyNex myNex(nexSer); // Create an object of EasyNex class with the name < myNex >

// Serial Ports RX = 0 TX = 1
// Serial 1 Ports RX = 19 TX = 18
// Serial 2 Ports RX = 17 TX = 16
// Serial 3 Ports RX = 15 TX = 14


// GPS Ports are RX=15 and TX = 14 (Serial 3 Ports)
#define GPSSerial Serial1 // Setup name of Serial 1 to be used by GPS
static const uint32_t GPSBaud = 9600;// GPS Buad Rate
static const uint32_t DeBugBaud = 115200;// GPS Buad Rate

#define BugSerial Serial // Setup name of Serial for debugging

String MyFullTime = "", MyFullDate = "", MyGPSVersion = "Estate GPS Libary Version: ";

void setup() {
  MyGPSVersion += TinyGPSPlus::libraryVersion();
  BugSerial.begin(115200);
  while (!BugSerial);
  BugSerial.println(F("Serial: Serial open"));
  GPSSerial.begin(GPSBaud);
  while (!GPSSerial);
  BugSerial.println(F("Serial: GPSSerial open"));
  nexSer.begin(9600);
  while (!nexSer);
  BugSerial.println(F("Serial: LCDSerial open"));
  BugSerial.print(F("Information: "));
  BugSerial.print(F("Estate GPS Libary Version: ")); Serial.print(TinyGPSPlus::libraryVersion());
  BugSerial.println();
  myNex.writeStr("tGPSVersion.txt", MyGPSVersion);
  myNex.writeStr("tInformation.txt", "Information: Powered on and waiting GPS Lines");
  myNex.writeStr("tStatus.txt", "Information: Waiting on GPS module");
}

void loop() {
  while (GPSSerial.available()) {
    gps.encode(GPSSerial.read());
    BugSerial.println(F("Information GPS: Reading GPS module"));
    myNex.writeStr("tStatus.txt", "Information: Reading GPS module");
    GetMyTimeDate();
    GetMyTime();
    if (millis() > 5000 && gps.charsProcessed() < 10)
    {
      BugSerial.println(F("Warning: No GPS detected: check wiring."));
      myNex.writeStr("tStatus.txt", "Information: No new lines");
      myNex.writeStr("tInformation.txt", "Warning GPS: No new line in over 5 seconds. Is the GPS module flashing yet?");
      while (true);
    }
  }
}
void GetMyTimeDate() {
      if (gps.date.isValid() == true) {
        BugSerial.println(F("Information GPS: GPS date valid"));
        myNex.writeStr("tStatus.txt", "Information: Updating Date");
        char buffer [10] = "";
        sprintf(buffer, "%02d/%02d/%04d", gps.date.day(), gps.date.month(), gps.date.year());
        MyFullDate = buffer;
        myNex.writeStr("tGPSDate.txt", MyFullDate);
        buffer [10] = "";
        BugSerial.print(F("Information GPS: GPS Date: "));
        BugSerial.println(MyFullDate);
        myNex.writeStr("tStatus.txt", "Information: Date updated");
      }
}
void GetMyTime() {
    BugSerial.println(F("Information GPS: GPS time valid"));
    myNex.writeStr("tStatus.txt", "Information: Updating time");
    gps.encode(GPSSerial.read());
    char buffer [13] = "";
    sprintf(buffer, "%02d:%02d:%02d", gps.time.hour(), gps.time.minute(), gps.time.second());
    MyFullTime = buffer;
    myNex.writeStr("tGPSTime.txt", MyFullTime);
    BugSerial.print(F("Information GPS: GPS Time: "));
    BugSerial.println(MyFullTime);
    myNex.writeStr("tStatus.txt", "Information: Time updated");
}

5 seconds after reset, millis() will be greater than 5000 until the Arduino is reset or power cycled.

May not be your problem, just something I see.

edit: I see the same thing in the example program at the repository, odd.

a7

might want a bigger buffer. your date consumes 2 + 1 + 2 + 1 + 4 = 10 so there is nowhere to put the trailing nul char.

Hi Alto77, I had already tried it with and without the 5000. I have increase/decreased the number. Same result. The same line also works as is on an UNO.

Hi Blh64, the line does work for me as I do get a time and date. I will in crease it anyway to try and I will update you later this evening.

Personally I think it's a serial related issue. I only thought about it this morning and I'm going to try the same thing on another Mega in case there is a fault with the one I'm currently using.

A buffer or array that is too short is the kind of thing that might happen to work:

Work on one Arduino and fai on a different kind.

Work then mysteriously fail on the same Arduino it worked on before you made some changes.

It can mean very frustrating problems that don’t relate to what you think they might.

That your code behaves differently one place to the other is your strongest clue right now.

When you do small experiments, continue to do them if you can on both

a7

I found changing the code to this works and I am getting the correct time and date. I have noticed a bit of a lag. Current as i write this post , my GPS doesn't have a satellite fix. So when I remove the satellite count code, the lag seems to disappear. My hope is when i have a satellite fix, the code won't slow down.

void loop() {
  while (GPSSerial.available() > 0)
    if (gps.encode(GPSSerial.read()))
      displayInfo();

  if (millis() > 5000 && gps.charsProcessed() < 10)
  {
    Serial.println(F("No GPS detected: check wiring."));
    while (true);
  }
}

Scrap that. I powered down and moved the Mega to beside a window. Since then it has stopped working. I'll have to take another look at it over the weekend. In case any ones is going to point it out, I can get full Satellite signal in my house :slight_smile:

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.