GSM shield repeatedly sends sms...

GSM shield repeatedly sends sms.
How to make it send only 1 sms when the buttonState is HIGH?
Thank you.

Here's my code.

char Rx_data[50];
unsigned char Rx_index = 0;
int i = 0;
char msg[160];
int sig;
const int buttonPin = 2;
const int buttonPin1 = 4;
const int buttonPin2 = 6;
const int ledPin = 12;// the number of the pushbutton pin
const int ledPin1 = 13;
const int ledPin2 = 11;
const int buzzerPin = 8;
// the number of the LED pin

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

void setup() {
  Serial.begin(38400);
  // initialize the LED pin as an output:
  pinMode(ledPin, OUTPUT);   
  pinMode(ledPin1, OUTPUT);
  pinMode(ledPin2, OUTPUT);
  pinMode(buzzerPin, OUTPUT);  
  // initialize the pushbutton pin as an input:
  pinMode(buttonPin, INPUT_PULLUP); 
  pinMode(buttonPin1, INPUT_PULLUP);
  pinMode(buttonPin2, INPUT_PULLUP);  
}

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  && buttonState2 == LOW) {     
    // turn LED on:    
    digitalWrite(ledPin, HIGH);
    SendTextMessage();
    Serial.println("Alert Level 1!");
delay(150);


  } 
  else {
    // turn LED off:
    digitalWrite(ledPin, LOW); 
  }
  buttonState2 = digitalRead(buttonPin1);
  if (buttonState2 == HIGH  && buttonState3 == LOW) {     
    // turn LED on:    
    digitalWrite(ledPin1, HIGH);
    SendTextMessage();
  Serial.println("Alert Level 2!");  
delay(150);  
} 
  else {
    // turn LED off:
   
    digitalWrite(ledPin1, LOW); 
  }
  buttonState3 = digitalRead(buttonPin2);
  if (buttonState3 == HIGH) {     
    // turn LED on:    
    digitalWrite(ledPin2, HIGH);
    digitalWrite(buzzerPin, HIGH);
    SendTextMessage();
  Serial.println("Alert Level 3!");  
 delay(150);
  } 
  else {
    // turn LED off:
   
    digitalWrite(ledPin2, LOW); 
    digitalWrite(buzzerPin, LOW);
  }
}
//**********************************************************
void SendTextMessage()
{
 Serial.print("AT+CMGF=1\r");
 delay(100);
 Serial.println("AT + CMGS = \"09108690901\"");
 delay(100);
 Serial.println("Warning!");
 delay(100);
 Serial.println((char)26);
 delay(100);
 Serial.println();
}

up

You're using pull up resistors, which means, unless your button sends the input LOW, it is usually going to be high.

Therefore this line is going to evaluate to true if your button is NOT pressed.!

if (buttonState == HIGH  && buttonState2 == LOW) {

thanks for ur reply sir.
Is there any option to limit the send of sms to 1? because it keeps sending sms.

Add a variable, e.g. SmsSent, that you set to true once you sent the message. Before you would send another messege you'd check for that variable. Eventually you want to reset the value to false after a timeout.

thanks for ur reply sir.
Can u give me an example code?
So I can easily understand thanks.

Can u give me an example code?

Below are some previous post concerning setting flags and toggling. One has to be careful testing email code and similar as one can accidentally start a spew of messages and maybe blow out the email in box.

https://www.google.com/search?as_q=toggle+flag&as_epq=&as_oq=&as_eq=&as_nlo=&as_nhi=&lr=&cr=&as_qdr=all&as_sitesearch=http%3A%2F%2Fforum.arduino.cc%2Findex&as_occt=any&safe=images&tbs=&as_filetype=&as_rights=&gws_rd=ssl

Did you miss the point I made in answer #1. Your code is going to send a message if your button is NOT pressed. Surely this is not what you want?

Regardless of how you have the switches wired, surely you want to send a message when the switch BECOMES pressed, not when the switch IS pressed.

Look at the state change detection example.