Go Down

Topic: Losing Global Variable value partially  (Read 708 times) previous topic - next topic

Santoshk

Mar 31, 2015, 06:12 am Last Edit: Mar 31, 2015, 06:18 am by CrossRoads
Hi,


I am reading values from SD card and populating a global arrays: arrLats[] and arrLongs[]. The values of the array would be referenced to find deviation of real-time GPS coordinates - added a readGPS() function.

I am facing a problem wherein I lose the global array values inside loop() function if I use readGPS() - I see only one of array value on serial output. If I simply comment out the call to readGPS(), I am able to see the output of arrLats and arrLongs.  


Not sure what could be the reason for this. I would appreciate your help on this. FYR, the code & serial output:

CODE:-
(code with Code Tags!)
Code: [Select]


#include <SPI.h>
#include <SD.h>
#include <Adafruit_GPS.h>
#include <SoftwareSerial.h>

//********start - Global variables to read from SD card
File myFile;
float arrLats[4];
float arrLongs[4];
float fLat = 0;
float fLong = 0;

//********start - Global variables to read from GPS sheild
SoftwareSerial mySerial(8, 7);
Adafruit_GPS GPS(&mySerial);

#define GPSECHO  true

uint32_t timer = millis();


String NMEA1;  //We will use this variable to hold our first NMEA sentence
String NMEA2;  //We will use this variable to hold our second NMEA sentence
char c;       //Used to read the characters spewing from the GPS module



void setup() {
 Serial.begin(9600);

 String content = "";
 char character;
 String strLatLon = "";
 String strLat = "";
 String strLong = "";
 int idxComma = 0, idxNewline = 0, idxArray = 0;
 
 
 GPS.begin(9600);
 
 GPS.sendCommand("$PGCMD,33,0*6D");  //Turn off antenna update nuisance data
 GPS.sendCommand(PMTK_SET_NMEA_OUTPUT_RMCGGA); //Request RMC and GGA Sentences only
 GPS.sendCommand(PMTK_SET_NMEA_UPDATE_1HZ); //Set update rate to 1 hz
 delay(1000);
 pinMode(10, OUTPUT);

//--------Start read contents (reference lat/long) from SD file            
 if(!SD.begin(10)){ Serial.println("initialization failed!");}
 else{    
   myFile = SD.open("LatLon.txt");
   if (myFile)
   {
     while(myFile.available())
     {

       character = myFile.read();
       
       if (character != '\n' && character !='\r') {
         content.concat(character);
       }
       else {
         Serial.println(content);
            strLatLon = content;

            idxComma =  strLatLon.indexOf(",");
            idxNewline = strLatLon.length();
           
            strLat = strLatLon.substring(0, idxComma);
            strLong = strLatLon.substring(idxComma + 1, idxNewline);
           
            strLat.trim();
            strLong.trim();
           
            arrLats[idxArray] = strLat.toFloat();

            arrLongs[idxArray] = strLong.toFloat();

            idxArray++;
           
            content = "";
            strLatLon = "";
            strLat = "";
            strLong = "";
            idxComma = 0;
            idxNewline = 0;        
       }      
     }
     myFile.close();
    }
    else
    {
     Serial.println("error opening LatLon.txt");
    }    
 }
//--------End read contents (reference lat/long) from SD file    
   
}

void loop() {

 bool bReadGps;
 
 
 for (int i=0; i<4; i++) {
       Serial.println(i);
       Serial.println(arrLats[i]);
       Serial.println(arrLongs[i]);
     }
 
   
   bReadGps = readGPS();
     
}


Code for readGPS() function:

boolean readGPS() {  //This function will read and remember two NMEA sentences from GPS
       
 clearGPS();    //Serial port probably has old or corrupt data, so begin by clearing it all out
 boolean bReturn = false;

 
 if (GPS.newNMEAreceived()) {
   if (!GPS.parse(GPS.lastNMEA()))   // this also sets the newNMEAreceived() flag to false
     return bReturn;  // we can fail to parse a sentence in which case we should just wait for another
 }

 // if millis() or timer wraps around, we'll just reset it
 if (timer > millis())  timer = millis();

 // approximately every 2 seconds or so, print out the current stats
 if (millis() - timer > 2000) {
   timer = millis(); // reset the timer
   
   Serial.print("Fix: "); Serial.print((int)GPS.fix);
   Serial.print(" quality: "); Serial.println((int)GPS.fixquality);
   
   if (GPS.fix) {
     Serial.print("Location (in degrees, works with Google Maps): ");
     fLat = GPS.latitudeDegrees;
     Serial.print(fLat, 6);
 
     Serial.print(", ");
     fLong = GPS.longitudeDegrees;
     Serial.println(fLong, 6);
     
     bReturn = true;
     
   }
 }
 return bReturn;
}

void clearGPS() {  //Since between GPS reads, we still have data streaming in, we need to clear the old data by reading a few sentences, and discarding these
 while(!GPS.newNMEAreceived()) {
   c=GPS.read();
 }
 GPS.parse(GPS.lastNMEA());
}

OUTPUT:-
If I comment readGPS() inside loop:
0
77.64
12.97
1
77.64
12.97
2
77.64
12.96
3
77.64
12.97

If I uncomment readGPS() inside loop:
0
77.64
0.00
1
77.64
0.00
2
77.64
0.00
3
77.64
0.00

Delta_G

I wonder if you aren't running out of SRAM.  The SD library chews up a ton of it and I'm sure the GPS stuff does too.  Try using F() on all the Serial prints of constant strings and see if that might help. 
|| | ||| | || | ||  ~Woodstock

MorganS

Do you know how much RAM you are using? You may have filled the RAM with those long strings:
Code: [Select]
Serial.println("error opening LatLon.txt");

I wouldn't expect this to fill the RAM but the library may have a lot of this sort of stuff inside it. Get into the habit of using the F() macro to store long strings in the flash memory:

Code: [Select]
Serial.println(F("error opening LatLon.txt"));
"The problem is in the code you didn't post."

Go Up