Need help in troubleshooting this code involving Arduino Uno and GSM sim900

My aim of the project is to detect the presence of electricity and then turn on the motor or any appliance so I am firstly doing this on small scale.

How this code I want to work is: Pin number 8 will detect HIGH voltage level(i.e electricity present), i do this by giving it +5V then Arduino will send a message to given no. about the presence of electricity then the user will send SMS to gsm module to turn the led on(#0) or off(#1) connected to pin no. 13.

#include <SoftwareSerial.h>
SoftwareSerial mySerial(10, 11); // RX, TX connnecetd t0 GSM module
boolean bOK = true;
char msg;
int pinstate = 0;  //current pinstate of pin 8
int lastpinstate = 0;  //last pinstate of pin 8
void setup()
{
  pinMode(8, INPUT); //detects high or low signal given manually from +5V pin
  pinMode(13, OUTPUT); //led
  Serial.begin(9600);  //For Serial monitor
  mySerial.begin(9600);  //For GSM MODULE
  digitalWrite(13, HIGH);
  mySerial.println("AT");
  delay(1200);
  digitalWrite(13, LOW);
}
void loop()
{
  if (bOK == 1) //loop-1=detects if arduino and gsm are working or not
  {
    mySerial.println("AT+CMGF=1"); // sets the SMS mode to text
    delay(1500);
    mySerial.print("AT+CMGS=\""); // send the SMS number
    mySerial.print("+9196xxxxxxx");
    mySerial.print("\"");
    delay(1000);
    //mySerial.println("Arduino & GSM On"); // sent SMS on mobile
    Serial.println("Arduino & GSM On"); // SMS displayed on  monitor
    delay(2500);
    mySerial.write(0x1A); // sends ctrl+z- end of message
    mySerial.write(0x0D); // carriage return = go back to starting i.e left
    mySerial.write(0x0A);  // new line
    bOK = LOW;
  }
  pinstate = digitalRead(8);
  if (pinstate != lastpinstate) //loop-2
  {
    if (pinstate == HIGH) //loop-3= if high is given
    {
      mySerial.print("AT+CMGS=\""); // send the SMS number
      mySerial.print("+9196xxxxxxx");
      mySerial.println("\"");
      delay(1000);
      //mySerial.print("Mains Available"); // SMS body
      Serial.println("Mains Available"); // SMS body
      delay(1000);
      mySerial.write(0x1A);
      mySerial.write(0x0D);
      mySerial.write(0x0A);
      delay(3000);
      mySerial.println("AT+CNMI=2,2,0,0,0");
      Serial.println("AT+CNMI=2,2,0,0,0");
      if (mySerial.available() > 0) //loop-4=to read received message
      {
        delay(1000);
        msg = mySerial.read();
        Serial.println(msg);
        if (msg == '#') //loop-5
        {
          Serial.println("HERE");
          delay(1000);
          msg = mySerial.read();
          Serial.println(msg);
          if (msg == '0') //loop-6
          {
            delay(1000);
            Serial.println(msg);
            digitalWrite(13, HIGH);
          }
          else if (msg == '1')   //loop-7
          {
            delay(1000);
            Serial.print(msg);
            digitalWrite(13, LOW);
          }
        }
      }
    }
    else            //loop-8=high level removed(low)
    {
      mySerial.print("AT+CMGS=\""); // send the SMS number
      mySerial.print("+9196xxxxxxx");
      mySerial.println("\"");
      delay(1000);
      //mySerial.print("Mains Off"); // SMS body
      Serial.println("Mains Off"); // SMS body
      delay(1000);
      mySerial.write(0x1A);
      mySerial.write(0x0D);
      mySerial.write(0x0A);
      delay(3000);
    }
  }
  lastpinstate = pinstate;
}

Where the problem is: The code is working except in loop where I can on/off the led i.e loop 4,5,6,7. I am getting messages of electricity(High) available and off(low) but when electricity is available I can’t on or off the led.

Thanks.

Seems overly complicated. Try designing this as a state machine, it will be simpler

(Also for consistency, if bOK is a boolean then use true and false everywhere, not 1 or LOW)