GPS wont execute together with an if statement?

iv been having a problem with my thesis project and I found out that when I put the gps codes together with a timer code the gps wont send the coordinates.

#include <Streamers.h>
#include <NMEAGPS.h>
#include <NeoSWSerial.h>
#include <LiquidCrystal_I2C.h>
#include <Wire.h>

#define deviceid 1
#define j9 5
#define NEO_RX 10
#define NEO_TX 11

NeoSWSerial neogps_Serial(NEO_RX, NEO_TX);    // RX, TX //GPS
LiquidCrystal_I2C lcd(0x27, 16, 2);

//GPS Variables
static NMEAGPS  gps;
static gps_fix  fix;


//LCD Variables
String sim808data;
int runtime;
int countminutes;
int countseconds = 0;
bool overdue = false;
unsigned long previousMillis;
bool runme = false;
bool getgps = false;

void setup() {
  Wire.begin();
  //Sim808 Switch Bypass
  pinMode(j9, OUTPUT);
  digitalWrite(j9, HIGH);
  delay(1000);
  digitalWrite(j9, LOW);

  //Start Serial
  Serial.begin(9600);
  while (!Serial);
  neogps_Serial.begin(19200);
  delay(1000);
  //Serial.println(F("Serial Check."));

  lcd.init();
  lcd.backlight();
  lcd.setCursor(0, 0);
  lcd.print("STARTING DEVICE!");
  delay(1000);
  clear_Sim();
  Serial.println(F("\nSMS Cleared!"));

  lcd.clear();
  lcd.setCursor(6, 0);
  lcd.print("SMS");
  lcd.setCursor(4, 1);
  lcd.print("CLEARED!");

  neogps_Serial.print("AT+CGNSPWR=1\r\n");
  delay(100);
  neogps_Serial.print("AT+CGNSTST=1\r\n");
  delay(100);
  neogps_Serial.print("AT+CSCS=\"GSM\"\r\n");
  neogps_Serial.print("AT+CMGF=1\r\n");
  neogps_Serial.print("AT+CNMI=2,2,0,0,0\r\n");
  delay(5000);

  Serial.println(F("GPS Check."));

  lcd.clear();
  lcd.setCursor(2, 0);
  lcd.print("SYSTEM READY");

  Serial.println(F("Device Ready to Use!"));
}

//Loop Declarations
uint32_t loopCount = 0;
uint16_t gpsSeconds;
uint16_t lastTrackSent;

//Milli_Codes
unsigned long time_loc1   = 0;
unsigned long time_send1  = 0;
unsigned long time_loop1  = 0;
unsigned long time_send5  = 0;

//Milli Intervals
const int D_1000 = 1000;

void loop() {
 //proc_LOCATION();
  proc_SIM808();
 // proc_TIMER();
}

void clear_Sim() {
  neogps_Serial.print("AT+CMGD=\"Del All\"");
  delay(300);
  while (neogps_Serial.available())
    Serial.write(neogps_Serial.read());
}

void send_SMS(char* data) {
  neogps_Serial.println("AT+CMGF=1\r\n");
  delay(500);
  neogps_Serial.println("AT+CMGS=\"+63XXXXXXX\"");
  delay(100);
  neogps_Serial.println(data);
  delay(100);
  neogps_Serial.println((char)26);
  delay(100);
  time_send5 = millis();
  neogps_Serial.println();
  delay(500);
}

void proc_SIM808() {
  if (neogps_Serial.available()) {
    sim808data = neogps_Serial.readString();

    if (sim808data.indexOf("start") > 0) {
      int value = sim808data.indexOf("start");
      runtime = sim808data.substring(value + 5).toInt();
      countseconds = 0;
      runme = true;
    }
    if (sim808data.indexOf("stop") > 0) {
      if (overdue)
      {
        lcd.setCursor(0, 1);
        lcd.print("--DEVICE STOP!--");
      } else {
        lcd.setCursor(0, 1);
        lcd.print("--DEVICE STOP!--");
      }
      runme = false;
      overdue = false;
    }
    if (sim808data.indexOf("gps") > 0) {  // Theproblem is here
      proc_LOCATION();
    }
    while (Serial.available()) {
      Serial.read();
    }
    while (neogps_Serial.available()) {
      neogps_Serial.read();
    }
  }
}

void proc_LOCATION() {
  while (gps.available(neogps_Serial)) {
    fix = gps.read();
    gpsSeconds++;
    //Serial.print(F("Position: "));
    if (fix.valid.location && ((gpsSeconds - lastTrackSent) > 5)) {
      char floatlat[10];
      char floatlon[11];
      char message[30] = {0};
      float dlat = fix.latitude();
      float dlon = fix.longitude();
      dtostrf(dlat,  9, 6, floatlat);
      dtostrf(dlon, 10, 6, floatlon);
      //Serial.println(floatlat);
      //Serial.println(floatlon);
      //Transfer Data to dataLocation
      strncat(message, floatlat, 30);
      strncat(message, ";", 30);
      strncat(message, floatlon, 30);
      send_SMS(message);
      lastTrackSent = gpsSeconds; // reset the timer
    } else {
      //Serial.println(F("Delaying Response"));
      proc_SIM808();
    }
  }
}

void proc_TIMER() {
  if (runme) {
    unsigned long currentMillis = millis();
    if (currentMillis - previousMillis >= 1000 ) {
      lcd.clear();
      if (runtime <= 0 && overdue) {
        if (countseconds == 59)
        {
          runtime--;
          countseconds = 0;
        }
        lcd.setCursor(0, 1);
        lcd.print("Cost:");
        lcd.print((runtime * -1));
        countseconds++;
      } else if (runtime >= 0) {
        if (countseconds == 0) {
          runtime--;
          countseconds = 59;
          if (runtime == -1) {
            overdue = true;
            runtime = 0;
            countseconds = 1;
          }
        }
        countseconds--;
      }
      lcd.setCursor(5, 0);
      if (runtime == 0 && overdue) {
        lcd.print("-" + runtime);
      }
      lcd.print(runtime);
      lcd.print(":");
      lcd.print(countseconds);
      previousMillis = currentMillis;
    }
  }
}

this is my full code

and this is the part where I get the problem

void proc_SIM808() {
  if (neogps_Serial.available()) {
    sim808data = neogps_Serial.readString();

    if (sim808data.indexOf("start") > 0) {
      int value = sim808data.indexOf("start");
      runtime = sim808data.substring(value + 5).toInt();
      countseconds = 0;
      runme = true;
    }
    if (sim808data.indexOf("stop") > 0) {
      if (overdue)
      {
        lcd.setCursor(0, 1);
        lcd.print("--DEVICE STOP!--");
      } else {
        lcd.setCursor(0, 1);
        lcd.print("--DEVICE STOP!--");
      }
      runme = false;
      overdue = false;
    }
    if (sim808data.indexOf("gps") > 0) {        // this part 
      proc_LOCATION();
    }
    while (Serial.available()) {
      Serial.read();
    }
    while (neogps_Serial.available()) {
      neogps_Serial.read();
    }
  }
}

EDITED:
PROBLEM 1 (SOLVED): im thinking that maybe its imposible to send the coordinates together with my timer codes if I call the whole gps codes? is there a way that I can call the coordinates from the GPS Codes without calling the whole gps codes? I really appreciate any help from you guys :smiley: its been days since I have this problem.

while (Serial.available()) {
      Serial.read();
    }

Why are you throwing away what you get from Serial.read() ?

oh is it unnecessary I thought I need to clear my input buffer every time it send the coordinates haha

void proc_SIM808() {
  if (neogps_Serial.available()) {
    sim808data = neogps_Serial.readString();

    if (sim808data.indexOf("start") > 0) {

What IS in sim808data? Why are you trying to parse unknown data?

uhm I I just made it that way so that the code wont be long inside an if statement

this one

 if (neogps_Serial.readString().indexOf("gps") > 0) {
      proc_LOCATION();
    }

to this

if (sim808data.indexOf("gps") > 0) {
      proc_LOCATION();
    }

uhm is doing this not good sir?

thank you for the replies ! :smiley:

I also found out that when I combine my gps code together with my LCD codes gps wont work :frowning:
oh also when I text something to my sim808, the screen updates and countdown depending on the value that I texted.

Is it the reason why my gps code wont work? because of the lcd.print on timer? because I saw one thread on this forum titled (loosing GPS fix with LCD).

HARDWARE INFO: I used NeoSWSerial rx10 tx11
Arduino UNO
16x2 LCD with I2C
Sim808 GPS GSM

oh question 1 is solved now! I know now why my if statement on my gps part is not working but I encountered two more problems:

QUESTION 2:
Do i need to use two serials on my LCD and GPS? since whenevener i test each of them i always need to keep changing their Baud rate so if i changed my Serial.begin and my neogps_Serial.begin and made my lcd work, my gps wont work. and vice versa can someone give me a tip? please :smiley:

thank you in advance!

QUESTION 3:
Is there a way to make my sim808 do something while the 16x2 LCD with I2C is counting? for example make my sim808 send coordinates to my phone while its counting? since I noticed that my sim808 will not do something unless I stop first the timer on the LCD

Do i need to use two serials on my LCD and GPS?

Obviously.

You are talking to one, and listening to the other.

Is there a way to make my sim808 do something while the 16x2 LCD with I2C is counting?

The LCD doesn't count. It simply displays values.

If the Arduino is counting in a non-blocking way, then yes, you can do other stuff. If it is counting in a blocking way (using for loops or delay()), then no, you can't.

two serials? hmm like using altsoftserials for gsm and neoswserial for gps sir?

ah yes my bad sorry for that, uhm this is the one that I used on my timer sir what about this one? this is a non blocking code right sir? but why is it that I cant do multi tasking on this one like making it send coordinates while it is counting

void proc_TIMER() {
  if (runme) {
    unsigned long currentMillis = millis();
    if (currentMillis - previousMillis >= 1000 ) {
      lcd.clear();
      if (runtime <= 0 && overdue && getgps) {
        if (countseconds == 59)
        {
          runtime--;
          countseconds = 0;
        }
        lcd.setCursor(0, 1);
        lcd.print("Cost:");
        lcd.print((runtime * -1));
        countseconds++;
      } else if (runtime >= 0) {
        if (countseconds == 0) {
          runtime--;
          countseconds = 59;
          if (runtime == -1) {
            overdue = true;
            runtime = 0;
            countseconds = 1;
          }
        }
        countseconds--;
      }
      lcd.setCursor(5, 0);
      if (runtime == 0 && overdue && getgps) {
        lcd.print("-" + runtime);
      }
      lcd.print(runtime);
      lcd.print(":");
      lcd.print(countseconds);
      previousMillis = currentMillis;

    }
  }
}

this is a non blocking timer right sir?

That snippet is non-blocking. it requires that the function be called over and over.

If you call that from loop(), like

   proc_TIMER();

then it is non-blocking.

If you call it from loop(), like:

   while(runTime > 0)
   {
      proc_TIMER();
   }

then that is blocking code.

By the way, mixing && and & in an if statement is generally not a good idea.

oh the single & sir is a typo its also a &&

ah yes sir I call my proc_TIMER on a loop , so that means there is still a chance that I can send a coordinates while the Arduino is counting sir? how can I do that sir? since I tested on my void proc_SIM808 that unless I make runme = false and overdue = false inside an if statement sir it wont do anything

void proc_SIM808() {
  if (neogps_Serial.available()) {
    sim808data = neogps_Serial.readString();

    if (sim808data.indexOf("start") > 0) {
      int value = sim808data.indexOf("start");
      runtime = sim808data.substring(value + 5).toInt();
      countseconds = 0;
      runme = true;
    }

    if (sim808data.indexOf("stop") > 0) {
      if (overdue)
      {
        lcd.setCursor(0, 1);
        lcd.print("--DEVICE STOP!--");
      } else {
        lcd.setCursor(0, 1);
        lcd.print("--DEVICE STOP!--");
      }
      runme = false;
      overdue = false;
    }

    if (sim808data.indexOf("gps") > 0) {    // this code sir is for sending of location to my phone
      if (getgps) {
        proc_LOCATION();
      } else {
        proc_LOCATION();
      }
      runme = false;      // Stops the timer 
      overdue = false;
    }
  }
}

my updated code

//getgps code test

#include <Streamers.h>
#include <NMEAGPS.h>
#include <NeoSWSerial.h>
#include <LiquidCrystal_I2C.h>
#include <Wire.h>

#define deviceid 1
#define j9 5
#define NEO_RX 10
#define NEO_TX 11

NeoSWSerial neogps_Serial(NEO_RX, NEO_TX);    // RX, TX //GPS
LiquidCrystal_I2C lcd(0x27, 16, 2);

//GPS Variables
static NMEAGPS  gps;
static gps_fix  fix;


//LCD Variables
String sim808data;
int runtime;
int countminutes;
int countseconds = 0;
bool overdue = false;
unsigned long previousMillis;
bool runme = false;
bool getgps = false;

void setup() {
  Wire.begin();
  //Sim808 Switch Bypass
  pinMode(j9, OUTPUT);
  digitalWrite(j9, HIGH);
  delay(1000);
  digitalWrite(j9, LOW);

  //Start Serial
  Serial.begin(9600);
  while (!Serial);
  neogps_Serial.begin(19200);
  delay(2000);
  //Serial.println(F("Serial Check."));

  lcd.init();
  lcd.backlight();
  lcd.setCursor(0, 0);
  lcd.print("STARTING DEVICE!");
  delay(1000);
  clear_Sim();
  Serial.println(F("\nSMS Cleared!"));

  lcd.clear();
  lcd.setCursor(6, 0);
  lcd.print("SMS");
  lcd.setCursor(4, 1);
  lcd.print("CLEARED!");

  neogps_Serial.print("AT+CGNSPWR=1\r\n");
  delay(100);
  neogps_Serial.print("AT+CGNSTST=1\r\n");
  delay(100);
  neogps_Serial.print("AT+CSCS=\"GSM\"\r\n");
  neogps_Serial.print("AT+CMGF=1\r\n");
  neogps_Serial.print("AT+CNMI=2,2,0,0,0\r\n");
  delay(5000);

  Serial.println(F("GPS Check."));

  lcd.clear();
  lcd.setCursor(2, 0);
  lcd.print("SYSTEM READY");

  Serial.println(F("Device Ready to Use!"));
}

//Loop Declarations
uint32_t loopCount = 0;
uint16_t gpsSeconds;
uint16_t lastTrackSent;

//Milli_Codes
unsigned long time_loc1   = 0;
unsigned long time_send1  = 0;
unsigned long time_loop1  = 0;
unsigned long time_send5  = 0;

//Milli Intervals
const int D_1000 = 1000;


void loop() {
  proc_SIM808();
  proc_TIMER();
}

void clear_Sim() {
  neogps_Serial.print("AT+CMGD=\"Del All\"");
  delay(300);
  while (neogps_Serial.available())
    Serial.write(neogps_Serial.read());
}

void send_SMS(char* data) {
  unsigned long currentMillis = millis();
  if (currentMillis - previousMillis >= 1000 ) {
    neogps_Serial.println("AT+CMGF=1\n");
    neogps_Serial.println("AT+CMGS=\"+639058072930\"\n");
    neogps_Serial.println(data);
    neogps_Serial.println((char)26);
    time_send5 = millis();
    neogps_Serial.println();
    previousMillis = currentMillis;
  }
}

void proc_SIM808() {
  if (neogps_Serial.available()) {
    sim808data = neogps_Serial.readString();

    if (sim808data.indexOf("start") > 0) {
      int value = sim808data.indexOf("start");
      runtime = sim808data.substring(value + 5).toInt();
      countseconds = 0;
      runme = true;
    }

    if (sim808data.indexOf("gps") > 0) {
      if (getgps) {
        proc_LOCATION();
      } else {
        proc_LOCATION();
      }
      runme = false;
      overdue = false;
    }

    if (sim808data.indexOf("stop") > 0) {
      if (overdue)
      {
        lcd.setCursor(0, 1);
        lcd.print("--DEVICE STOP!--");
      } else {
        lcd.setCursor(0, 1);
        lcd.print("--DEVICE STOP!--");
      }
      runme = false;
      overdue = false;
    }
  }
}

void checkGPS() {
  static bool warningPrinted = false;

  if (not warningPrinted and (millis() > 5000) and (gps.statistics.chars < 10))
  {
    Serial.println(F("No GPS detected: check wiring."));
    warningPrinted = true;
  }
}

void proc_LOCATION() {
  char message[30] = {0};
  char floatlat[10];
  char floatlon[11];
  float dlat, dlon;

  // Check for and parse any available GPS characters
  if (gps.available( neogps_Serial )) {
    // Once per second, a complete structure of GPS fields is ready.
    fix = gps.read();
    gpsSeconds++;

    if (gpsSeconds >= 5) {
      gpsSeconds = 0;
      Serial.println("\nNumber of satellies: ");
      if (fix.valid.satellites)
        Serial.println( fix.satellites );
      // Latitude
      if (fix.valid.location) {
        dlat = fix.latitude();
              }
      // Longitude
      if (fix.valid.location) {
        dlon = fix.longitude();
      }

      dtostrf(dlat,  9, 6, floatlat);
      dtostrf(dlon, 10, 6, floatlon);
      Serial.println(floatlat);
      Serial.println(floatlon);
      strncat(message, floatlat, 30);
      strncat(message, ";", 30);
      strncat(message, floatlon, 30);
      send_SMS(message);
    } else
    {
      Serial.println("Waiting");
    }
  } checkGPS();
}

void proc_TIMER() {
  if (runme) {
    unsigned long currentMillis = millis();
    if (currentMillis - previousMillis >= 1000 ) {
      lcd.clear();
      if (runtime <= 0 && overdue && getgps) {
        if (countseconds == 59)
        {
          runtime--;
          countseconds = 0;
        }
        lcd.setCursor(0, 1);
        lcd.print("Cost:");
        lcd.print((runtime * -1));
        countseconds++;
      } else if (runtime >= 0) {
        if (countseconds == 0) {
          runtime--;
          countseconds = 59;
          if (runtime == -1) {
            overdue = true;
            runtime = 0;
            countseconds = 1;
          }
        }
        countseconds--;
      }
      lcd.setCursor(5, 0);
      if (runtime == 0 && overdue && getgps) {
        lcd.print("-" + runtime);
      }
      lcd.print(runtime);
      lcd.print(":");
      lcd.print(countseconds);
      previousMillis = currentMillis;

    }
  }
}