I have a project that uses an Arduino Mega 2560 with a 2.4" LCD shield that I want to use a GPS on one of the serial ports. The GPS works fine without the shield as it gives a lot of information on the serial monitor but will not show the information on the LCD shield until some time (20-30 minutes or more) has passed and then the location will not update as I move about. I have tried it on Serial 1 and Serial 2 with the same results, changing the coding to reflect the different serial ports. The soil fertility sensor works fine and displays the proper information.
I have worked around this problem by adapting it to an ESP32 so it isn't critical that I get an answer.
[code]
#include <Adafruit_GFX.h>
#include <MCUFRIEND_kbv.h>
#include <TinyGPSPlus.h>
#include <SPI.h>
#include <SD.h>
File myFile;
TinyGPSPlus gps; // create gps object
MCUFRIEND_kbv tft;
#define BLACK 0x0000
#define RED 0xF800
#define YELLOW 0xFFE0
#define TFT_GREENYELLOW 0xB7E0 /* 180, 255, 0 */
#define TFT_PINK 0xFC9F
#define TFT_MAGENTA 0xF81F
#define LCD_CS A3
#define LCD_CD A2
#define LCD_WR A1
#define LCD_RD A0
#define LCD_RESET A4
//#define RE 8
//#define DE 7
const int DIN_PIN = 23;
String gpstext;
//const byte code[]= {0x01, 0x03, 0x00, 0x1e, 0x00, 0x03, 0x65, 0xCD};
const byte nitro[] = {0x01, 0x03, 0x00, 0x1e, 0x00, 0x01, 0xe4, 0x0c};
const byte phos[] = {0x01, 0x03, 0x00, 0x1f, 0x00, 0x01, 0xb5, 0xcc};
const byte pota[] = {0x01, 0x03, 0x00, 0x20, 0x00, 0x01, 0x85, 0xc0};
byte values[11];
void setup() {
Serial.begin(115200);
Serial1.begin(9600);
Serial1.begin(9600);
pinMode( DIN_PIN, INPUT_PULLUP );
tft.reset();
tft.begin(0x9341);
tft.setRotation(1);
tft.fillScreen(BLACK);
if (!SD.begin()) {
Serial.println("fail");
tft.setTextColor(TFT_PINK );
tft.setCursor(160, 100);
tft.setTextSize(3);
tft.print("No SD");
delay(15000);
tft.setTextColor(BLACK);
tft.setCursor(160, 100);
tft.setTextSize(3);
tft.print("No SD");
}
else {
if (SD.exists("/Filed.csv"))
{
}
File myFile = SD.open("Field.csv", FILE_WRITE);
myFile.print("Latitude");
myFile.print(", ");
myFile.print("Longitude");
myFile.print(", ");
myFile.print("Nitrogen");
myFile.print(", ");
myFile.print("Phosphorus");
myFile.print(", ");
myFile.println("Potash");
myFile.close();
}
}
void loop() {
int value;
byte val1, val2, val3;
val1 = nitrogen();
delay(250);
val2 = phosphorous();
delay(250);
val3 = potassium();
delay(250);
while (Serial2.available() > 0)
if (gps.encode(Serial2.read())) {
if (displayInfo() != "0")
{
// Get GPS string
gpstext = displayInfo();
}
}
tft.setTextColor(YELLOW);
tft.setCursor(7, 10);
tft.setTextSize(2);
tft.print("Latitude");
tft.setCursor(135, 10);
tft.print("Longitude");
tft.setCursor(270, 10);
tft.print("Sats");
tft.fillRect(5, 40, 320, 20, BLACK);
tft.setCursor(5, 40);
tft.setTextColor(YELLOW);
tft.print(gpstext);
tft.setCursor(125, 40);
//tft.print(String(longi));
tft.setCursor(7,90);
tft.setTextSize(3);
tft.print("N:");
tft.fillRect(45, 90,52,25,BLACK);
tft.setCursor(45, 90);
tft.setTextColor(YELLOW);
tft.print(val1);
tft.setCursor(7, 140);
tft.print("P:");
tft.fillRect(45,140,52,25,BLACK);
tft.setCursor(45, 140);
tft.setTextColor(YELLOW);
tft.print(val2);
tft.setCursor(7, 190);
tft.print("K:");
tft.fillRect(45, 190,52,25,BLACK);
tft.setCursor(45, 190);
tft.setTextColor(YELLOW);
tft.print(val3);
value = digitalRead(DIN_PIN);
Serial.println(value);
if (value < 1) {
Serial.println(value);
File myFile = SD.open("Field.csv", FILE_WRITE);
// if the file is available, write to it:
// if (myFile) {
myFile.print(gpstext);
// myFile.print(", ");
// myFile.print(longi);
myFile.print(", ");
myFile.print(val1);
myFile.print(", ");
myFile.print(val2);
myFile.print(", ");
myFile.println(val3);
myFile.close();
tft.setTextColor(TFT_MAGENTA );
tft.setCursor(150, 100);
tft.setTextSize(3);
tft.print("Recording");
delay(1000);
tft.setCursor(150, 130);
tft.print("Recording");
delay(1000);
tft.setCursor(180, 160);
tft.print("DONE!");
delay(2000);
tft.setTextColor(BLACK);
tft.setCursor(150, 100);
tft.setTextSize(3);
tft.print("Recording");
tft.setCursor(150, 130);
tft.print("Recording");
tft.setCursor(180, 160);
tft.print("DONE!");
// }
}
delay(1000);
}
byte nitrogen() {
// digitalWrite(DE, HIGH);
// digitalWrite(RE, HIGH);
delay(10);
if (Serial1.write(nitro, sizeof(nitro)) == 8) {
// digitalWrite(DE, LOW);
// digitalWrite(RE, LOW);
for (byte i = 0; i < 7; i++) {
//Serial.print(mod.read(),HEX);
values[i] = Serial1.read();
// Serial.print(values[i], HEX);
}
// Serial.println();
}
return values[4];
}
byte phosphorous() {
// digitalWrite(DE, HIGH);
// digitalWrite(RE, HIGH);
delay(10);
if (Serial1.write(phos, sizeof(phos)) == 8) {
// digitalWrite(DE, LOW);
// digitalWrite(RE, LOW);
for (byte i = 0; i < 7; i++) {
//Serial.print(mod.read(),HEX);
values[i] = Serial1.read();
// Serial.print(values[i], HEX);
}
// Serial.println();
}
return values[4];
}
byte potassium() {
// digitalWrite(DE, HIGH);
// digitalWrite(RE, HIGH);
delay(10);
if (Serial1.write(pota, sizeof(pota)) == 8) {
// digitalWrite(DE, LOW);
// digitalWrite(RE, LOW);
for (byte i = 0; i < 7; i++) {
//Serial.print(mod.read(),HEX);
values[i] = Serial1.read();
// Serial.print(values[i], HEX);
}
// Serial.println();
return values[4];
}
}
String displayInfo()
{
// Define empty string to hold output
String gpsdata = "";
// Get latitude and longitude
if (gps.location.isValid())
{
gpsdata = String(gps.location.lat(), 6);
gpsdata += (", ");
gpsdata += String(gps.location.lng(), 6);
gpsdata += (", ");
gpsdata += String(gps.satellites.value());
}
else
{
return "0";
}
// Return completed string
return gpsdata;
}
[/code]