Arduino 3G/GPS (SMS)

Here I insert the main code and its functions.

Main code:

int8_t answer, counter;
char salir;
int x;
int onModulePin= 2;  // Pin para activar o desactivar el módulo 3G
char aux_string[30];
char response[255];
char SMS[200];
int a=0;
int y=0;
int z=0;
char phone_number[]="3214435420";
//int b= 0; 
char name[20];
char picture_name[20];
//GPS 
char gps_data[100];
int contador;
char data[255];
char latitude[12],longitude[13];
char date[7],UTC_time[9];
char speed_OG[7],altitude[7];

void(*Reset)(void)=0;

void setup(){
  
    pinMode(13, OUTPUT);

    pinMode(onModulePin, OUTPUT);  // Pin para activar modulo como salida
    Serial.begin(115200);     //Configuramos puerto UART

    Serial.println("Starting..."); //Activamos el módulo 3G
    power_on();
    
    delay(3000);
    
    //Configuramos SD como espacio de almacenamiento principal.
    sendATcommand("AT+FSLOCA=1","OK",2000);
    Serial.println("Setting SD storage...");
    Serial.println("AT+CNSM=1"); // Enables noise supression 
    while(Serial.read()!='K');    
    
    Serial.println("Setting SMS mode...");
    sendATcommand("AT+CMGF=1", "OK", 1000);    // Configuramos el SMS a modo texto 
   //  Serial.println("AT+CNMI=3,3,0,0"); // set module to send SMS data to serial out upon receipt 
    sendATcommand("AT+CPMS=\"SM\",\"SM\",\"SM\"", "OK", 1000);    // selects the memory
   //delay(5000);
   //answer = sendATcommand("AT+CMGD=1,4", "+CMGD:", 2000);
   //Serial.println("AT+CMGD=1,4"); // delete all SMS 

Serial.println("Send your SMS, 20 sec remaining...");
    
    delay(20000);
   //answer = sendATcommand("AT+CMGRD=1", "+CMGRD:", 2000);
   answer = sendATcommand("AT+CMGR=1", "+CMGR:", 2000);    // Se lee el primer SMS en la sim card
    if (answer == 1)
    {
        answer = 0;
        while(Serial.available() == 0);
        // Este buble lee los datos del SMS, lo q llega del SMS
        do{
            // Si hay datos en el buffer de entrada UART, lo lee y comprueba la answer(respuesta)
            if(Serial.available() > 0){    
                SMS[x] = Serial.read();
                   
                    if (SMS[x] == '5')
                    {
                      
                      Serial.println("Voice");
                      rec_voz();
                      delay(2000);
                      Reset();
                    }
                   
                    else if (SMS[x] == '4')
                    {
                      Serial.println("Alarm");
                      Alarma();
                      delay(2000);
                      Reset();
                    }
                    else if (SMS[x] == '2')
                    {
                      Serial.println("Taking pic!");
                      tomaFoto();
                      delay(2000);
                      Reset();
                    }
                    else if (SMS[x] == '1')
                    {
                      Serial.println("Getting GPS position...");
                    get_GPS();
                    delay(2000);
                    Reset();
                    }
                     
               
                x++;
                              
                // Se comprueba si la respuesta deseada (OK) es en la respuesta del módulo 
                if (strstr(SMS, "OK") != NULL)    
                {
                    answer = 1;
                }
            }
            
            
        }
        

        while(answer == 0);    // Espera por la respuesta (answer) con el tiempo de espera
        
        SMS[x] = '\0';
        
      //  Serial.print(SMS);    // Se imprime el valor de SMS     -->Serial.print(val) 
       
                   
    }
    
    
    
    else
    {
        Serial.print("error "); // se imprime error
        //imprime como codificado ASCII decimal
        Serial.println(answer, DEC); //Serial.print(val, format) -> format: especifica el número de la base (para números enteros) o el número de posiciones decimales (para números de coma flotante o tipo "float")
    // Serial.println("AT+CFUN=1,1"); //reset shield
  }
//sendATcommand("AT+CMGD=1,4", "+CMGD:", 2000);
//delay(2000);
sendATcommand("AT+CFUN=1,1","+CFUN:",2000);
Reset();
}

void loop(){
}


void power_on(){

    uint8_t answer=0;
    
    // / / Comprueba si el módulo es iniciado
    answer = sendATcommand("AT", "OK", 2000);
    if (answer == 0)
    {
        // power on pulse
        digitalWrite(onModulePin,HIGH);
        delay(3000);
        digitalWrite(onModulePin,LOW);
        // Espera por una respuesta(answer) del módulo
        while(answer == 0){     // Enviar AT cada dos segundos y esperar la  (answer)
        answer = sendATcommand("AT", "OK", 2000); 
         
            
        }
    }
    
}

int8_t sendATcommand(char* ATcommand, char* expected_answer, unsigned int timeout){

    uint8_t x=0,  answer=0;
    char response[100];
    unsigned long previous;

    memset(response, '\0', 100);    // Inicializa la cadena (string)

    delay(100);

    while( Serial.available() > 0) Serial.read();    // Limpiar el buffer de entrada

    Serial.println(ATcommand);    // Enviar el comando AT


        x = 0;
    previous = millis();

    // Este bucle espera por la respuesta (answer)
    do{
        // Si hay datos en el buffer de entrada UART, lo lee y comprueba la asnwer
        if(Serial.available() != 0){    
            response[x] = Serial.read();
            x++;
            // comprobar si la respuesta deseada es en la respuesta del módulo
            if (strstr(response, expected_answer) != NULL)    
            {
                answer = 1;
            }
        }
        // Waits for the asnwer with time out
    }while((answer == 0) && ((millis() - previous) < timeout));    

    return answer;
}

int8_t sendATcommand2(char* ATcommand, char* expected_answer1, char* expected_answer2,
    unsigned int timeout){

    uint8_t x=0,  answer=0;
    char response[100];
    unsigned long previous;

    memset(response, '\0', 100);    // Initialize the string

    delay(100);

    while( Serial.available() > 0) Serial.read();    // Clean the input buffer

    Serial.println(ATcommand);    // Send the AT command 

    x = 0;
    previous = millis();

    // this loop waits for the answer
    do{
        // if there are data in the UART input buffer, reads it and checks for the asnwer
        if(Serial.available() != 0){    
            response[x] = Serial.read();
            x++;
            // check if the desired answer 1 is in the response of the module
            if (strstr(response, expected_answer1) != NULL)    
            {
                answer = 1;
            }
            // check if the desired answer 2 is in the response of the module
            if (strstr(response, expected_answer2) != NULL)    
            {
                answer = 2;
            }
        }
        // Waits for the asnwer with time out
    }while((answer == 0) && ((millis() - previous) < timeout));    

    return answer;
}