two if available statements in one loop

I want to send a cellular module a text message and it respond with its coordinates. I need to receive coordinates from a gps over a serial line using if(gps.available()) and also wait for a response from my cellular module on a separate serial port using if(cell.available() . In my code if I wait for a text to be received using if(cell.available()>0) then when its received the if(gps.available()) thats inside of the cell if statement wont work, it just skips right over the gps code. this is kinda hard to explain so ill post the code and just tell me if you guys can spot a problem.

#include <TinyGPS.h>
#include <NewSoftSerial.h>
#include <string.h>   
byte button, ctrlz = 26;
char inchar;
TinyGPS gps;
NewSoftSerial GPS(14, 15);
NewSoftSerial cell(2,3);
float flat, flon;
float latitude, longitude;



void getgps(TinyGPS &gps);
void setup()
{
  Serial.begin(9600);
  cell.begin(9600);
  GPS.begin(4800);
  delay(30000); 
  cell.println("AT+CMGF=1");
  delay(200);
  cell.println("AT+CNMI=3,3,0,0"); 
  delay(200);
  Serial.println("ready");
  pinMode(13, OUTPUT);
  digitalWrite(13, HIGH);
}


void loop() {
  if(cell.available() >0)
  {
    inchar=cell.read();
    Serial.print(inchar);
    if (inchar=='C'){
      delay(10);
      inchar=cell.read();
      if (inchar=='o'){ 
        delay(10);
        inchar=cell.read();
        if (inchar=='o'){ 
          delay(10);
          inchar=cell.read();
          if (inchar=='d'){ 
            delay(10);
            inchar=cell.read();
            if (inchar=='s'){ 
              delay(10);
                Serial.println("recieved");

              if(GPS.available()){
                byte c = GPS.read();
                if(gps.encode(c)){
                  float latitude, longitude;
                  gps.f_get_position(&latitude, &longitude);
                  Serial.println("good");
                  Serial.println("sending");
                  cell.println("AT+CMGF=1");
                  cell.print("AT+CMGS=");             
                  cell.print(34,BYTE);                
                  cell.print("214934****");            
                  cell.println(34,BYTE);
                  delay(500);   
                  cell.print(latitude, 4);
                  cell.print(", ");
                  cell.print(longitude, 5);
                  cell.println(26,BYTE);  
                  delay(500);
                }
              }
            }
          }
        }
      }
    }
  }
}

The if (cell.available () > 0) guarantees you have one byte, but you proceed to read more. Maybe the delays will help, maybe not. Read this about processing serial data without blocking:

I can receive bytes from the cell fine. The problem Im having is when it comes to reading the gps from inside the cells if statement. I tested it with the Serial monitor and confirmed that when if(gps.available()) is inside the if(cell.available()) it is always false meaning its not getting anything in from the gps serial port. However when I put the gps if statement on the outside and the cell if statement inside it does the opposite. I get gps readings but nothing for the cell.

I think it's a design mistake to nest one inside the other.

The code will still do what you told it to do - it will accurate detect whether there is input available when your sketch reaches that code, but the design is poor.

The reason is that these two serial streams are not dependent on each other. You can get input on the GPS stream without having input on the cellular stream, and vice versa. If you only think about processing the GPS stream when there is input on the cellular stream then what is going to happen to the input on the GPS stream the rest of the time? You can no longer assume that he data you're getting is current. Worse, you have to guess what happens in the GPS module when its output stream gets congested.

If the two streams are in fact independent of each other then the better design imo is to handle both streams independently within loop(). If your GPS module is sending continual updates then process those and remember the latest data, then it is available when you need it to respond to messages on the cellular interface.

So I tried putting the if(gps.available()) outside of the if(cell.available()) but now when I send a text to the cell module I dont get any responce. So here it receives the gps coords first then does the cell if statement but there is still no response.

#include <TinyGPS.h>
#include <NewSoftSerial.h>
#include <string.h>   
byte button, ctrlz = 26;
char inchar;
TinyGPS gps;
NewSoftSerial GPS(14, 15);
NewSoftSerial cell(2,3);
float flat, flon;
float latitude, longitude;



void getgps(TinyGPS &gps);
void setup()
{
  Serial.begin(9600);
  cell.begin(9600);
  GPS.begin(4800);
  delay(30000); 
  cell.println("AT+CMGF=1");
  delay(200);
  cell.println("AT+CNMI=3,3,0,0"); 
  delay(200);
  Serial.println("ready");
  pinMode(13, OUTPUT);
  digitalWrite(13, HIGH);
}


void loop() {
  if(GPS.available()){
    byte c = GPS.read();
    if(gps.encode(c)){
      float latitude, longitude;
      gps.f_get_position(&latitude, &longitude);
    }
  }

  if(cell.available() >0)
  {
    inchar=cell.read();
    Serial.print(inchar);
    if (inchar=='C'){
      delay(10);
      inchar=cell.read();
      if (inchar=='o'){ 
        delay(10);
        inchar=cell.read();
        if (inchar=='o'){ 
          delay(10);
          inchar=cell.read();
          if (inchar=='d'){ 
            delay(10);
            inchar=cell.read();
            if (inchar=='s'){ 
              delay(10);
              Serial.println("recieved");
              cell.println("AT+CMGF=1");
              cell.print("AT+CMGS=");             
              cell.print(34,BYTE);                
              cell.print("214934****");            
              cell.println(34,BYTE);
              delay(500);   
              cell.print(latitude, 4);
              cell.print(", ");
              cell.print(longitude, 5);
              cell.println(26,BYTE);  
              delay(500);
            }
          }
        }
      }
    }
  }
}

anyone else have any ideas???

My idea is to follow my earlier suggestion rather than saying it isn’t required because you can do one or the other just fine. It’s a timing problem. If you rewrite it to not need any delay() statements you will be getting somewhere. Why are they there anyway? If you say “oh they seem to be needed to make it work” then you are showing that you have timing issues.

Yes.

Throw away the code you have reading the cellular stream. I mean drag the mouse across that bit of code, hit delete and then save the file.

Now re-read and understand the advice that Nick Gammon gave you and follow it. That is how to process a serial input stream.