Hey All!
First time poster, long time lurker. I've learned a boatload from this site, and have been inspired to start working on building some fun custom stuff! I've searched a LOT to try and answer this question, but I'm very stumped.
The current project I'm working on is the first one that it has felt very, very necessary to split the code/functions into separate files to simplify my main(). I've been quite successful, based on another post, at creating more simple separated files. However, I'm having a very difficult time when it comes to handling objects of libraries I've included, namely <TinyGPS++.h> and .
What I'd like to do, is have my main() cleaned up by making all my gps functions separate. But I can't for the life of me figure out how to handle the objects required. I just keep getting compiler errors. Declaring/Instantiating objects in different area's seems to switch up where I get the errors, but that just tells me that its all WRONG, haha. My gut tells me this is where pointer's come in, but I don't understand them despite my best efforts. My hope is, you fine gentlemen can point(pun intended) me in the right direction. And perhaps this, if pointers are the solution, will make them finally "Click" for me!
Here is my original program, that executes perfectly.
GPSTest.ino
#include <TinyGPS++.h>
#include <SoftwareSerial.h>
SoftwareSerial serial_connection(0, 1); //create SS object on RX = pin 0, TX = pin 1
TinyGPSPlus gps; //create a tinygps++ object
void setup() {
Serial.begin(9600);
serial_connection.begin(9600);
Serial.println("GPS Start");
}
void loop() {
while(serial_connection.available()){
gps.encode(serial_connection.read()); //This feeds the serial NEMA data into the "GPS" library to be decoded
}
if(gps.location.isUpdated()){
Serial.println("Satellite Count: ");
Serial.println(gps.satellites.value());
Serial.println("Latitude: ");
Serial.println(gps.location.lat(), 6);
Serial.println("Longitude: ");
Serial.println(gps.location.lng(), 6);
Serial.println("Altitude Feet: ");
Serial.println(gps.altitude.feet());
Serial.println("Time:");
Serial.println(gps.time.value());
Serial.println("");
}
}
Long story short, it starts a serial connection between the computer and then the gps, queries the gps for data, and outputs it through serial to the computer. Simple enough.
Now in order for it to do this, to the best of my understanding, I use a <SoftwareSerial.h> object and a <TinyGPSPlus.h> object, which I instantiate before the setup() function on these lines:
SoftwareSerial serial_connection(0, 1); //create SS object on RX = pin 0, TX = pin 1
TinyGPSPlus gps; //create a tinygps++ object
I then use these repeatedly in the loop to store/recall data.
Onto my attempt to break the code down:
GPSTestBreakdown.ino
#include <TinyGPS++.h>
#include <SoftwareSerial.h>
#include "gps.c"
#include "gps.h"
SoftwareSerial ssGPS(0, 1); //create SS object on RX = pin 0, TX = pin 1
TinyGPSPlus gps; //create a tinygps++ object
void setup() {
Serial.begin(9600);
beginGPS(); //run start gps communication function
Serial.println("GPS Start");
}
void loop() {
getGPS(); //Run's a while loop to grab all available NMEA data and store it into TinyGPS++ Object "gps"
printGPS(); //Print GPS data to serial if it's updated
}
then my gps.c file:
/* This gps.c file will serve as the container for handling
* all of the gps functions within the main() function.
*/
#include <TinyGPS++.h>
#include <SoftwareSerial.h>
#include "gps.h"
SoftwareSerial ssGPS(0,1); //create SS object on RX = pin 0, TX = pin 1
TinyGPSPlus gps; //create a tinygps++ object
void beginGPS(){
ssGPS.begin(9600); //start communicating with ssGps to capture gps data from unit
}
void endGPS(){
ssGPS.stop(); //end communication with the gps unit
}
void getGPS(){
while(ssGPS.available()){
gps.encode(ssGPS.read()); //this feeds the serial NMEA data into the "GPS" object to be decoded
}
}
void printGPS(){
if(gps.location.isUpdated()){
Serial.println("Satellite Count: ");
Serial.println(gps.satellites.value());
Serial.println("Latitude: ");
Serial.println(gps.location.lat(), 6);
Serial.println("Longitude: ");
Serial.println(gps.location.lng(), 6);
Serial.println("Altitude Feet: ");
Serial.println(gps.altitude.feet());
Serial.println("Time:");
Serial.println(gps.time.value());
Serial.println("");
} else {
Serial.println("GPS Data not Updated");
}
}
and finally my header file, gps.h:
//gps.h
#ifndef __GPS_HEADER__
#define __GPS_HEADER__
#include <TinyGPS++.h>
#include <SoftwareSerial.h>
void beginGPS();
void endGPS();
void getGPS();
void printGPS();
#endif
In this state, if I attempt to instantiate the SoftwareSerial/TinyGPSPlus objects in the gps.c file, I get the compiler error "Unknown type name 'SoftwareSerial'. It also feels wrong because I'm instantiating the same object in two different places...which feels like a nono.
But if I don't attempt to instantiate them, I get the following: "'ssGPS' undeclared (first use in this function)". I assume this is because the object it's trying to use, doesn't exist yet, at least not within its scope.
As I said, my gut is telling me something to do with pointers...any help would be greatly appreciated!