Error message: "initializeGSM" was not declared in this scope

This message does not pup up when I run the gsm code by itself but it comes up when I combine it with a code that sends data. I’m not very sure how to declare ‘initializeGSM’ so if anyone knows how that would be great. This is the code: (the error is the third line after ‘void setup’

#include <SoftwareSerial.h>
#include <Wire.h>
#define DEBUG true
#define NUMBER  "xxxxxxxx" //phone number
#define MSG     "TESTING 2"

SoftwareSerial gsm(10, 11);

const int valPin = A0;

char reply[100];
int val;
bool smsSent;

void setup() {
  Wire.begin();        // join i2c bus (address optional for master)
  Serial.begin(9600);  // start serial for output
  initializeGSM();
}

void loop() {
Wire.requestFrom(8, 1);    // request 1 byte from slave device #8
while (Wire.available()) { // slave may send less than requested
char c = Wire.read(); // receive a byte as character
if (c == '0'){
val = analogRead(valPin);
Serial.println(val);
if (val >= 500){
  if(!smsSent){
  sendSMS(NUMBER,MSG);
  smsSent = true;
  }
}

else{
  smsSent = false;
}
}

bool initializeGSM() {
  char *command[] = { "AT", "ATE0", "AT+CPIN?", "AT+CLIP=1", "AT+CMEE=0", "AT+CMGF=1", "AT+CPBS=\"SM\"", "AT+CMGD=,4" };
  char *response[] = { "OK", "OK", "READY", "OK", "OK", "OK", "OK", "OK" };
  int  time[] = { 1000, 1000, 1000, 1000, 1000, 1000, 1000, 5000 };
  
  // initialize GSM 8 commands
  gsm.begin(9600);
  Serial.print("Initializing GSM.. ");
  for (int i = 0; i < 8; i++) {
    int tries = 0;
    // try each command for 5 times
    while (sendATCommandWaitResponse(command[i], response[i], time[i]) == false) {
      tries++;

      // if fail 10 times, exit
      if (tries == 10) {
        Serial.println("FAILED!");
        return false;
      }
    }
  }
  // wait for gsm to connect to network
  while (!sendATCommandWaitResponse("AT+CREG?", "+CREG: 0,1", 1000));
  Serial.println("OK!");
  return true;
  delay(10);
}

bool sendSMS(char *num, char *msg) {
  char buffer[30];
  byte i;
  // create end character to terminate SMS command
  char end[2];
  end[0] = 0x1a;
  end[1] = '\0';
  
  if (DEBUG) {
    Serial.print("Sending SMS.. ");
  }
  // issue send SMS command
  sprintf(buffer, "AT+CMGS=\"%s\"", num);
  // wait for reply to input message
  if (sendATCommandWaitResponse(buffer, ">", 1000)) {
    gsm.print(msg);
    // send end command to start sending sms
    if (sendATCommandWaitResponse(end, "+CMGS", 10000)) {
      if (DEBUG) {
        Serial.println("OK!");
      }
      return true;
    }
    else {
      if (DEBUG) {
        Serial.println("FAILED!");
      }
      return false;
    }
  }
  else {
    if (DEBUG) {
      Serial.println("FAILED!");
    }
    return false;
  }
}

bool sendATCommandWaitResponse(char* command, char* response, long timeout) {
  if (DEBUG) {
    Serial.print("Send: "); Serial.println(command);
  }
  gsm.println(command);

  long startTime = millis();
  int pointer = 0;

  // if not timed out, check for reply
  while (millis() - startTime < timeout) {
    while (gsm.available()) {
      char c = gsm.read();
      reply[pointer++] = c;
      reply[pointer] = '\0';
    }

    // check reply for the response we want
    if (strstr(reply, response) != NULL) {
      if (DEBUG) {
        Serial.print("OK! Got: "); Serial.println(reply);
        Serial.println();
      }
      return true;
    }
  }

  if (DEBUG) {
    Serial.print("FAILED! Got: "); Serial.println(reply);
    Serial.println();
  }
  return false;
}

The code calls the initializeGSM() function but as the closing } of the loop() function is missing the function does not actually exist because it is being declared inside the loop() function

Do yourself a favour and Auto format the code in the IDE. It should show up the problem

I added the "}" and autoformatted but the same issue still came up.

CelineGoh: I added the "}" and autoformatted but the same issue still came up.

Check that each { has a corresponding } in the right place. Putting each { and } alone on a line can help spot the problem. Putting the mouse over a { shows the corresponding } by putting a box round it.

Auto format will not fix problems but can show them

Each function definition should be on the left margin after Auto format. If not then you likely have at least one } missing earlier in the code

Auto format the code and post it here if you still have a problem and can't find it

everything should be fine since the original code:

#include <SoftwareSerial.h>

#define DEBUG true
#define NUMBER  "xxxxxxxx"
#define MSG     "TESTING 2"

SoftwareSerial gsm(10, 11);

const int valPin = A0;

char reply[100];
int val;
bool smsSent;

void setup() {
  Serial.begin(9600);
  
  initializeGSM();
}

void loop() {
val = analogRead(valPin);
Serial.println(val);
if (val >= 500){
  if(!smsSent){
  sendSMS(NUMBER,MSG);
  smsSent = true;
  }
}

else{
  smsSent = false;
}
}

bool initializeGSM() {
  char *command[] = { "AT", "ATE0", "AT+CPIN?", "AT+CLIP=1", "AT+CMEE=0", "AT+CMGF=1", "AT+CPBS=\"SM\"", "AT+CMGD=,4" };
  char *response[] = { "OK", "OK", "READY", "OK", "OK", "OK", "OK", "OK" };
  int  time[] = { 1000, 1000, 1000, 1000, 1000, 1000, 1000, 5000 };
  
  // initialize GSM 8 commands
  gsm.begin(9600);
  Serial.print("Initializing GSM.. ");
  for (int i = 0; i < 8; i++) {
    int tries = 0;
    // try each command for 5 times
    while (sendATCommandWaitResponse(command[i], response[i], time[i]) == false) {
      tries++;

      // if fail 10 times, exit
      if (tries == 10) {
        Serial.println("FAILED!");
        return false;
      }
    }
  }
  // wait for gsm to connect to network
  while (!sendATCommandWaitResponse("AT+CREG?", "+CREG: 0,1", 1000));
  Serial.println("OK!");
  return true;
  delay(10);
}

bool sendSMS(char *num, char *msg) {
  char buffer[30];
  byte i;
  // create end character to terminate SMS command
  char end[2];
  end[0] = 0x1a;
  end[1] = '\0';
  
  if (DEBUG) {
    Serial.print("Sending SMS.. ");
  }
  // issue send SMS command
  sprintf(buffer, "AT+CMGS=\"%s\"", num);
  // wait for reply to input message
  if (sendATCommandWaitResponse(buffer, ">", 1000)) {
    gsm.print(msg);
    // send end command to start sending sms
    if (sendATCommandWaitResponse(end, "+CMGS", 10000)) {
      if (DEBUG) {
        Serial.println("OK!");
      }
      return true;
    }
    else {
      if (DEBUG) {
        Serial.println("FAILED!");
      }
      return false;
    }
  }
  else {
    if (DEBUG) {
      Serial.println("FAILED!");
    }
    return false;
  }
}

bool sendATCommandWaitResponse(char* command, char* response, long timeout) {
  if (DEBUG) {
    Serial.print("Send: "); Serial.println(command);
  }
  gsm.println(command);

  long startTime = millis();
  int pointer = 0;

  // if not timed out, check for reply
  while (millis() - startTime < timeout) {
    while (gsm.available()) {
      char c = gsm.read();
      reply[pointer++] = c;
      reply[pointer] = '\0';
    }

    // check reply for the response we want
    if (strstr(reply, response) != NULL) {
      if (DEBUG) {
        Serial.print("OK! Got: "); Serial.println(reply);
        Serial.println();
      }
      return true;
    }
  }

  if (DEBUG) {
    Serial.print("FAILED! Got: "); Serial.println(reply);
    Serial.println();
  }
  return false;
}

works completely fine and I copied it into another code:

#include <Wire.h>

void setup() {
  Wire.begin();        // join i2c bus (address optional for master)
  Serial.begin(9600);  // start serial for output
}

void loop() {
Wire.requestFrom(8, 1);    // request 1 byte from slave device #8
while (Wire.available()) { // slave may send less than requested
char c = Wire.read(); // receive a byte as character
if (c == '0'){
Serial.println("zero");
}

both codes are ok but when I combine them many declaration issues start popping up

So post (or attach) the combined version.

wildbill: So post (or attach) the combined version.

that's in the main post

Where do you think the loop function ends?

The compiler disagrees.

It helps if you post all the error message, not just the bit you think is important.

#include <SoftwareSerial.h>
#include <Wire.h>
#define DEBUG true
#define NUMBER  "xxxxxxxx" //phone number
#define MSG     "TESTING 2"

SoftwareSerial gsm(10, 11);

const int valPin = A0;

char reply[100];
int val;
bool smsSent;

void setup() {
  Wire.begin();        // join i2c bus (address optional for master)
  Serial.begin(9600);  // start serial for output
  initializeGSM();
}

void loop() {
Wire.requestFrom(8, 1);    // request 1 byte from slave device #8
while (Wire.available()) { // slave may send less than requested
char c = Wire.read(); // receive a byte as character

if (c == '0'){
val = analogRead(valPin);
Serial.println(val);
if (val >= 500){
  if(!smsSent){
  sendSMS(NUMBER,MSG);
  smsSent = true;
  }
}

else{
  smsSent = false;
}
}
}
}



bool initializeGSM() {
  char *command[] = { "AT", "ATE0", "AT+CPIN?", "AT+CLIP=1", "AT+CMEE=0", "AT+CMGF=1", "AT+CPBS=\"SM\"", "AT+CMGD=,4" };
  char *response[] = { "OK", "OK", "READY", "OK", "OK", "OK", "OK", "OK" };
  int  time[] = { 1000, 1000, 1000, 1000, 1000, 1000, 1000, 5000 };
 
  // initialize GSM 8 commands
  gsm.begin(9600);
  Serial.print("Initializing GSM.. ");
  for (int i = 0; i < 8; i++) {
    int tries = 0;
    // try each command for 5 times
    while (sendATCommandWaitResponse(command[i], response[i], time[i]) == false) {
      tries++;

      // if fail 10 times, exit
      if (tries == 10) {
        Serial.println("FAILED!");
        return false;
      }
    }
  }
  // wait for gsm to connect to network
  while (!sendATCommandWaitResponse("AT+CREG?", "+CREG: 0,1", 1000));
  Serial.println("OK!");
  return true;
  delay(10);
}



bool sendSMS(char *num, char *msg) {
  char buffer[30];
  byte i;
  // create end character to terminate SMS command
  char end[2];
  end[0] = 0x1a;
  end[1] = '\0';
 
  if (DEBUG) {
    Serial.print("Sending SMS.. ");
  }
  // issue send SMS command
  sprintf(buffer, "AT+CMGS=\"%s\"", num);
  // wait for reply to input message
  if (sendATCommandWaitResponse(buffer, ">", 1000)) {
    gsm.print(msg);
    // send end command to start sending sms
    if (sendATCommandWaitResponse(end, "+CMGS", 10000)) {
      if (DEBUG) {
        Serial.println("OK!");
      }
      return true;
    }
    else {
      if (DEBUG) {
        Serial.println("FAILED!");
      }
      return false;
    }
  }
  else {
    if (DEBUG) {
      Serial.println("FAILED!");
    }
    return false;
  }
}



bool sendATCommandWaitResponse(char* command, char* response, long timeout) {
  if (DEBUG) {
    Serial.print("Send: "); Serial.println(command);
  }
  gsm.println(command);

  long startTime = millis();
  int pointer = 0;

  // if not timed out, check for reply
  while (millis() - startTime < timeout) {
    while (gsm.available()) {
      char c = gsm.read();
      reply[pointer++] = c;
      reply[pointer] = '\0';
    }

    // check reply for the response we want
    if (strstr(reply, response) != NULL) {
      if (DEBUG) {
        Serial.print("OK! Got: "); Serial.println(reply);
        Serial.println();
      }
      return true;
    }
  }

  if (DEBUG) {
    Serial.print("FAILED! Got: "); Serial.println(reply);
    Serial.println();
  }
  return false;
}

this should do, you missed two } after smsSent = false;

this should do, you missed two } after smsSent = false;

It may well "do" it but your formatting is appalling

}
}
}
}

Really ?

UKHeliBob: It may well "do" it but your formatting is appalling

}
}
}
}

Really ?

there are couple of nested if's inside of loop()

KASSIMSAMJI: there are couple of nested if's inside of loop()

So, your indentation should reflect that.

}
}
}

is just as unacceptable as }}}

AWOL: So, your indentation should reflect that.

}
}
}

is just as unacceptable as }}}

i admit i am rude when indenting brackets