3g/gps shield on mega 2560

I am trying to activate 3g/gps shield with push button on arduino mega 2560
i am beginner, i've got the following errors:
sketch_may01a.ino: In function 'void loop()':
sketch_may01a:29: error: a function-definition is not allowed here before '{' token
sketch_may01a:256: error: expected }' at end of input sketch_may01a:256: error: expected }' at end of input

can any one help me please!!
Thanks in advance..

here is the code:

const int buttonPin = 12;     // the number of the pushbutton pin
const int ledPin = 13;      // the number of the LED pin

// variables will change:
int buttonState = 0;         // variable for reading the pushbutton status

void setup() {
  // initialize the LED pin as an output:
  pinMode(ledPin, OUTPUT);
  // initialize the pushbutton pin as an input:
  pinMode(buttonPin, INPUT);
}

void loop(){
  // read the state of the pushbutton value:
  buttonState = digitalRead(buttonPin);

  // check if the pushbutton is pressed.
  // if it is, the buttonState is HIGH:

  if (buttonState == HIGH) {
    //digitalWrite(ledPin, HIGH);

    int8_t answer;
    int onModulePin= 2;
    char gps_data[100];
    int counter;

     void setup(){

        pinMode(onModulePin, OUTPUT);
        Serial.begin(115200);

        Serial.println("Starting...");
        power_on();

        delay(5000);

        // starts GPS session in stand alone mode
        answer = sendATcommand("AT+CGPS=1,1","OK",1000);
        if (answer == 0)
        {
            Serial.println("Error starting the GPS");
            Serial.println("The code stucks here!!");
            while(1);
        }
      }
    void loop(){

        answer = sendATcommand("AT+CGPSINFO","+CGPSINFO:",1000);    // request info from GPS
        if (answer == 1)
        {

            counter = 0;
            do{
              while(Serial.available() == 0);
              gps_data[counter] = Serial.read();
              counter++;
            }
            while(gps_data[counter - 1] != '\r');
            gps_data[counter] = '\0';
            if(gps_data[0] == ',')
            {
               Serial.println("No GPS data available");
            }
        else
        {
            Serial.print("GPS data:");
            Serial.println(gps_data);
            Serial.println("");
        }

        }
        else
        {
            Serial.println("Error");
        }

        delay(5000);
    }

    void power_on(){

        uint8_t answer=0;

    // checks if the module is started
        answer = sendATcommand("AT", "OK", 2000);
        if (answer == 0)
        {
        // power on pulse
            digitalWrite(onModulePin,HIGH);
            delay(3000);
            digitalWrite(onModulePin,LOW);

            // waits for an answer from the module
            while(answer == 0){
                // Send AT every two seconds and wait for the answer
                answer = sendATcommand("AT", "OK", 2000);
            }
        }

    }


   int8_t sendATcommand(char* ATcommand, char* expected_answer1, 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(Serial.available() != 0){
                response[x] = Serial.read();
                x++;
                // check if the desired answer is in the response of the module
                if (strstr(response, expected_answer1) != NULL)
                {
                    answer = 1;
                }
            }
            // Waits for the asnwer with time out
        }
        while((answer == 0) && ((millis() - previous) < timeout));

        return answer;
    }
    // sms code
    int8_t answer;
    int onModulePin= 2;
    char aux_string[30];
    char phone_number[]="*********";

    void setup(){

        pinMode(onModulePin, OUTPUT);
        Serial.begin(115200);

        Serial.println("Starting...");
        power_on();

        delay(3000);

        // sets the PIN code
        sendATcommand("AT+CPIN=****", "OK", 2000);

        delay(3000);

        Serial.println("Connecting to the network...");

        while( (sendATcommand("AT+CREG?", "+CREG: 0,1", 500) ||
                sendATcommand("AT+CREG?", "+CREG: 0,5", 500)) == 0 );

        Serial.print("Setting SMS mode...");
        sendATcommand("AT+CMGF=1", "OK", 1000);    // sets the SMS mode to text
        Serial.println("Sending SMS");

        sprintf(aux_string,"AT+CMGS=\"%s\"", phone_number);
        answer = sendATcommand(aux_string, ">", 2000);    // send the SMS number
        if (answer == 1)
        {
            Serial.println("Test-Arduino-Hello World");
            Serial.write(0x1A);
            answer = sendATcommand("", "OK", 20000);
            if (answer == 1)
            {
                Serial.print("Sent ");
            }
            else
            {
                Serial.print("error ");
            }
        }
        else
        {
            Serial.print("error ");
            Serial.println(answer, DEC);
        }

    }


    void loop(){

    }

    void power_on(){

        uint8_t answer=0;

        // checks if the module is started
        answer = sendATcommand("AT", "OK", 2000);
        if (answer == 0)
        {
            // power on pulse
            digitalWrite(onModulePin,HIGH);
            delay(3000);
            digitalWrite(onModulePin,LOW);

            // waits for an answer from the module
            while(answer == 0){     // Send AT every two seconds and wait for the 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);    // Initialice 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 is in the response of the module
                if (strstr(response, expected_answer) != NULL)
                {
                    answer = 1;
                }
            }
        // Waits for the asnwer with time out
        }while((answer == 0) && ((millis() - previous) < timeout));

        return answer;
    }



}
}

arduinocode.c (6.41 KB)

  if (buttonState == HIGH) {
    //digitalWrite(ledPin, HIGH);

    int8_t answer;
    int onModulePin= 2;
    char gps_data[100];
    int counter;

     void setup(){

        pinMode(onModulePin, OUTPUT);
        Serial.begin(115200);

You can't define a function inside another function. You can't have two functions with the same name.

so, what can i do for this?

so, what can i do for this?

Ctrl-A, Ctrl-X and start over.

Amen.

=(
thanks

With all due respect I think that you are being a bit ambitious for a beginner.

Get each sketch that you seem to be trying to combine to work separately and once you have achieved that study them to see how they work, the concept of setup() and loop() (there's lots of information on this site). Then basically combine both setup() and both loop(), which is easier said than done as you may pin or library conflicts.

And remember, Tools-> AutoFormat (CTRL+T) is your friend!

ok friend, thank you very much