How to make the GSM900A send only one text

Hello everybody,

Recently I’ve been doing a home security project that sends a text to the user whenever a reed switch is triggered and the user could deactivate/activate it by texting to the GSM. For now i have 4 reed switch connecting to a Mega2560 with the GSM connecting in serial since I’m using SoftwareSerial library.

The problem is that once the switch is triggered(LOW to HIGH), the GSM keeps on sending messages repeatedly till the the switch turns back to its normal state(HIGH to LOW).

Here are the full code:

#include <SoftwareSerial.h>
SoftwareSerial mySerial(2, 3);
char msg;
char call;
int Reed1 = 25;
int Reed2 = 27;
int Reed3 = 29;
int Reed4 = 31;
int out = A0;
int val1;
int val2;
int val3;
int val4;
int state = LOW;
int place;

void setup()
{
  GsmSetup();
  pinMode(out, OUTPUT);
  pinMode(Reed1, INPUT);
  pinMode(Reed2, INPUT);
  pinMode(Reed3, INPUT);
  pinMode(Reed4, INPUT);
}

void GsmSetup()
{
  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();
  delay(100);
}

void loop()
{
  val1 = digitalRead(Reed1);
  val2 = digitalRead(Reed2);
  val3 = digitalRead(Reed3);
  val4 = digitalRead(Reed4);
  if (val1 == HIGH) {
    AlarmActivate();

    place = 1;
  }
  else if (val2 == HIGH) {
    AlarmActivate();
    Serial.println("REED 2 REED 2 REED 2 REED 2 REED 2 REED 2 REED 2 ");
    Serial.println();
    place = 2;
  }
  else if (val3 == HIGH) {
    AlarmActivate();
    Serial.println("REED 3 REED 3 REED 3 REED 3 REED 3 REED 3 REED 3 ");
    Serial.println();
    place = 3;
  }
  else if (val4 == HIGH) {
    AlarmActivate();
    Serial.println("REED 4 REED 4 REED 4 REED 4 REED 4 REED 4 REED 4 ");
    Serial.println();
    place = 4;
  }
  else
    digitalWrite(out, LOW);
  if (Serial.available() > 0)
    switch (Serial.read())
    {
      case 's':
        SendMessage();
        break;
      case 'c':
        MakeCall();
        break;
      case 'h':
        HangupCall();
        break;
    }
  if (mySerial.available() > 0)
    Serial.write(mySerial.read());
}

void AlarmActivate() {
  if (place == 1) {
    mySerial.println("AT+CMGF=1");
    delay(1000); 
    mySerial.println("AT+CMGS=\"+60xxxxxxxxx\"\r");
    delay(1000);
    mySerial.println("Reed 1");
    delay(100);
    mySerial.println((char)26);
    delay(1000);
  }
  else if (place == 2) {
    mySerial.println("AT+CMGF=1");   
    delay(1000);  
    mySerial.println("AT+CMGS=\"+60xxxxxxxxx\"\r"); 
    delay(1000);
    mySerial.println("Reed 2");
    delay(100);
    mySerial.println((char)26);
    delay(1000);
  }
}

/*

        GSM FUNCTIONS

*/



void SendMessage()
{
  mySerial.println("AT+CMGF=1"); 
  delay(1000);  
  mySerial.println("AT+CMGS=\"+60xxxxxxxxx\"\r");
  delay(1000);
  mySerial.println("sim900a sms");
  delay(100);
  mySerial.println((char)26);
  delay(1000);
}

void ReceiveMessage()
{
  mySerial.println("AT+CNMI=1,2,0,0,0"); 
  delay(1000);
  if (mySerial.available() > 0)
  {
    msg = mySerial.read();
    Serial.print(msg);
  }
}

void MakeCall() //  Test Purpose
{
  mySerial.println("ATD+60xxxxxxxxx;"); 
  Serial.println("Calling  "); 
  delay(1000);
}


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

Really hoped someone could be kind enough, as this is the first time i ever posted in a forum, plus im kinda new in this field.

connecting to a Mega2560 with the GSM connecting in serial since I'm using SoftwareSerial library.

With 4 hardware serial ports, why are you doing that?

SoftwareSerial mySerial(2, 3);

Not all pins on the Mega and Mega 2560 support change interrupts, so only the following can be used for RX: 10, 11, 12, 13, 14, 15, 50, 51, 52, 53, A8 (62), A9 (63), A10 (64), A11 (65), A12 (66), A13 (67), A14 (68), A15 (69).

If you ARE going to use SoftwareSerial, despite the 4 hardware serial ports, RTFM!

void AlarmActivate() {
  if (place == 1) {

The ONLY difference between your 4 long blocks of code is one character. Most of the code should NOT be in the body of any if statements.

The problem is that once the switch is triggered(LOW to HIGH), the GSM keeps on sending messages repeatedly till the the switch turns back to its normal state(HIGH to LOW).

No. The problem is that you haven't bothered to read the reference documentation or look at any of the examples that come with the IDE. It is entirely possible to detect that the state of the reed switches has changed.