user31518: Please check the arduino program given below and help me to find out error.
The program is for following hardware.
GSM 800 module
Arduino Uno board
Limit switch(2)
We place two limit switch at main door & back door as main door is open the message will be send that is Main door is open & when it closed it will send message Main door is closed. This is same for Back door also.
We run this program & it run but it sends the massage repeatedly after some time.
Please help me to find out error..
my ARDUINO code is
const int Main = 7;
const int Store = 8;
const int Main_led =  13;
const int Store_led =  14;
bool i = true;
bool j = true;
bool k = true;
bool l = true;
int buttonState1 = 0;
int buttonState2 = 0;
void setup() {

  Serial.begin(9600);
  pinMode(Main_led, OUTPUT);
  pinMode(Store_led, OUTPUT);
  pinMode(Main, INPUT);
  pinMode(Store, INPUT);
}

void loop() {
  buttonState1 = digitalRead(Main);
  if (buttonState1 == HIGH && i == true ) {
    digitalWrite(Main_led, HIGH);
    Serial.println("OK");
    delay(1000);
    Serial.print("AT+CMGF=1\r\n");
    delay(500);
    Serial.print("AT+CMGS=\"+91-------------");
    Serial.print("\"\r\n");
    delay(5000);
    Serial.print("Main Door is closed");
    Serial.write(0x1A);
    delay(5000);
    i = false;
    j = true;
  }
  else
  { if (buttonState1 == LOW && j == true )

    { digitalWrite(Main_led, LOW);
      Serial.println("OK");
      delay(1000);
      Serial.print("AT+CMGF=1\r\n");
      delay(500);
      Serial.print("AT+CMGS=\"");
      Serial.print("+91---------------");
      Serial.print("\"\r\n");
      delay(5000);
      Serial.print("Main Door is open");
      Serial.write(0x1A);
      delay(5000);
      i = true;
      j = false;
    }
  }

  buttonState2 = digitalRead(Store);
  if (buttonState2 == HIGH && k == true) {

    digitalWrite(Store_led, HIGH);
    Serial.println("OK");
    delay(1000);
    Serial.print("AT+CMGF=1\r\n");
    delay(500);
    Serial.print("AT+CMGS=\"");
    Serial.print("+91-------------");
    Serial.print("\"\r\n");
    delay(5000);
    Serial.print("Store room is Closed");
    Serial.write(0x1A);
    delay(5000);
    k = false;
    l = true;
  }
  else
  { if (buttonState2 == LOW && l == true)
    {
      digitalWrite(Store_led, LOW);
      Serial.println("OK");
      delay(1000);
      Serial.print("AT+CMGF=1\r\n");
      delay(500);
      Serial.print("AT+CMGS=\"");
      Serial.print("+91------------------");
      Serial.print("\"\r\n");
      delay(5000);
      Serial.print("Store room is Open");
      Serial.write(0x1A);
      delay(5000);
      k = true;
      l = false;
    }
  }
}


user24459: I can't see any obvious flaws in the code other than the excessive use of delay. However you could make your code a lot cleaner which makes it easier to spot the errors.
The version below has the duplicate lines moved into a function and instead of using the poorly named i,j,k & l to track two states it uses two variables to do the same job. Finally things that don't need to be global have been moved to being local.
const int Main = 7;
const int Store = 8;
const int Main_led =  13;
const int Store_led =  14;

void setup() {
  Serial.begin(9600);
  pinMode(Main_led, OUTPUT);
  pinMode(Store_led, OUTPUT);
  pinMode(Main, INPUT);
  pinMode(Store, INPUT);
}

void sendMessage(char *text) {
    Serial.println("OK");
    delay(1000);
    Serial.print("AT+CMGF=1\r\n");
    delay(500);
    Serial.print("AT+CMGS=\"+91-------------");
    Serial.print("\"\r\n");
    delay(5000);
    Serial.print(text);
    Serial.write(0x1A);
    delay(5000);
}

void loop() {
  static int oldState1 = -1;
  static int oldState2 = -1;

  int buttonState1 = digitalRead(Main);
  if (buttonState1 != oldState1) {
    digitalWrite(Main_led, buttonState1);
    if (buttonState1 == HIGH)
      sendMessage("Main Door is closed");
    else
      sendMessage("Main Door is open");
    oldState1 = buttonState1;
  }

  int buttonState2 = digitalRead(Store);
  if (buttonState2 != oldState2) {
    digitalWrite(Store_led, buttonState2 );
    if (buttonState2 == HIGH)
      sendMessage("Store room is closed");
    else
      sendMessage("Store room is open");
    oldState2 = buttonState2;
  }
}


31518: <p>Please check the arduino program given below and help me to find out error.
The program is for following hardware.
GSM 800 module
Arduino Uno board
Limit switch(2)</p>

<p>We place two limit switch at main door &amp; back door as main door is open the message will be send that is Main door is open &amp; when it closed it will send message Main door is closed. This is same for Back door also.</p>

<p>We run this program &amp; it run but it sends the massage repeatedly after some time.
Please help me to find out error..</p>

<p><strong><em>my ARDUINO code is</em></strong></p>

<pre><code>const int Main = 7;
const int Store = 8;
const int Main_led =  13;
const int Store_led =  14;
bool i = true;
bool j = true;
bool k = true;
bool l = true;
int buttonState1 = 0;
int buttonState2 = 0;
void setup() {

  Serial.begin(9600);
  pinMode(Main_led, OUTPUT);
  pinMode(Store_led, OUTPUT);
  pinMode(Main, INPUT);
  pinMode(Store, INPUT);
}

void loop() {
  buttonState1 = digitalRead(Main);
  if (buttonState1 == HIGH &amp;&amp; i == true ) {
    digitalWrite(Main_led, HIGH);
    Serial.println("OK");
    delay(1000);
    Serial.print("AT+CMGF=1\r\n");
    delay(500);
    Serial.print("AT+CMGS=\"+91-------------");
    Serial.print("\"\r\n");
    delay(5000);
    Serial.print("Main Door is closed");
    Serial.write(0x1A);
    delay(5000);
    i = false;
    j = true;
  }
  else
  { if (buttonState1 == LOW &amp;&amp; j == true )

    { digitalWrite(Main_led, LOW);
      Serial.println("OK");
      delay(1000);
      Serial.print("AT+CMGF=1\r\n");
      delay(500);
      Serial.print("AT+CMGS=\"");
      Serial.print("+91---------------");
      Serial.print("\"\r\n");
      delay(5000);
      Serial.print("Main Door is open");
      Serial.write(0x1A);
      delay(5000);
      i = true;
      j = false;
    }
  }

  buttonState2 = digitalRead(Store);
  if (buttonState2 == HIGH &amp;&amp; k == true) {

    digitalWrite(Store_led, HIGH);
    Serial.println("OK");
    delay(1000);
    Serial.print("AT+CMGF=1\r\n");
    delay(500);
    Serial.print("AT+CMGS=\"");
    Serial.print("+91-------------");
    Serial.print("\"\r\n");
    delay(5000);
    Serial.print("Store room is Closed");
    Serial.write(0x1A);
    delay(5000);
    k = false;
    l = true;
  }
  else
  { if (buttonState2 == LOW &amp;&amp; l == true)
    {
      digitalWrite(Store_led, LOW);
      Serial.println("OK");
      delay(1000);
      Serial.print("AT+CMGF=1\r\n");
      delay(500);
      Serial.print("AT+CMGS=\"");
      Serial.print("+91------------------");
      Serial.print("\"\r\n");
      delay(5000);
      Serial.print("Store room is Open");
      Serial.write(0x1A);
      delay(5000);
      k = true;
      l = false;
    }
  }
}
</code></pre>
 24459: <p>I can't see any obvious flaws in the code other than the excessive use of delay. However you could make your code a lot cleaner which makes it easier to spot the errors.<br>
The version below has the duplicate lines moved into a function and instead of using the poorly named i,j,k &amp; l to track two states it uses two variables to do the same job. Finally things that don't need to be global have been moved to being local.</p>

<pre><code>const int Main = 7;
const int Store = 8;
const int Main_led =  13;
const int Store_led =  14;

void setup() {
  Serial.begin(9600);
  pinMode(Main_led, OUTPUT);
  pinMode(Store_led, OUTPUT);
  pinMode(Main, INPUT);
  pinMode(Store, INPUT);
}

void sendMessage(char *text) {
    Serial.println("OK");
    delay(1000);
    Serial.print("AT+CMGF=1\r\n");
    delay(500);
    Serial.print("AT+CMGS=\"+91-------------");
    Serial.print("\"\r\n");
    delay(5000);
    Serial.print(text);
    Serial.write(0x1A);
    delay(5000);
}

void loop() {
  static int oldState1 = -1;
  static int oldState2 = -1;

  int buttonState1 = digitalRead(Main);
  if (buttonState1 != oldState1) {
    digitalWrite(Main_led, buttonState1);
    if (buttonState1 == HIGH)
      sendMessage("Main Door is closed");
    else
      sendMessage("Main Door is open");
    oldState1 = buttonState1;
  }

  int buttonState2 = digitalRead(Store);
  if (buttonState2 != oldState2) {
    digitalWrite(Store_led, buttonState2 );
    if (buttonState2 == HIGH)
      sendMessage("Store room is closed");
    else
      sendMessage("Store room is open");
    oldState2 = buttonState2;
  }
}
</code></pre>
