How do i created one time event correlated with button status?

I am doing a tiny project that detect status of main power,
Basically i want to send a sms once main power is down, so far i came to a point that able to detect status of power and send the sms
But there is a problem, sending the sms keep repeating itself until power comes back on. i wanted to design it one sms once power down and no sms when power comes back on.


#include <SoftwareSerial.h>
SoftwareSerial mySerial(9, 10);
char msg;
char call;

void setup()
{
  pinMode(2, INPUT_PULLUP);
  pinMode(13,OUTPUT);
  mySerial.begin(9600);   // Setting the baud rate of GSM Module  
  Serial.begin(9600);    // Setting the baud rate of Serial Monitor (Arduino)
  Serial.println("GSM SIM900A BEGIN");
  Serial.println("Enter character for control option:");
  Serial.println("h : to disconnect a call");
  Serial.println("i : to receive a call");
  Serial.println("s : to send message");
  Serial.println("c : to make a call");  
  Serial.println("e : to redial");
  Serial.println();
  delay(100);
}

void loop()

{  int pushButton = digitalRead(2);
   if (pushButton == HIGH)
  {
    mySerial.println("AT+CMGF=1");    //Sets the GSM Module in Text Mode
    delay(1000);  // Delay of 1000 milli seconds or 1 second
    mySerial.println("AT+CMGS=\"+60XXXXXXXXX\"\r"); // Replace x with mobile number
    delay(1000);
    mySerial.println("sim900a sms");// The SMS text you want to send
    delay(100);
     mySerial.println((char)26);// ASCII code of CTRL+Z
    delay(1000);
   } else {
    digitalWrite(13,LOW);
    
   }
  
  if (Serial.available()>0)
   switch(Serial.read())
  {
    case 's':
      SendMessage();
      break;
    case 'c':
      MakeCall();
      break;
    case 'h':
      HangupCall();
      break;
    case 'e':
      RedialCall();
      break;
    case 'i':
      ReceiveCall();
      break;
  }
 if (mySerial.available()>0)
 Serial.write(mySerial.read());
}

void SendMessage()
{
  mySerial.println("AT+CMGF=1");    //Sets the GSM Module in Text Mode
  delay(1000);  // Delay of 1000 milli seconds or 1 second
  mySerial.println("AT+CMGS=\"+60XXXXXXXXX\"\r"); // Replace x with mobile number
  delay(1000);
  mySerial.println("sim900a sms");// The SMS text you want to send
  delay(100);
   mySerial.println((char)26);// ASCII code of CTRL+Z
  delay(1000);
}

void ReceiveMessage()
{
  mySerial.println("AT+CNMI=2,2,0,0,0"); // AT Command to recieve a live SMS
  delay(1000);
  if (mySerial.available()>0)
  {
    msg=mySerial.read();
    Serial.print(msg);
  }
}

void MakeCall()
{
  mySerial.println("ATD+60XXXXXXXXX;"); // ATDxxxxxxxxxx; -- watch out here for semicolon at the end!!
  Serial.println("Calling  "); // print response over serial port
  delay(1000);
}


void HangupCall()
{
  mySerial.println("ATH");
  Serial.println("Hangup Call");
  delay(1000);
}

void ReceiveCall()
{
  mySerial.println("ATA");
  delay(1000);
  {
    call=mySerial.read();
    Serial.print(call);
  }
}

void RedialCall()
{
  mySerial.println("ATDL");
  Serial.println("Redialing");
  delay(1000);
}

Also when it triggers the sms coad writing part to serial monitor takes about 3 second for each letter does anyone knows why its happening?

You need a bool flag like say "smsSent" initially false. Only send the sms if it is false. Set it true when the sms has been sent. Set it false again when the power comes back.

Thanks Buddy. appreciate your time (y)

Study state machines

Great help!
Again this is my first project even though somehow I managed to get little bit of understanding the principle, still having hard time to put into action. would you mind giving an example in relation to my problem?

Post you best attempt how you tried to code it. Make sure to post your complete sketch.
It doesn't matter if it does not compile or not work as intended. You should post your own attempt for two reasons:

  1. showing some own effort
  2. receive much better adapted support and explanation based on your attempt.

Additionally modifying code needs a bit more knowledge than just modifying a password.
You seem to be a real beginner which is completely OK.
If you want to become more independant of waiting for other users to post ready to apply code
there is no way around learning the basics of coding yourself.

Take a look into this tutorial:

Arduino Programming Course

It is easy to understand and has a good mixture between explaining important concepts and example-codes to get you going. So give it a try and report your opinion about this tutorial.

best regards Stefan

what do you intent to do with this line of code?

  smsSent = ("AT+CMGF=1"), ("AT+CMGS=\"+9700000000\"\r"), ("Power Off Again!"), ((char)26);

bool var = val; is the syntax the above language reference gives us.

Use it like this:

bool myFlag = false;

The variable myFlag is set to false. It is named a flag because it flags up to the microcontroller when we want to do something different.

So, pseudocode:
If myFlag == false do something
Else do something else

Now we have an if else statement

We now have 2 bits of code that will run. Do something will run if the flag is false and do something else will run if it is true. Now all you need to do is change the flag when a specific condition is met eg in pseudocode

If buttonPressed myFlag = true
You then change myFlag as required to change which bit of code will run

TBH I don't know. Nevertheless i delete everything i mean everything and started from clean sheet.

Your support is awesome, I've been looking for this answer but I couldn't find enough info to put everything together much appreciated.
Side note: I've started from clean sheet I find it much easier to learn.