Here is updated code Version 2.1
All I've done is add the NewSoftSerial library to the code so one does not have to disconnect the GPS transmit every time code is uploaded.
Thanks to Mikal Hart for the NewSoftSerial library and the creators of the WString and LiquidCrystal libraries!
#include <WString.h>
#include <LiquidCrystal.h>
#include <NewSoftSerial.h>
NewSoftSerial gps(8, -1);
String chkStr = String(5);
String dataStr = String(65);
String returnStr = String(12);
LiquidCrystal lcd(12, 11, 6, 5, 4, 3);
char chkByte;
char inByte;
int i = 5;
int page = 1;
void setup() {
lcd.begin(16, 2);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("--Arduino GPS--");
lcd.setCursor(0, 1);
lcd.print("------v2.1------");
attachInterrupt(0, button, RISING);
gps.begin(4800);
}
void loop() {
chkStr = '\0';
if (gps.available() > 0) {
chkByte = gps.read();
while (chkByte == '
) {
if (i > 0) {
if (gps.available() > 0) {
inByte = gps.read();
chkStr.append(inByte);
i--;
}
else {
waitForByte();
}
}
else {
i = 5;
break;
}
while (chkStr.equals("GPRMC")) {
if (gps.available() > 0) {
if (dataStr.length() == dataStr.capacity()) {
break;
}
else {
inByte = gps.read();
dataStr.append(inByte);
}
}
else {
waitForByte();
}
if (dataStr.length() == dataStr.capacity()) {
if(dataStr.charAt(12) == 'A') {
lcd.clear();
if (page == 1) {
printLat(0);
printLon(1);
resetData();
break;
}
else if (page == 2) {
printSpeed(0);
printHeading(1);
resetData();
break;
}
else if (page == 3) {
printTime(0);
printDate(1);
resetData();
break;
}
}
else {
lcd.clear();
aquiringFix();
resetData();
break;
}
}
}
}
}
}
void waitForByte() {
while (true) {
if (gps.available() > 0) {
break;
}
else {
continue;
}
}
}
void aquiringFix() {
lcd.setCursor(0, 0);
lcd.print("----Aquiring----");
lcd.setCursor(0, 1);
lcd.print("------Fix!------");
}
void button() {
if (page >= 3) {
page = 1;
}
else {
page++;
}
}
void dataParse(int section) {
char nextChar;
int commas = 0;
resetReturn();
for (int x = 0; x <= dataStr.length(); x++) {
nextChar = dataStr.charAt(x);
if (nextChar == ',') {
commas++;
continue;
}
if (commas == section) {
returnStr.append(nextChar);
}
else if (commas > section) {
break;
}
}
}
void printLat(int row) {
lcd.setCursor(0, row);
lcd.print("Lt: ");
dataParse(4);
lcd.print(returnStr);
lcd.print(' ');
dataParse(3);
lcd.print('0');
lcd.print(returnStr.charAt(0));
lcd.print(returnStr.charAt(1));
lcd.print(' ');
lcd.print(returnStr.charAt(2));
lcd.print(returnStr.charAt(3));
lcd.print(returnStr.charAt(4));
lcd.print(returnStr.charAt(5));
lcd.print(returnStr.charAt(6));
lcd.print(returnStr.charAt(7));
}
void printLon(int row) {
lcd.setCursor(0, row);
lcd.print("Ln: ");
dataParse(6);
lcd.print(returnStr);
lcd.print(' ');
dataParse(5);
lcd.print(returnStr.charAt(0));
lcd.print(returnStr.charAt(1));
lcd.print(returnStr.charAt(2));
lcd.print(' ');
lcd.print(returnStr.charAt(3));
lcd.print(returnStr.charAt(4));
lcd.print(returnStr.charAt(5));
lcd.print(returnStr.charAt(6));
lcd.print(returnStr.charAt(7));
lcd.print(returnStr.charAt(8));
}
void printSpeed(int row) {
lcd.setCursor(0, row);
dataParse(7);
lcd.print("Speed: ");
lcd.print(returnStr);
lcd.print("Kts");
}
void printHeading(int row) {
lcd.setCursor(0, row);
dataParse(8);
lcd.print("Heading: ");
lcd.print(returnStr);
}
void printDate(int row) {
lcd.setCursor(0, row);
dataParse(9);
lcd.print("Date: ");
lcd.print(returnStr.charAt(0));
lcd.print(returnStr.charAt(1));
lcd.print("-");
lcd.print(returnStr.charAt(2));
lcd.print(returnStr.charAt(3));
lcd.print("-");
lcd.print(returnStr.charAt(4));
lcd.print(returnStr.charAt(5));
}
void printTime(int row) {
lcd.setCursor(0, row);
dataParse(1);
lcd.print("Time: ");
lcd.print(returnStr.charAt(0));
lcd.print(returnStr.charAt(1));
lcd.print(":");
lcd.print(returnStr.charAt(2));
lcd.print(returnStr.charAt(3));
lcd.print(":");
lcd.print(returnStr.charAt(4));
lcd.print(returnStr.charAt(5));
}
void resetData() {
dataStr = '\0';
gps.flush();
}
void resetReturn() {
returnStr = '\0';
}